forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15e1ca0
commit f2c5dec
Showing
5 changed files
with
208 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 17 additions & 37 deletions
54
packages/plugin-nft-collections/src/services/cache-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,37 @@ | ||
interface CacheConfig { | ||
ttl: number; | ||
maxSize: number; | ||
export interface CacheManager { | ||
get<T>(key: string): Promise<T | null>; | ||
set<T>(key: string, value: T, ttl?: number): Promise<void>; | ||
clear(): Promise<void>; | ||
} | ||
|
||
interface CacheEntry<T> { | ||
data: T; | ||
timestamp: number; | ||
} | ||
|
||
export class CacheManager { | ||
private cache: Map<string, CacheEntry<any>>; | ||
private config: CacheConfig; | ||
export class MemoryCacheManager implements CacheManager { | ||
private cache: Map<string, { value: any; expiry: number }>; | ||
private defaultTtl: number; | ||
|
||
constructor(config: CacheConfig) { | ||
this.config = config; | ||
constructor(defaultTtl: number = 3600000) { | ||
// 1 hour default | ||
this.cache = new Map(); | ||
this.defaultTtl = defaultTtl; | ||
} | ||
|
||
async get<T>(key: string): Promise<T | null> { | ||
const entry = this.cache.get(key); | ||
if (!entry) return null; | ||
const item = this.cache.get(key); | ||
if (!item) return null; | ||
|
||
// Check if entry has expired | ||
if (Date.now() - entry.timestamp > this.config.ttl) { | ||
if (Date.now() > item.expiry) { | ||
this.cache.delete(key); | ||
return null; | ||
} | ||
|
||
return entry.data; | ||
} | ||
|
||
async set<T>(key: string, data: T): Promise<void> { | ||
// Implement LRU eviction if cache is full | ||
if (this.cache.size >= this.config.maxSize) { | ||
const oldestKey = this.cache.keys().next().value; | ||
this.cache.delete(oldestKey); | ||
} | ||
|
||
this.cache.set(key, { | ||
data, | ||
timestamp: Date.now(), | ||
}); | ||
return item.value as T; | ||
} | ||
|
||
async delete(key: string): Promise<void> { | ||
this.cache.delete(key); | ||
async set<T>(key: string, value: T, ttl?: number): Promise<void> { | ||
const expiry = Date.now() + (ttl || this.defaultTtl); | ||
this.cache.set(key, { value, expiry }); | ||
} | ||
|
||
async clear(): Promise<void> { | ||
this.cache.clear(); | ||
} | ||
|
||
async has(key: string): Promise<boolean> { | ||
return this.cache.has(key); | ||
} | ||
} |
Oops, something went wrong.