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 :).
This commit is contained in:
committed by
Arthur Cinader
parent
a45d1848db
commit
e6006e800e
50
spec/RedisCacheAdapter.spec.js
Normal file
50
spec/RedisCacheAdapter.spec.js
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
var RedisCacheAdapter = require('../src/Adapters/Cache/RedisCacheAdapter').default;
|
||||||
|
|
||||||
|
describe('RedisCacheAdapter', function() {
|
||||||
|
var KEY = 'hello';
|
||||||
|
var VALUE = 'world';
|
||||||
|
|
||||||
|
function wait(sleep) {
|
||||||
|
return new Promise(function(resolve) {
|
||||||
|
setTimeout(resolve, sleep);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
it('should get/set/clear', (done) => {
|
||||||
|
var cache = new RedisCacheAdapter({
|
||||||
|
ttl: NaN
|
||||||
|
});
|
||||||
|
|
||||||
|
cache.put(KEY, VALUE)
|
||||||
|
.then(() => cache.get(KEY))
|
||||||
|
.then((value) => expect(value).toEqual(VALUE))
|
||||||
|
.then(() => cache.clear())
|
||||||
|
.then(() => cache.get(KEY))
|
||||||
|
.then((value) => expect(value).toEqual(null))
|
||||||
|
.then(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should expire after ttl', (done) => {
|
||||||
|
var cache = new RedisCacheAdapter(null, 1);
|
||||||
|
|
||||||
|
cache.put(KEY, VALUE)
|
||||||
|
.then(() => cache.get(KEY))
|
||||||
|
.then((value) => expect(value).toEqual(VALUE))
|
||||||
|
.then(wait.bind(null, 2))
|
||||||
|
.then(() => cache.get(KEY))
|
||||||
|
.then((value) => expect(value).toEqual(null))
|
||||||
|
.then(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should find un-expired records', (done) => {
|
||||||
|
var cache = new RedisCacheAdapter(null, 5);
|
||||||
|
|
||||||
|
cache.put(KEY, VALUE)
|
||||||
|
.then(() => cache.get(KEY))
|
||||||
|
.then((value) => expect(value).toEqual(VALUE))
|
||||||
|
.then(wait.bind(null, 1))
|
||||||
|
.then(() => cache.get(KEY))
|
||||||
|
.then((value) => expect(value).not.toEqual(null))
|
||||||
|
.then(done);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -9,9 +9,10 @@ function debug() {
|
|||||||
|
|
||||||
export class RedisCacheAdapter {
|
export class RedisCacheAdapter {
|
||||||
|
|
||||||
constructor(ctx) {
|
constructor(redisCtx, ttl = DEFAULT_REDIS_TTL) {
|
||||||
this.client = redis.createClient(ctx);
|
this.client = redis.createClient(redisCtx);
|
||||||
this.p = Promise.resolve();
|
this.p = Promise.resolve();
|
||||||
|
this.ttl = ttl;
|
||||||
}
|
}
|
||||||
|
|
||||||
get(key) {
|
get(key) {
|
||||||
@@ -30,7 +31,7 @@ export class RedisCacheAdapter {
|
|||||||
return this.p;
|
return this.p;
|
||||||
}
|
}
|
||||||
|
|
||||||
put(key, value, ttl = DEFAULT_REDIS_TTL) {
|
put(key, value, ttl = this.ttl) {
|
||||||
value = JSON.stringify(value);
|
value = JSON.stringify(value);
|
||||||
debug('put', key, value, ttl);
|
debug('put', key, value, ttl);
|
||||||
if (ttl === 0) {
|
if (ttl === 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user