Skip to content

Commit

Permalink
Enhancement: Upgrade cache to use Map for improved performance
Browse files Browse the repository at this point in the history
  • Loading branch information
ekamahuja committed Feb 24, 2024
1 parent 538865a commit e3df1e9
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 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,19 @@ 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 e3df1e9

Please sign in to comment.