Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 27a97c9

Browse files
author
Federico De Giuli
committed
feat: expose cache.unset
1 parent d3b18b0 commit 27a97c9

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,8 @@ When multiple `getOrElse` calls concurrently encounter the same stale value, it
202202
This is done on a per-instance level, so if you create many cache instances reading and writing the same keys, you are asking for trouble.
203203
If you don't, the worst case is every process in your system fetching the value at once.
204204
Which should be a smaller number than the number of concurrent requests in most cases.
205+
206+
### Cache.unset(key, cb) -> Promise
207+
208+
Cache delete operation.
209+
`key` has to be a string.

lib/cache.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,14 @@ Cache.prototype.getOrElse = function _getOrElse(rawKey, val, _opts, _cb) {
127127
return getOrElse(this, key, val, optsWithDefaults).nodeify(args.cb);
128128
};
129129

130+
Cache.prototype.unset = function unset(rawKey, cb) {
131+
var key = this.applyPrefix(rawKey);
132+
133+
return this._unset(key).nodeify(args.cb);
134+
};
135+
136+
Cache.prototype._unset = function _unset(key) {
137+
return this._applyTimeout(this.backend.unset(key));
138+
};
139+
130140
module.exports = Cache;

test/get-set.test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Bluebird from 'bluebird';
33

44
import withBackends from './_backends';
55

6-
describe('Cache::{get,set}', () => {
6+
describe('Cache::{get,set,unset}', () => {
77
withBackends(cache => {
88
it('get/set (callback style)', done => {
99
cache.set('callback-key', 'callback-value', setError => {
@@ -26,6 +26,32 @@ describe('Cache::{get,set}', () => {
2626
assert.equal('promise-value', await cache.get('promise-key'));
2727
});
2828

29+
30+
it('set/unset (callback style', done => {
31+
cache.set('callback-key', 'callback-value', setError => {
32+
if (setError) return done(setError);
33+
cache.unset('callback-key', unsetError => {
34+
if (unsetError) return done(unsetError);
35+
cache.get('callback-key', (getError, value) => {
36+
if (getError) return done(getError);
37+
let assertError = null;
38+
try {
39+
assert.equal(null, value);
40+
} catch (error) {
41+
assertError = error;
42+
}
43+
done(assertError);
44+
});
45+
});
46+
});
47+
});
48+
49+
it('set/unset (promise style)', async () => {
50+
await cache.set('promise-key', 'promise-value', { expire: 1 });
51+
await cache.unset('promise-key');
52+
assert.equal(null, await cache.get('promise-key'));
53+
});
54+
2955
it('honors expires', async () => {
3056
const values = {
3157
key1: 'Value 1',

0 commit comments

Comments
 (0)