UNPKG

@splitsoftware/splitio-commons

Version:
45 lines (44 loc) 2.2 kB
import { __spreadArray } from "tslib"; import { impressionsToJSON } from '../utils'; var IMPRESSIONS_TTL_REFRESH = 3600; // 1 hr var ImpressionsCacheInRedis = /** @class */ (function () { function ImpressionsCacheInRedis(log, key, redis, metadata) { this.log = log; this.key = key; this.redis = redis; this.metadata = metadata; } ImpressionsCacheInRedis.prototype.track = function (impressions) { var _a; var _this = this; return (_a = this.redis).rpush.apply(_a, __spreadArray([this.key], impressionsToJSON(impressions, this.metadata), false)).then(function (queuedCount) { // If this is the creation of the key on Redis, set the expiration for it in 1hr. if (queuedCount === impressions.length) { return _this.redis.expire(_this.key, IMPRESSIONS_TTL_REFRESH); } }); }; ImpressionsCacheInRedis.prototype.count = function () { return this.redis.llen(this.key).catch(function () { return 0; }); }; ImpressionsCacheInRedis.prototype.drop = function (count) { if (!count) return this.redis.del(this.key); return this.redis.ltrim(this.key, count, -1); }; ImpressionsCacheInRedis.prototype.popNWithMetadata = function (count) { var _this = this; return this.redis.lrange(this.key, 0, count - 1).then(function (items) { return _this.redis.ltrim(_this.key, items.length, -1).then(function () { // This operation will simply do nothing if the key no longer exists (queue is empty) // It's only done in the "successful" exit path so that the TTL is not overriden if impressons weren't // popped correctly. This will result in impressions getting lost but will prevent the queue from taking // a huge amount of memory. _this.redis.expire(_this.key, IMPRESSIONS_TTL_REFRESH).catch(function () { }); // noop catch handler return items.map(function (item) { return JSON.parse(item); }); }); }); }; return ImpressionsCacheInRedis; }()); export { ImpressionsCacheInRedis };