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.
29 lines
978 B
29 lines
978 B
3 years ago
|
import {Collection} from "@extollo/util"
|
||
|
import {Cache} from "./Cache"
|
||
|
|
||
|
export class MemoryCache extends Cache {
|
||
|
private static cacheItems: Collection<{key: string, value: string}> = new Collection<{key: string; value: string}>()
|
||
|
|
||
|
public fetch(key: string): string | Promise<string | undefined> | undefined {
|
||
|
return MemoryCache.cacheItems
|
||
|
.firstWhere('key', '=', key)?.value
|
||
|
}
|
||
|
|
||
|
public put(key: string, value: string): void | Promise<void> {
|
||
|
const existing = MemoryCache.cacheItems.firstWhere('key', '=', key)
|
||
|
if ( existing ) {
|
||
|
existing.value = value
|
||
|
} else {
|
||
|
MemoryCache.cacheItems.push({ key, value })
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public has(key: string): boolean | Promise<boolean> {
|
||
|
return !!MemoryCache.cacheItems.firstWhere('key', '=', key)
|
||
|
}
|
||
|
|
||
|
public drop(key: string): void | Promise<void> {
|
||
|
MemoryCache.cacheItems = MemoryCache.cacheItems.where('key', '!=', key)
|
||
|
}
|
||
|
}
|