server/services/redis/index.js
Artem Anufrij 408b6c3dc5
All checks were successful
continuous-integration/drone/push Build is passing
finish share api
2023-02-15 23:36:19 +01:00

45 lines
1020 B
JavaScript

const { createClient } = require('redis');
const server = require("../../server");
const config = server.config;
const redisUrl = "redis://" + config.redis.host + ":" + config.redis.port
const client = createClient({
url: redisUrl
});
client.on('error', (err) => console.log('Redis Client Error', err));
client.connect();
client.flushAll();
const expire = 57600; // 24h
exports.set = function (key, value) {
if (value) {
client.set(key, JSON.stringify(value));
client.expire(key, expire);
}
}
exports.get = function (key, callback) {
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();
}
});
}
exports.flushAll = function () {
client.flushAll();
process.stdout.write("services/redis flushAll()\n");
}