server/services/redis/index.js

45 lines
1020 B
JavaScript
Raw Permalink Normal View History

2023-02-08 12:30:56 +01:00
const { createClient } = require('redis');
const server = require("../../server");
const config = server.config;
const redisUrl = "redis://" + config.redis.host + ":" + config.redis.port
const client = createClient({
2023-02-15 23:36:19 +01:00
url: redisUrl
2023-02-08 12:30:56 +01:00
});
client.on('error', (err) => console.log('Redis Client Error', err));
client.connect();
client.flushAll();
const expire = 57600; // 24h
exports.set = function (key, value) {
2023-02-15 23:36:19 +01:00
if (value) {
client.set(key, JSON.stringify(value));
client.expire(key, expire);
}
2023-02-08 12:30:56 +01:00
}
exports.get = function (key, callback) {
2023-02-15 23:36:19 +01:00
process.stdout.write("services/redis get '" + key + "'\n");
client.get(key).then(value => {
callback(JSON.parse(value));
});
client.expire(key, expire);
}
exports.del = function (key, callback) {
process.stdout.write("services/redis del '" + key + "'\n");
client.del(key).then(() => {
if (callback) {
callback();
}
});
2023-02-08 12:30:56 +01:00
}
exports.flushAll = function () {
2023-02-15 23:36:19 +01:00
client.flushAll();
process.stdout.write("services/redis flushAll()\n");
2023-02-08 12:30:56 +01:00
}