-
Notifications
You must be signed in to change notification settings - Fork 111
/
redisCache.js
81 lines (80 loc) · 2.26 KB
/
redisCache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function Cache() {
}
try {
var redis = require("redis");
var cli = process.communal.redisCli
var c = {
port: cli.port,
host: cli.host,
options: {
timeout: cli.timeout || 3000,
password: cli.password || null
},
password: cli.password || "",
user: cli.user || "",
version: cli.version || "3.0",
db: cli.db || "0"
}
} catch (e) {
try {
var config = require("../config/config");
var c = config.redis
} catch (ee) {
}
}
try {
if (cli.version == "4.0") {
let client = redis.createClient({
url: `redis://${c.user}:${c.password}@${c.host}:${c.port}/${c.db}`
});
client.on("error", function(err) {
console.log(err);
});
Cache.connect = async function() {
await client.connect();
}
Cache.set = async function(key, value) {
return await client.set(key, JSON.stringify(value));
};
Cache.expire = async function(key, time) {
return await client.expire(key, time);
};
Cache.get = async (key) => {
return process.communal.jsonParse(await client.get(key));
};
Cache.close = async function() {
return await client.quit();
}
}
else {
var client = redis.createClient(c.port, c.host, c.options);
client.on("error", function(err) {
console.log(err);
});
Cache.connect = async function() {
await client.on("connect", function(res) {
});
}
Cache.set = async function(key, value) {
return client.set(key, JSON.stringify(value));
};
Cache.get = async (key) => {
let doc = await new Promise((resolve) => {
client.get(key, function(err, res) {
return resolve(res);
});
});
return process.communal.jsonParse(doc);
};
Cache.expire = function(key, time) {
return client.expire(key, time);
};
Cache.close = async function() {
return await client.quit();
}
}
} catch (e) {
Cache.connect = function() {
}
}
module.exports = Cache;