first commit
This commit is contained in:
1
node+vue/shop-bend/cache/.pydio
vendored
Normal file
1
node+vue/shop-bend/cache/.pydio
vendored
Normal file
@@ -0,0 +1 @@
|
||||
c4e4d830-303d-4c00-bb1f-c77d83c90ba3
|
||||
37
node+vue/shop-bend/cache/index.js
vendored
Executable file
37
node+vue/shop-bend/cache/index.js
vendored
Executable file
@@ -0,0 +1,37 @@
|
||||
const redis = require("redis");
|
||||
const config = require("../config");
|
||||
|
||||
class Redis {
|
||||
constructor() {
|
||||
// 创建redis服务
|
||||
this.client = redis.createClient(config.dev.redis.port, config.dev.redis.host);
|
||||
// 监听错误
|
||||
this.client.on("error", function (err) {
|
||||
console.log("redis 连接失败:" + err);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 设置缓存值
|
||||
* @param {*string} key 名称
|
||||
* @param {*string} value 数值
|
||||
*/
|
||||
Set(key, value) {
|
||||
this.client.set(key, JSON.stringify(value), redis.print)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存数值
|
||||
* @param {*string} key 名称
|
||||
*/
|
||||
Get(key) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.client.expire(key, config.dev.redis.timeout);
|
||||
this.client.get(key, function (error, result) {
|
||||
if (error) reject(error)
|
||||
resolve(JSON.parse(result))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new Redis()
|
||||
Reference in New Issue
Block a user