@melchyore/adonis-cache
Version:
Cache package for AdonisJS V5
38 lines (37 loc) • 1.59 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const crypto_1 = __importDefault(require("crypto"));
const TaggedCache_1 = __importDefault(require("./TaggedCache"));
class RedisTaggedCache extends TaggedCache_1.default {
/*constructor (_store: CacheStoreContract, _tags: TagSet) {
super(_store, _tags)
}*/
async put(key, value, ttl) {
this.pushStandardKeys(await this._tags.getNamespace(), key);
return await super.put(key, value, ttl);
}
async forever(key, value) {
this.pushStandardKeys(await this._tags.getNamespace(), key);
return await super.forever(key, value);
}
pushStandardKeys(namespace, key) {
this.pushKeys(namespace, key, RedisTaggedCache.REFERENCE_KEY_STANDARD);
}
async pushKeys(namespace, key, reference) {
const fullKey = this.store.prefix + crypto_1.default.createHash('sha1').update(namespace).digest('hex') + ':' + key;
for (const segment of namespace.split('|')) {
await this._store
.connection()
.sadd(this.referenceKey(segment, reference), fullKey);
}
}
referenceKey(segment, suffix) {
return this.store.prefix + segment + ':' + suffix;
}
}
exports.default = RedisTaggedCache;
RedisTaggedCache.REFERENCE_KEY_FOREVER = 'forever_ref';
RedisTaggedCache.REFERENCE_KEY_STANDARD = 'standard_ref';