Skip to content

Commit 8f28d53

Browse files
committed
Allow options to be passed to del method
1 parent 3e56e5a commit 8f28d53

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

dist/index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,11 @@ var buildRedisStoreWithConfig = function buildRedisStoreWithConfig(redisCache, c
528528
while (1) {
529529
switch (_context3.prev = _context3.next) {
530530
case 0:
531+
if (isObject(args.at(-1))) {
532+
args.pop();
533+
}
531534
return _context3.abrupt("return", redisCache.del(args));
532-
case 1:
535+
case 3:
533536
case "end":
534537
return _context3.stop();
535538
}
@@ -542,16 +545,16 @@ var buildRedisStoreWithConfig = function buildRedisStoreWithConfig(redisCache, c
542545
}();
543546
var _mset = /*#__PURE__*/function () {
544547
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(args) {
545-
var _options, _options2;
546548
var options, ttl, items, multi, _iterator, _step, kv, _kv, key, value;
547549
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
548550
while (1) {
549551
switch (_context4.prev = _context4.next) {
550552
case 0:
553+
options = {};
551554
if (isObject(args.at(-1))) {
552555
options = args.pop();
553556
}
554-
ttl = (_options = options) !== null && _options !== void 0 && _options.ttl || ((_options2 = options) === null || _options2 === void 0 ? void 0 : _options2.ttl) === 0 ? options.ttl : config.ttl; // Zips even and odd array items into tuples
557+
ttl = options.ttl || options.ttl === 0 ? options.ttl : config.ttl; // Zips even and odd array items into tuples
555558
items = args.map(function (key, index) {
556559
if (index % 2 !== 0) return null;
557560
var value = args[index + 1];
@@ -563,7 +566,7 @@ var buildRedisStoreWithConfig = function buildRedisStoreWithConfig(redisCache, c
563566
return key !== null;
564567
});
565568
if (!ttl) {
566-
_context4.next = 10;
569+
_context4.next = 11;
567570
break;
568571
}
569572
multi = redisCache.multi();
@@ -580,9 +583,9 @@ var buildRedisStoreWithConfig = function buildRedisStoreWithConfig(redisCache, c
580583
_iterator.f();
581584
}
582585
return _context4.abrupt("return", multi.exec());
583-
case 10:
584-
return _context4.abrupt("return", redisCache.mSet(items));
585586
case 11:
587+
return _context4.abrupt("return", redisCache.mSet(items));
588+
case 12:
586589
case "end":
587590
return _context4.stop();
588591
}
@@ -643,11 +646,14 @@ var buildRedisStoreWithConfig = function buildRedisStoreWithConfig(redisCache, c
643646
for (_len2 = _args6.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
644647
args[_key2] = _args6[_key2];
645648
}
649+
if (isObject(args.at(-1))) {
650+
args.pop();
651+
}
646652
if (Array.isArray(args)) {
647653
args = args.flat();
648654
}
649655
return _context6.abrupt("return", redisCache.del(args));
650-
case 3:
656+
case 5:
651657
case "end":
652658
return _context6.stop();
653659
}

index.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ const buildRedisStoreWithConfig = (redisCache, config) => {
1919
const ttl = (options?.ttl || options?.ttl === 0) ? options.ttl : config.ttl;
2020

2121
if (ttl) {
22-
return redisCache.setEx(key, ttl, getValue(value));
22+
return redisCache.setEx(key, ttl, encodeValue(value));
2323
} else {
24-
return redisCache.set(key, getValue(value));
24+
return redisCache.set(key, encodeValue(value));
2525
}
2626
};
2727
const get = async (key, options) => {
@@ -30,17 +30,21 @@ const buildRedisStoreWithConfig = (redisCache, config) => {
3030
if (val === null) {
3131
return null;
3232
}
33-
return options.parse !== false ? JSON.parse(val) : val;
33+
return options.parse !== false ? decodeValue(val) : val;
3434
};
3535
const del = async (args) => {
36+
let options = {};
37+
if (isObject(args.at(-1))) {
38+
options = args.pop();
39+
}
3640
return redisCache.del(args);
3741
};
3842
const mset = async (args) => {
39-
let options;
43+
let options = {};
4044
if (isObject(args.at(-1))) {
4145
options = args.pop();
4246
}
43-
const ttl = (options?.ttl || options?.ttl === 0) ? options.ttl : config.ttl;
47+
const ttl = (options.ttl || options.ttl === 0) ? options.ttl : config.ttl;
4448

4549
// Zips even and odd array items into tuples
4650
const items = args
@@ -50,7 +54,7 @@ const buildRedisStoreWithConfig = (redisCache, config) => {
5054
if (!isCacheableValue(value)) {
5155
throw new Error(`"${value}" is not a cacheable value`);
5256
}
53-
return [key, getValue(value)];
57+
return [key, encodeValue(value)];
5458
})
5559
.filter((key) => key !== null);
5660

@@ -78,11 +82,15 @@ const buildRedisStoreWithConfig = (redisCache, config) => {
7882
return null;
7983
}
8084

81-
return options.parse !== false ? JSON.parse(val) : val;
85+
return options.parse !== false ? decodeValue(val) : val;
8286
}),
8387
);
8488
};
8589
const mdel = async (...args) => {
90+
let options = {};
91+
if (isObject(args.at(-1))) {
92+
options = args.pop();
93+
}
8694
if (Array.isArray(args)) {
8795
args = args.flat();
8896
}
@@ -184,10 +192,14 @@ const buildRedisStoreWithConfig = (redisCache, config) => {
184192
};
185193
};
186194

187-
function getValue(value) {
195+
function encodeValue(value) {
188196
return JSON.stringify(value) || '"undefined"';
189197
}
190198

199+
function decodeValue(val) {
200+
return JSON.parse(val);
201+
}
202+
191203
function isObject(object) {
192204
return typeof object === 'object'
193205
&& !Array.isArray(object)

test/index.test.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const config = {
1717
beforeEach(async () => {
1818
redisCache = cacheManager.caching({
1919
store: await redisStore(config),
20-
...config,
2120
});
2221
await redisCache.reset();
2322

@@ -35,7 +34,6 @@ beforeEach(async () => {
3534

3635
customRedisCache = cacheManager.caching({
3736
store: await redisStore(customConfig),
38-
...customConfig,
3937
});
4038
await customRedisCache.reset();
4139
});
@@ -174,6 +172,13 @@ describe('del', () => {
174172
await expect(redisCache.get('foo')).resolves.toEqual(null);
175173
});
176174

175+
it('should delete a value for a given key if options provided', async () => {
176+
await redisCache.set('foo', 'bar');
177+
await expect(redisCache.get('foo')).resolves.toEqual('bar');
178+
await redisCache.del('foo', {});
179+
await expect(redisCache.get('foo')).resolves.toEqual(null);
180+
});
181+
177182
it('should return an error if there is an error acquiring a connection', async () => {
178183
await redisCache.store.getClient().disconnect();
179184
await expect(redisCache.del('foo')).rejects.toThrowError('The client is closed');
@@ -328,13 +333,27 @@ describe('mdel', () => {
328333
await expect(redisCache.mget('foo', 'foo2')).resolves.toEqual([null, null]);
329334
});
330335

336+
it('should delete a unlimited number of keys if options provided', async () => {
337+
await redisCache.mset('foo', 'bar', 'foo2', 'bar2');
338+
await expect(redisCache.mget('foo', 'foo2')).resolves.toEqual(['bar', 'bar2']);
339+
await redisCache.store.mdel('foo', 'foo2', {});
340+
await expect(redisCache.mget('foo', 'foo2')).resolves.toEqual([null, null]);
341+
});
342+
331343
it('should delete an array of keys', async () => {
332344
await redisCache.mset('foo', 'bar', 'foo2', 'bar2');
333345
await expect(redisCache.mget('foo', 'foo2')).resolves.toEqual(['bar', 'bar2']);
334346
await redisCache.store.mdel(['foo', 'foo2']);
335347
await expect(redisCache.mget('foo', 'foo2')).resolves.toEqual([null, null]);
336348
});
337349

350+
it('should delete an array of keys if options provided', async () => {
351+
await redisCache.mset('foo', 'bar', 'foo2', 'bar2');
352+
await expect(redisCache.mget('foo', 'foo2')).resolves.toEqual(['bar', 'bar2']);
353+
await redisCache.store.mdel(['foo', 'foo2'], {});
354+
await expect(redisCache.mget('foo', 'foo2')).resolves.toEqual([null, null]);
355+
});
356+
338357
it('should return an error if there is an error acquiring a connection', async () => {
339358
await redisCache.store.getClient().disconnect();
340359
await expect(redisCache.store.mdel('foo')).rejects.toThrowError('The client is closed');

0 commit comments

Comments
 (0)