PC-vote/plugins/cache.server.js

34 lines
889 B
JavaScript
Raw Normal View History

2025-06-03 11:09:52 +08:00
// plugins/cache.server.js
import NodeCache from "node-cache";
// 创建全局缓存实例
const cache = new NodeCache({
stdTTL: 60, // 默认过期时间(秒)
checkperiod: 120, // 清理过期缓存的检查周期(秒)
maxKeys: 1000, // 最大缓存项数量超过时LRU淘汰
});
export default defineNuxtPlugin(() => {
return {
provide: {
cache: {
get: (key) => cache.get(key),
set: (key, value, ttl) => cache.set(key, value, ttl),
del: (key) => cache.del(key),
flush: () => cache.flushAll(),
},
},
};
});
// import NodeCache from 'node-cache';
// const cache = new NodeCache({ stdTTL: 3600 }); // 缓存有效期 1 小时
// export default defineNuxtPlugin((nuxtApp) => {
// return {
// provide: {
// cache
// }
// };
// });