redis.js
1.0 KB
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
/*
* @Descripttion:
* @version: 0.0.0
* @Author: genglw
* @Date: 2022-02-12 20:34:11
* @LastEditors: genglw
* @LastEditTime: 2022-02-15 11:09:45
*/
var redis = require('redis');
exports = module.exports = function (logger, settings) {
async function set(key, value) {
const client = redis.createClient({
url: `redis://${settings.redis.host}:${settings.redis.port}`,
password: settings.redis.pass,
});
client.on('error', (err) => logger.info('Redis Client Error', err));
await client.connect();
await client.set(key, value);
await client.quit();
}
async function get(key) {
const client = redis.createClient({
url: `redis://${settings.redis.host}:${settings.redis.port}`,
password: settings.redis.pass,
});
client.on('error', (err) => logger.info('Redis Client Error', err));
await client.connect();
const value = await client.get(key);
await client.quit();
return value;
}
return {
set: set,
get: get,
};
};
exports['@singleton'] = true;
exports['@require'] = ['igloo/logger', 'igloo/settings'];