UNPKG

node-cache-redis

Version:

Simplistic node redis cache ready can scale with generic-pool support

385 lines (384 loc) 18.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var error_1 = require("./error"); var helpers_1 = require("./helpers"); var RedisStore_1 = __importDefault(require("./RedisStore")); var redisCache_1 = require("./redisCache"); describe('redisCache', function () { // test('throws NotInitialisedError when not init', () => { // expect(getName()).toThrow(NotInitialisedError) // }) var name = 'testCache'; var redisOptions = { host: process.env.REDIS_HOST || '127.0.0.1', auth_pass: process.env.REDIS_AUTH }; var poolOptions = { min: 2, max: 4 }; var options = { name: name, redisOptions: redisOptions, poolOptions: poolOptions }; redisCache_1.init(options); var key = 'chuck-norris'; var value = 'superman'; describe('getName', function () { test('get given name', function () { expect(redisCache_1.getName()).toBe(name); }); }); describe('getRedisOptions', function () { test('get given redis options', function () { expect(redisCache_1.getRedisOptions()).toBe(redisOptions); }); }); describe('getPoolOptions', function () { test('get given pool options', function () { expect(redisCache_1.getPoolOptions()).toBe(poolOptions); }); }); describe('getDefaultTtlInS', function () { test('returns undefined when not set', function () { expect(redisCache_1.getDefaultTtlInS()).toBeUndefined(); }); test('get defaultTtlInS', function () { expect(redisCache_1.setDefaultTtlInS(2)).toBe(2); expect(redisCache_1.getDefaultTtlInS()).toBe(2); }); }); describe('setDefaultTtlInS', function () { test('set defaultTtlInS option', function () { expect(redisCache_1.setDefaultTtlInS(2)).toBe(2); expect(redisCache_1.getDefaultTtlInS()).toBe(2); }); }); describe('unsetDefaultTtlInS', function () { test('unset defaultTtlInS option', function () { expect(redisCache_1.unsetDefaultTtlInS()).toBe(true); expect(redisCache_1.getDefaultTtlInS()).toBeUndefined(); }); }); describe('getStore', function () { test('returns store', function () { expect(redisCache_1.getStore()).toBeInstanceOf(RedisStore_1.default); }); }); describe('status', function () { test('get store stats', function () { var _a = redisCache_1.getStatus(), statusName = _a.name, size = _a.size, available = _a.available, pending = _a.pending; expect(statusName).toBe(name); expect(size).toBe(poolOptions.min); expect(available).toBe(0); expect(pending).toBe(0); }); }); describe('set', function () { beforeEach(function () { return redisCache_1.deleteAll(); }); // below are incorrect test('set value without expiry if ttl is not provided', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, redisCache_1.set(key, value)]; case 1: _a.sent(); return [4 /*yield*/, expect(redisCache_1.get(key)).resolves.toBe(value)]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }); test('set value with expiry if ttl is provided', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, redisCache_1.set(key, value, 1)]; case 1: _a.sent(); return [4 /*yield*/, expect(redisCache_1.get(key)).resolves.toBe(value)]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }); }); describe('get', function () { beforeEach(function () { return redisCache_1.deleteAll(); }); test('get the existing key', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, redisCache_1.set(key, value)]; case 1: _a.sent(); return [4 /*yield*/, expect(redisCache_1.get(key)).resolves.toBe(value)]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }); test('not get the non-existing key', function () { return expect(redisCache_1.get('nonExistingKey')).resolves.toBeNull(); }); }); describe('getset', function () { beforeEach(function () { return redisCache_1.deleteAll(); }); test('get the existing key which is NULL', function () { return expect(redisCache_1.getset(key, value, 60)).resolves.toBeNull(); }); test('get previous value set', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, redisCache_1.set(key, value)]; case 1: _a.sent(); return [4 /*yield*/, expect(redisCache_1.getset(key, value)).resolves.toBe(value)]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }); }); describe('wrap', function () { var newValue = 'test'; var fn = function () { return newValue; }; var fnToFail = function () { throw new Error('not be called'); }; var defaultTtlInS = 10; beforeAll(function () { return redisCache_1.deleteAll(); }); afterAll(function () { return redisCache_1.unsetDefaultTtlInS(); }); afterEach(function () { return redisCache_1.setDefaultTtlInS(defaultTtlInS); }); test("set if key doesn't exist", function () { return __awaiter(void 0, void 0, void 0, function () { var localKey, result; return __generator(this, function (_a) { switch (_a.label) { case 0: localKey = helpers_1.genRandomStr(); return [4 /*yield*/, redisCache_1.wrap(localKey, fn, { ttlInSeconds: 5000 })]; case 1: result = _a.sent(); expect(result).toBe(newValue); return [4 /*yield*/, expect(redisCache_1.get(localKey)).resolves.toBe(newValue)]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }); test('get if key exists', function () { return __awaiter(void 0, void 0, void 0, function () { var localKey; return __generator(this, function (_a) { switch (_a.label) { case 0: localKey = helpers_1.genRandomStr(); return [4 /*yield*/, redisCache_1.set(localKey, newValue)]; case 1: _a.sent(); return [4 /*yield*/, expect(redisCache_1.wrap(localKey, fnToFail)).resolves.toBe(newValue)]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }); test('throw InvalidTtlError when ttlInSeconds=0', function () { return __awaiter(void 0, void 0, void 0, function () { var localKey; return __generator(this, function (_a) { switch (_a.label) { case 0: localKey = helpers_1.genRandomStr(); return [4 /*yield*/, expect(redisCache_1.wrap(localKey, fn, { ttlInSeconds: 0 })).rejects.toThrow(error_1.InvalidTtlError)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }); test('throw InvalidTtlError when ttlInSeconds < 0', function () { return __awaiter(void 0, void 0, void 0, function () { var localKey; return __generator(this, function (_a) { switch (_a.label) { case 0: localKey = helpers_1.genRandomStr(); return [4 /*yield*/, expect(redisCache_1.wrap(localKey, fn, { ttlInSeconds: -1 })).rejects.toThrow(error_1.InvalidTtlError)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }); test('throw InvalidTtlError when ttlInSeconds is invalid', function () { return __awaiter(void 0, void 0, void 0, function () { var localKey; return __generator(this, function (_a) { switch (_a.label) { case 0: localKey = helpers_1.genRandomStr(); return [4 /*yield*/, expect(redisCache_1.wrap(localKey, fn, { ttlInSeconds: NaN })).rejects.toThrow(error_1.InvalidTtlError)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }); }); describe('keys', function () { var keyValues = { key1: 'value1', 'test:key2': 'value2' }; beforeAll(function () { return redisCache_1.deleteAll(); }); beforeEach(function () { // @ts-ignore return Promise.all(Object.keys(keyValues).map(function (k) { return redisCache_1.set(k, keyValues[k]); })); }); test('return all the keys', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, expect(redisCache_1.keys()).resolves.toEqual(expect.arrayContaining(Object.keys(keyValues)))]; }); }); }); test('not return keys not matching pattern', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, expect(redisCache_1.keys('test:*')).resolves.toEqual(expect.not.arrayContaining(['key1']))]; }); }); }); test('return keys matches pattern', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, expect(redisCache_1.keys('test:*')).resolves.toEqual(expect.arrayContaining(['test:key2']))]; }); }); }); }); describe('del', function () { var keyValues = { key1: 'value1', key2: 'value2' }; beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, redisCache_1.deleteAll() // @ts-ignore ]; case 1: _a.sent(); // @ts-ignore return [4 /*yield*/, Promise.all(Object.keys(keyValues).map(function (k) { return redisCache_1.set(k, keyValues[k]); }))]; case 2: // @ts-ignore _a.sent(); return [2 /*return*/]; } }); }); }); test('delete keys array', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, redisCache_1.del(Object.keys(keyValues))]; case 1: _a.sent(); return [4 /*yield*/, expect(redisCache_1.keys()).resolves.toHaveLength(0)]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }); test('deletes the key', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, redisCache_1.del(['key1'])]; case 1: _a.sent(); return [4 /*yield*/, expect(redisCache_1.keys()).resolves.toEqual(expect.arrayContaining(['key2']))]; case 2: _a.sent(); return [4 /*yield*/, expect(redisCache_1.keys()).resolves.toEqual(expect.not.arrayContaining(['key1']))]; case 3: _a.sent(); return [2 /*return*/]; } }); }); }); test('throw for an empty array', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, expect(redisCache_1.del([])).rejects.toThrow()]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }); }); describe('deleteAll', function () { var keyValues = { key1: 'value1', key2: 'value2' }; beforeEach(function () { // @ts-ignore return Promise.all(Object.keys(keyValues).map(function (k) { return redisCache_1.set(k, keyValues[k]); })); }); test('delete all the keys', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, redisCache_1.deleteAll()]; case 1: _a.sent(); return [4 /*yield*/, expect(redisCache_1.keys()).resolves.toHaveLength(0)]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }); test('delete all the keys with pattern', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, redisCache_1.deleteAll('key1*')]; case 1: _a.sent(); return [4 /*yield*/, expect(redisCache_1.keys()).resolves.toEqual(expect.arrayContaining(['key2']))]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }); }); });