AISR / backend /cache /index.ts
zhzabcd's picture
Upload 101 files
755dd12 verified
raw
history blame contribute delete
946 Bytes
import { createCache, memoryStore } from 'cache-manager';
import { createHash } from 'node:crypto';
import { TMode } from '../interface';
import { ESearXNGCategory } from '../search/searxng';
const memoryCache = createCache(memoryStore(), {
ttl: 60 * 1000 * 60 * 24 // ms
});
export async function setToCache(key: string, val: string, mode: TMode = 'simple', categories: ESearXNGCategory[] = []) {
const hash = createHash('sha256');
const str = `${key}_${mode}_${categories.join()}`;
const hashKey = hash.update(str, 'utf8').digest('hex');
await memoryCache.set(hashKey, val);
}
export async function getFromCache(q: string, mode: TMode = 'simple', categories: ESearXNGCategory[] = []) {
const hash = createHash('sha256');
const str = `${q}_${mode}_${categories.join()}`;
const hashKey = hash.update(str, 'utf8').digest('hex');
const val = await memoryCache.get<string>(hashKey);
return val;
}
export default memoryCache;