Add cache interface, factory, and basic in-mem implementation
This commit is contained in:
28
src/support/cache/MemoryCache.ts
vendored
Normal file
28
src/support/cache/MemoryCache.ts
vendored
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user