Skip to content

Commit

Permalink
feat(cache): create interface for KeyPairValue and fix issue with und…
Browse files Browse the repository at this point in the history
…efined values
  • Loading branch information
prisca-c committed Dec 23, 2023
1 parent 1f9549e commit 1b6f5ef
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CacheInterface } from './types/cache_interface.js'
import { KeyValuePairInterface } from './types/key_value_pair_interface.js'

/**
* Cache is a class that implements the CacheInterface interface.
Expand All @@ -7,7 +8,7 @@ import { CacheInterface } from './types/cache_interface.js'
* @implements {CacheInterface}
*/
export class Cache implements CacheInterface {
protected cache: Map<string, { value: object; expiry: number }>
protected cache: Map<string, Omit<KeyValuePairInterface, 'key'>>

constructor() {
this.cache = new Map()
Expand All @@ -18,17 +19,16 @@ export class Cache implements CacheInterface {
if (!entry) return undefined

const now = Date.now()
if (now >= entry.expiry) {
console.log('Cache entry expired')
if (entry.expiry && now >= entry.expiry) {
this.cache.delete(key)
return undefined
}
return entry.value
}

set(key: string, value: object, ttl?: number): void {
const expiry = ttl ? Date.now() + ttl : Number.POSITIVE_INFINITY
this.cache.set(key, { value, expiry })
set(pair: KeyValuePairInterface): void {
const expiry = pair.expiry ? Date.now() + pair.expiry : Number.POSITIVE_INFINITY
this.cache.set(pair.key, { value: pair.value, expiry })
}

has(key: string): boolean {
Expand All @@ -39,7 +39,7 @@ export class Cache implements CacheInterface {
const now = Date.now()
return Array.from(this.cache.entries())
.filter(([key, { expiry }]) => {
if (now >= expiry) {
if (expiry && now >= expiry) {
this.cache.delete(key)
return false
}
Expand Down
8 changes: 4 additions & 4 deletions src/types/cache_interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { KeyValuePairInterface } from './key_value_pair_interface.js'

/**
* CacheInterface is an interface that defines the structure of a cache object.
* It includes methods for getting, setting, deleting, and clearing cache entries,
Expand All @@ -15,11 +17,9 @@ export interface CacheInterface {

/**
* Sets the value of a cache entry with the specified key.
* @param key - The key of the cache entry.
* @param value - The value to set for the cache entry.
* @param ttl - The time-to-live (in seconds) for the cache entry. Optional.
* @param pair - The key-value pair to be set.
*/
set(key: string, value: object, ttl?: number): void
set(pair: KeyValuePairInterface): void

/**
* Deletes a cache entry with the specified key.
Expand Down
5 changes: 5 additions & 0 deletions src/types/key_value_pair_interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface KeyValuePairInterface {
key: string
value: object
expiry?: number
}

0 comments on commit 1b6f5ef

Please sign in to comment.