2022-11-24 02:12:25 +03:00
|
|
|
import type { Driver } from 'unstorage'
|
2023-02-12 14:56:29 +03:00
|
|
|
import memory from 'unstorage/drivers/memory'
|
2022-11-24 02:12:25 +03:00
|
|
|
import { defineDriver } from 'unstorage'
|
|
|
|
|
|
|
|
export interface CacheDriverOptions {
|
|
|
|
driver: Driver
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defineDriver((driver: Driver = memory()) => {
|
|
|
|
const memoryDriver = memory()
|
|
|
|
return {
|
|
|
|
...driver,
|
|
|
|
async hasItem(key: string) {
|
2024-09-11 14:20:26 +03:00
|
|
|
if (await memoryDriver.hasItem(key, {}))
|
2022-11-24 02:12:25 +03:00
|
|
|
return true
|
|
|
|
|
2023-07-02 20:11:13 +03:00
|
|
|
return driver.hasItem(key, {})
|
2022-11-24 02:12:25 +03:00
|
|
|
},
|
|
|
|
async setItem(key: string, value: any) {
|
|
|
|
await Promise.all([
|
2024-09-11 14:20:26 +03:00
|
|
|
memoryDriver.setItem?.(key, value, {}),
|
2023-07-02 20:11:13 +03:00
|
|
|
driver.setItem?.(key, value, {}),
|
2022-11-24 02:12:25 +03:00
|
|
|
])
|
|
|
|
},
|
|
|
|
async getItem(key: string) {
|
|
|
|
let value = await memoryDriver.getItem(key)
|
|
|
|
|
|
|
|
if (value !== null)
|
|
|
|
return value
|
|
|
|
|
|
|
|
value = await driver.getItem(key)
|
2024-09-11 14:20:26 +03:00
|
|
|
memoryDriver.setItem?.(key, value as string, {})
|
2022-11-24 02:12:25 +03:00
|
|
|
|
|
|
|
return value
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|