UNPKG

@splitsoftware/splitio-commons

Version:
58 lines (57 loc) 2.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SegmentsCacheInRedis = void 0; var lang_1 = require("../../utils/lang"); var constants_1 = require("../inLocalStorage/constants"); var SegmentsCacheInRedis = /** @class */ (function () { function SegmentsCacheInRedis(log, keys, redis) { this.log = log; this.redis = redis; this.keys = keys; } /** * Update the given segment `name` with the lists of `addedKeys`, `removedKeys` and `changeNumber`. * The returned promise is resolved if the operation success, with `true` if the segment was updated (i.e., some key was added or removed), * or rejected if it fails (e.g., Redis operation fails). */ SegmentsCacheInRedis.prototype.update = function (name, addedKeys, removedKeys, changeNumber) { var segmentKey = this.keys.buildSegmentNameKey(name); return Promise.all([ addedKeys.length && this.redis.sadd(segmentKey, addedKeys), removedKeys.length && this.redis.srem(segmentKey, removedKeys), this.redis.set(this.keys.buildSegmentTillKey(name), changeNumber + '') ]).then(function () { return addedKeys.length > 0 || removedKeys.length > 0; }); }; SegmentsCacheInRedis.prototype.isInSegment = function (name, key) { return this.redis.sismember(this.keys.buildSegmentNameKey(name), key).then(function (matches) { return matches !== 0; }); }; SegmentsCacheInRedis.prototype.getChangeNumber = function (name) { var _this = this; return this.redis.get(this.keys.buildSegmentTillKey(name)).then(function (value) { var i = parseInt(value, 10); return (0, lang_1.isNaNNumber)(i) ? undefined : i; }).catch(function (e) { _this.log.error(constants_1.LOG_PREFIX + 'Could not retrieve changeNumber from segments storage. Error: ' + e); return undefined; }); }; SegmentsCacheInRedis.prototype.registerSegments = function (segments) { if (segments.length) { return this.redis.sadd(this.keys.buildRegisteredSegmentsKey(), segments).then(function () { return true; }); } else { return Promise.resolve(true); } }; SegmentsCacheInRedis.prototype.getRegisteredSegments = function () { return this.redis.smembers(this.keys.buildRegisteredSegmentsKey()); }; // @TODO remove or implement. It is not being used. SegmentsCacheInRedis.prototype.clear = function () { return Promise.resolve(); }; return SegmentsCacheInRedis; }()); exports.SegmentsCacheInRedis = SegmentsCacheInRedis;