@dazejs/framework
Version:
Daze.js - A powerful web framework for Node.js
47 lines • 1.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisCacheStore = void 0;
const store_1 = require("./store");
const helpers_1 = require("../../../helpers");
class RedisCacheStore extends store_1.CacheStore {
constructor(connection = 'default') {
super();
this.redis = (0, helpers_1.redis)(connection);
}
async get(key, defaultVal) {
const value = await this.redis.get(key);
if (value) {
return value;
}
return defaultVal;
}
async set(key, value, seconds) {
await this.redis.setex(key, Math.max(seconds, 1), value);
return true;
}
async add(key, value, seconds) {
const lua = `return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])`;
const res = await this.redis.eval(lua, 1, key, value, Math.max(seconds, 1));
return !!res;
}
async increment(key, value = 1) {
return this.redis.incrby(key, value);
}
async decrement(key, value = 1) {
return this.redis.decrby(key, value);
}
async forever(key, value) {
await this.redis.set(key, value);
return true;
}
async remove(key) {
await this.redis.del(key);
return true;
}
async flush() {
await this.redis.flushdb();
return true;
}
}
exports.RedisCacheStore = RedisCacheStore;
//# sourceMappingURL=redis.js.map