mirror of
https://github.com/elk-zone/elk.git
synced 2024-12-24 21:48:16 +03:00
320ddc0e28
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: TAKAHASHI Shuuji <shuuji3@gmail.com>
37 lines
888 B
TypeScript
37 lines
888 B
TypeScript
import type { Driver } from 'unstorage'
|
|
import { defineDriver } from 'unstorage'
|
|
import memory from 'unstorage/drivers/memory'
|
|
|
|
export interface CacheDriverOptions {
|
|
driver: Driver
|
|
}
|
|
|
|
export default defineDriver((driver: Driver = memory()) => {
|
|
const memoryDriver = memory()
|
|
return {
|
|
...driver,
|
|
async hasItem(key: string) {
|
|
if (await memoryDriver.hasItem(key, {}))
|
|
return true
|
|
|
|
return driver.hasItem(key, {})
|
|
},
|
|
async setItem(key: string, value: any) {
|
|
await Promise.all([
|
|
memoryDriver.setItem?.(key, value, {}),
|
|
driver.setItem?.(key, value, {}),
|
|
])
|
|
},
|
|
async getItem(key: string) {
|
|
let value = await memoryDriver.getItem(key)
|
|
|
|
if (value !== null)
|
|
return value
|
|
|
|
value = await driver.getItem(key)
|
|
memoryDriver.setItem?.(key, value as string, {})
|
|
|
|
return value
|
|
},
|
|
}
|
|
})
|