Add cache interface, factory, and basic in-mem implementation

This commit is contained in:
2021-03-09 10:16:27 -06:00
parent 42d9cc101f
commit 088fdfb1ef
5 changed files with 138 additions and 0 deletions

28
src/support/cache/MemoryCache.ts vendored Normal file
View File

@@ -0,0 +1,28 @@
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)
}
}