DailyHotApi/utils/cacheData.js
2023-03-14 16:04:10 +08:00

42 lines
872 B
JavaScript
Raw Permalink 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.

const NodeCache = require("node-cache");
const cache = new NodeCache({
stdTTL: 1800, // 缓存默认过期时间(单位秒)
checkperiod: 60, // 定期检查过期缓存的时间(单位秒)
});
/**
* 从缓存中获取数据
* @param {string} key 缓存键值
* @return {Promise<any>} 数据
*/
const get = async (key) => {
return cache.get(key);
};
/**
* 将数据写入缓存
* @param {string} key 缓存键值
* @param {any} value 数据
* @param {number} ttl 有效期单位秒默认为300秒
* @return {Promise<void>} 无返回值
*/
const set = async (key, value, ttl = 300) => {
return cache.set(key, value, ttl);
};
/**
* 从缓存中删除数据
* @param {string} key 缓存键值
* @return {Promise<void>} 无返回值
*/
const del = async (key) => {
return cache.del(key);
};
module.exports = {
get,
set,
del,
};