This commit is contained in:
张成
2025-11-21 16:53:49 +08:00
commit 8309808835
286 changed files with 32656 additions and 0 deletions

50
api/service/redis.js Normal file
View File

@@ -0,0 +1,50 @@
const redis = require("redis");
const config = require("../../config/config");
let rclient = null;
module.exports = {
getConfig() {
let { port, host, opt } = config.redis || {}
return { port, host, opt }
},
init() {
let { port, host, opt } = this.getConfig()
if (host && port) {
rclient = redis.createClient(port, host, opt);
rclient.auth(config.pwd, function () {
console.log("redis通过认证");
});
rclient.on("connect", function () {
console.log("redis连接成功");
});
}
},
// 设置key值
set(key, value, expire = 0) {
if (!rclient) {
this.init();
}
rclient.set(key, value); //赋值
if (expire) {
rclient.expire(key, expire); //60秒自动过期
}
},
get(key) {
if (!rclient) {
this.init();
}
return new Promise((resolve, reject) => {
rclient.get(key, function (err, data) {
resolve(data);
});
});
},
};