|
|
|
@ -2,24 +2,34 @@ 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}>()
|
|
|
|
|
private static cacheItems: Collection<{key: string, value: string, expires?: Date}> = new Collection<{key: string; value: string, expires?: Date}>()
|
|
|
|
|
|
|
|
|
|
public fetch(key: string): string | Promise<string | undefined> | undefined {
|
|
|
|
|
const now = new Date()
|
|
|
|
|
return MemoryCache.cacheItems
|
|
|
|
|
.firstWhere('key', '=', key)?.value
|
|
|
|
|
.where('key', '=', key)
|
|
|
|
|
.firstWhere(item => {
|
|
|
|
|
return !item.expires || now < item.expires
|
|
|
|
|
})?.value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public put(key: string, value: string): void | Promise<void> {
|
|
|
|
|
public put(key: string, value: string, expires?: Date): void | Promise<void> {
|
|
|
|
|
const existing = MemoryCache.cacheItems.firstWhere('key', '=', key)
|
|
|
|
|
if ( existing ) {
|
|
|
|
|
existing.value = value
|
|
|
|
|
existing.expires = expires
|
|
|
|
|
} else {
|
|
|
|
|
MemoryCache.cacheItems.push({ key, value })
|
|
|
|
|
MemoryCache.cacheItems.push({ key, value, expires })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public has(key: string): boolean | Promise<boolean> {
|
|
|
|
|
return !!MemoryCache.cacheItems.firstWhere('key', '=', key)
|
|
|
|
|
const now = new Date()
|
|
|
|
|
return !!MemoryCache.cacheItems
|
|
|
|
|
.where('key', '=', key)
|
|
|
|
|
.firstWhere(item => {
|
|
|
|
|
return !item.expires || now < item.expires
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public drop(key: string): void | Promise<void> {
|
|
|
|
|