mirror of
https://github.com/khairul169/home-lab.git
synced 2025-04-28 16:49:36 +07:00
43 lines
965 B
TypeScript
43 lines
965 B
TypeScript
import type { Context, Next } from "hono";
|
|
import LRU from "../lib/lruCache";
|
|
|
|
type CacheOptions = {
|
|
ttl?: number;
|
|
noCache?: boolean;
|
|
public?: boolean;
|
|
store?: boolean;
|
|
};
|
|
|
|
const caches = new LRU<string>(50);
|
|
|
|
const cache = (options: CacheOptions = {}) => {
|
|
return (c: Context, next: Next) => {
|
|
const ttl = options.ttl || 3600;
|
|
|
|
const cacheControl = !options.noCache
|
|
? `public, max-age=${ttl}`
|
|
: "no-cache";
|
|
c.header("Cache-Control", cacheControl);
|
|
|
|
return next();
|
|
};
|
|
};
|
|
|
|
cache.key = (...keys: string[]) => {
|
|
const key = keys.join(".");
|
|
const data = cache.get(key);
|
|
const set = (value: any, ttl: number = 3600) => cache.set(key, value, ttl);
|
|
return { key, data, set };
|
|
};
|
|
|
|
cache.set = (key: string, value: any, ttl: number = 3600) => {
|
|
caches.set(key, JSON.stringify(value), ttl);
|
|
};
|
|
|
|
cache.get = (key: string) => {
|
|
const data = caches.get(key);
|
|
return data ? JSON.parse(data) : null;
|
|
};
|
|
|
|
export default cache;
|