mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-13 08:12:40 +10:00
feat: unified cache handler
This commit is contained in:
33
server/internal/cache/cacheHandler.ts
vendored
Normal file
33
server/internal/cache/cacheHandler.ts
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
import { prefixStorage, type StorageValue, type Storage } from "unstorage";
|
||||
|
||||
export interface CacheProviderOptions {
|
||||
/**
|
||||
* Max number of items in the cache
|
||||
*/
|
||||
max?: number;
|
||||
|
||||
/**
|
||||
* Time to live (in ms)
|
||||
*/
|
||||
ttl?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and manages the lifecycles of various caches
|
||||
*/
|
||||
export class CacheHandler {
|
||||
private caches = new Map<string, Storage<StorageValue>>();
|
||||
|
||||
/**
|
||||
* Create a new cache
|
||||
* @param name
|
||||
* @returns
|
||||
*/
|
||||
createCache<V extends StorageValue>(name: string) {
|
||||
// will allow us to dynamicing use redis in the future just by changing the storage used
|
||||
const provider = prefixStorage<V>(useStorage<V>("appCache"), name);
|
||||
// hack to let ts have us store cache
|
||||
this.caches.set(name, provider as unknown as Storage<StorageValue>);
|
||||
return provider;
|
||||
}
|
||||
}
|
||||
4
server/internal/cache/index.ts
vendored
Normal file
4
server/internal/cache/index.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
import { CacheHandler } from "./cacheHandler";
|
||||
|
||||
export const cacheHandler = new CacheHandler();
|
||||
export default cacheHandler;
|
||||
Reference in New Issue
Block a user