Skip to content

Commit

Permalink
Merge pull request #167 from ekamahuja/memor-engine-map
Browse files Browse the repository at this point in the history
Enhancement: Upgrade cache to use Map for improved performance
  • Loading branch information
ilovepixelart authored Feb 24, 2024
2 parents 538865a + cb72814 commit 8f50596
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/cache/engine/MemoryCacheEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import type IData from '../../interfaces/IData'
import type ICacheEngine from '../../interfaces/ICacheEngine'

class MemoryCacheEngine implements ICacheEngine {
private cache: Record<string, { value: IData, expiresAt: number } | undefined>
private cache: Map<string, { value: IData, expiresAt: number } | undefined>

constructor () {
this.cache = {}
this.cache = new Map()
}

get (key: string): IData {
const item = this.cache[key]
const item = this.cache.get(key)
if (!item || item.expiresAt < Date.now()) {
this.del(key)
return undefined
Expand All @@ -18,16 +18,18 @@ class MemoryCacheEngine implements ICacheEngine {
}

set (key: string, value: IData, ttl = Infinity): void {
this.cache[key] = { value, expiresAt: Date.now() + ttl }
this.cache.set(key, {
value,
expiresAt: Date.now() + ttl
})
}

del (key: string): void {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this.cache[key]
this.cache.delete(key)
}

clear (): void {
this.cache = {}
this.cache.clear()
}

close (): void {
Expand Down

0 comments on commit 8f50596

Please sign in to comment.