PC-vote/plugins/cache.server.js
2025-06-03 11:09:52 +08:00

34 lines
889 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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
// }
// };
// });