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.
 
 
 
 

77 lines
1.9 KiB

import type { CacheDriver, CacheManagerOptions } from '../types'
export class CacheManager {
private drivers: CacheDriver[]
private defaultTtl: number
constructor(options: CacheManagerOptions) {
if (!options.drivers || options.drivers.length === 0) {
throw new Error('[cache] at least one driver is required')
}
this.drivers = options.drivers
this.defaultTtl = options.defaultTtl ?? 0
}
async get<T>(key: string): Promise<T | null> {
for (const driver of this.drivers) {
try {
const value = await driver.get<T>(key)
return value
} catch (err) {
console.warn(`[cache] ${driver.name} get failed:`, err)
continue
}
}
return null
}
async set<T>(key: string, value: T, ttl?: number): Promise<void> {
const effectiveTtl = ttl ?? this.defaultTtl
await Promise.all(
this.drivers.map(async (driver) => {
try {
await driver.set(key, value, effectiveTtl)
} catch (err) {
console.warn(`[cache] ${driver.name} set failed:`, err)
}
})
)
}
async del(key: string): Promise<void> {
await Promise.all(
this.drivers.map(async (driver) => {
try {
await driver.del(key)
} catch (err) {
console.warn(`[cache] ${driver.name} del failed:`, err)
}
})
)
}
async exists(key: string): Promise<boolean> {
for (const driver of this.drivers) {
try {
const exists = await driver.exists(key)
if (exists) return true
} catch (err) {
console.warn(`[cache] ${driver.name} exists failed:`, err)
continue
}
}
return false
}
async clear(): Promise<void> {
await Promise.all(
this.drivers.map(async (driver) => {
try {
await driver.clear()
} catch (err) {
console.warn(`[cache] ${driver.name} clear failed:`, err)
}
})
)
}
}