Improving type handling.

This commit is contained in:
mike12345567 2023-12-13 15:39:04 +00:00
parent 48d70c45b9
commit c8128325c6
1 changed files with 8 additions and 5 deletions

View File

@ -7,7 +7,7 @@ import * as locks from "../redis/redlockImpl"
const DEFAULT_WRITE_RATE_MS = 10000 const DEFAULT_WRITE_RATE_MS = 10000
let CACHE: BaseCache | null = null let CACHE: BaseCache | null = null
interface CacheItem { interface CacheItem<T extends Document> {
doc: any doc: any
lastWrite: number lastWrite: number
} }
@ -24,7 +24,10 @@ function makeCacheKey(db: Database, key: string) {
return db.name + key return db.name + key
} }
function makeCacheItem(doc: any, lastWrite: number | null = null): CacheItem { function makeCacheItem<T extends Document>(
doc: T,
lastWrite: number | null = null
): CacheItem<T> {
return { doc, lastWrite: lastWrite || Date.now() } return { doc, lastWrite: lastWrite || Date.now() }
} }
@ -35,7 +38,7 @@ async function put(
) { ) {
const cache = await getCache() const cache = await getCache()
const key = doc._id const key = doc._id
let cacheItem: CacheItem | undefined let cacheItem: CacheItem<any> | undefined
if (key) { if (key) {
cacheItem = await cache.get(makeCacheKey(db, key)) cacheItem = await cache.get(makeCacheKey(db, key))
} }
@ -87,13 +90,13 @@ async function put(
async function get<T extends Document>(db: Database, id: string): Promise<T> { async function get<T extends Document>(db: Database, id: string): Promise<T> {
const cache = await getCache() const cache = await getCache()
const cacheKey = makeCacheKey(db, id) const cacheKey = makeCacheKey(db, id)
let cacheItem: CacheItem = await cache.get(cacheKey) let cacheItem: CacheItem<T> = await cache.get(cacheKey)
if (!cacheItem) { if (!cacheItem) {
const doc = await db.get<T>(id) const doc = await db.get<T>(id)
cacheItem = makeCacheItem(doc) cacheItem = makeCacheItem(doc)
await cache.store(cacheKey, cacheItem) await cache.store(cacheKey, cacheItem)
} }
return cacheItem.doc as T return cacheItem.doc
} }
async function remove(db: Database, docOrId: any, rev?: any): Promise<void> { async function remove(db: Database, docOrId: any, rev?: any): Promise<void> {