mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-09-01 14:57:14 +08:00
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { LRUCache } from 'lru-cache';
|
|
import { config } from '@/config';
|
|
import type CacheModule from './base';
|
|
|
|
const status = { available: false };
|
|
const clients: {
|
|
memoryCache?: LRUCache<any, any>;
|
|
} = {};
|
|
|
|
export default {
|
|
init: () => {
|
|
clients.memoryCache = new LRUCache({
|
|
ttl: config.cache.routeExpire * 1000,
|
|
max: config.memory.max,
|
|
});
|
|
status.available = true;
|
|
},
|
|
get: (key: string, refresh = true) => {
|
|
if (key && status.available && clients.memoryCache) {
|
|
let value = clients.memoryCache.get(key, { updateAgeOnGet: refresh }) as string | undefined;
|
|
if (value) {
|
|
value = value + '';
|
|
}
|
|
return value;
|
|
} else {
|
|
return null;
|
|
}
|
|
},
|
|
set: (key, value, maxAge = config.cache.contentExpire) => {
|
|
if (!value || value === 'undefined') {
|
|
value = '';
|
|
}
|
|
if (typeof value === 'object') {
|
|
value = JSON.stringify(value);
|
|
}
|
|
if (key && status.available && clients.memoryCache) {
|
|
return clients.memoryCache.set(key, value, { ttl: maxAge * 1000 });
|
|
}
|
|
},
|
|
clients,
|
|
status,
|
|
} as CacheModule;
|