File size: 946 Bytes
755dd12 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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;
|