You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
import { Cache } from './Cache'
|
|
import { Collection } from '../collection/Collection'
|
|
|
|
/**
|
|
* Base interface for an item stored in a memory cache.
|
|
*/
|
|
export interface InMemCacheItem {
|
|
key: string,
|
|
item: string,
|
|
}
|
|
|
|
/**
|
|
* A cache implementation stored in memory.
|
|
* @extends Cache
|
|
*/
|
|
export class InMemCache extends Cache {
|
|
/**
|
|
* The stored cache items.
|
|
* @type Collection<InMemCacheItem>
|
|
*/
|
|
protected items: Collection<InMemCacheItem> = new Collection<InMemCacheItem>()
|
|
|
|
public async fetch(key: string): Promise<string | undefined> {
|
|
const item = this.items.firstWhere('key', '=', key)
|
|
if ( item ) {
|
|
return item.item
|
|
}
|
|
}
|
|
|
|
public async put(key: string, item: string): Promise<void> {
|
|
const existing = this.items.firstWhere('key', '=', key)
|
|
if ( existing ) {
|
|
existing.item = item
|
|
} else {
|
|
this.items.push({ key,
|
|
item })
|
|
}
|
|
}
|
|
|
|
public async has(key: string): Promise<boolean> {
|
|
return this.items.where('key', '=', key).length > 0
|
|
}
|
|
|
|
public async drop(key: string): Promise<void> {
|
|
this.items = this.items.whereNot('key', '=', key)
|
|
}
|
|
}
|