Skip to content

Commit e6006e8

Browse files
f0sterArthur Cinader
authored and
Arthur Cinader
committed
adding TTL option for redis cache adapter (#3397)
* adding TTL option for redis cache adapter * adding test for RedisCacheAdapter * Fixing adapater var name * changing timeout * updating default time * Fix the redis cache spec to construct the cache with the anticipated ttl make timeout values really really small so our test run fast :).
1 parent a45d184 commit e6006e8

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

spec/RedisCacheAdapter.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var RedisCacheAdapter = require('../src/Adapters/Cache/RedisCacheAdapter').default;
2+
3+
describe('RedisCacheAdapter', function() {
4+
var KEY = 'hello';
5+
var VALUE = 'world';
6+
7+
function wait(sleep) {
8+
return new Promise(function(resolve) {
9+
setTimeout(resolve, sleep);
10+
})
11+
}
12+
13+
it('should get/set/clear', (done) => {
14+
var cache = new RedisCacheAdapter({
15+
ttl: NaN
16+
});
17+
18+
cache.put(KEY, VALUE)
19+
.then(() => cache.get(KEY))
20+
.then((value) => expect(value).toEqual(VALUE))
21+
.then(() => cache.clear())
22+
.then(() => cache.get(KEY))
23+
.then((value) => expect(value).toEqual(null))
24+
.then(done);
25+
});
26+
27+
it('should expire after ttl', (done) => {
28+
var cache = new RedisCacheAdapter(null, 1);
29+
30+
cache.put(KEY, VALUE)
31+
.then(() => cache.get(KEY))
32+
.then((value) => expect(value).toEqual(VALUE))
33+
.then(wait.bind(null, 2))
34+
.then(() => cache.get(KEY))
35+
.then((value) => expect(value).toEqual(null))
36+
.then(done);
37+
});
38+
39+
it('should find un-expired records', (done) => {
40+
var cache = new RedisCacheAdapter(null, 5);
41+
42+
cache.put(KEY, VALUE)
43+
.then(() => cache.get(KEY))
44+
.then((value) => expect(value).toEqual(VALUE))
45+
.then(wait.bind(null, 1))
46+
.then(() => cache.get(KEY))
47+
.then((value) => expect(value).not.toEqual(null))
48+
.then(done);
49+
});
50+
});

src/Adapters/Cache/RedisCacheAdapter.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ function debug() {
99

1010
export class RedisCacheAdapter {
1111

12-
constructor(ctx) {
13-
this.client = redis.createClient(ctx);
12+
constructor(redisCtx, ttl = DEFAULT_REDIS_TTL) {
13+
this.client = redis.createClient(redisCtx);
1414
this.p = Promise.resolve();
15+
this.ttl = ttl;
1516
}
1617

1718
get(key) {
@@ -30,7 +31,7 @@ export class RedisCacheAdapter {
3031
return this.p;
3132
}
3233

33-
put(key, value, ttl = DEFAULT_REDIS_TTL) {
34+
put(key, value, ttl = this.ttl) {
3435
value = JSON.stringify(value);
3536
debug('put', key, value, ttl);
3637
if (ttl === 0) {

0 commit comments

Comments
 (0)