From b671dbeec69eb75a637f48230a593076e2f1d067 Mon Sep 17 00:00:00 2001 From: f0ster Date: Wed, 18 Jan 2017 16:33:54 -0800 Subject: [PATCH 1/6] adding TTL option for redis cache adapter --- src/Adapters/Cache/RedisCacheAdapter.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Adapters/Cache/RedisCacheAdapter.js b/src/Adapters/Cache/RedisCacheAdapter.js index 5c1507f239..0347e97260 100644 --- a/src/Adapters/Cache/RedisCacheAdapter.js +++ b/src/Adapters/Cache/RedisCacheAdapter.js @@ -9,9 +9,10 @@ function debug() { export class RedisCacheAdapter { - constructor(ctx) { - this.client = redis.createClient(ctx); + constructor(redisCtx, ttl = DEFAULT_REDIS_TTL) { + this.client = redis.createClient(redisCtx); this.p = Promise.resolve(); + this.ttl = ttl; } get(key) { @@ -30,7 +31,7 @@ export class RedisCacheAdapter { return this.p; } - put(key, value, ttl = DEFAULT_REDIS_TTL) { + put(key, value, ttl = this.ttl) { value = JSON.stringify(value); debug('put', key, value, ttl); if (ttl === 0) { From 75ae1a493f1c044e754af95b38d849347f5e8d14 Mon Sep 17 00:00:00 2001 From: Ryan F Date: Wed, 25 Jan 2017 12:40:09 -0800 Subject: [PATCH 2/6] adding test for RedisCacheAdapter --- spec/RedisCacheAdapter.spec.js | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 spec/RedisCacheAdapter.spec.js diff --git a/spec/RedisCacheAdapter.spec.js b/spec/RedisCacheAdapter.spec.js new file mode 100644 index 0000000000..8dc70ab1fb --- /dev/null +++ b/spec/RedisCacheAdapter.spec.js @@ -0,0 +1,41 @@ +var InMemoryCacheAdapter = 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({ + ttl: 10 + }); + + cache.put(KEY, VALUE) + .then(() => cache.get(KEY)) + .then((value) => expect(value).toEqual(VALUE)) + .then(wait.bind(null, 50)) + .then(() => cache.get(KEY)) + .then((value) => expect(value).toEqual(null)) + .then(done); + }) + +}); From 6a5c0dd09b460eddac7bb8fdc75b24dd46532119 Mon Sep 17 00:00:00 2001 From: Ryan F Date: Wed, 25 Jan 2017 12:44:37 -0800 Subject: [PATCH 3/6] Fixing adapater var name --- spec/RedisCacheAdapter.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/RedisCacheAdapter.spec.js b/spec/RedisCacheAdapter.spec.js index 8dc70ab1fb..5dc8122b1f 100644 --- a/spec/RedisCacheAdapter.spec.js +++ b/spec/RedisCacheAdapter.spec.js @@ -1,4 +1,4 @@ -var InMemoryCacheAdapter = require('../src/Adapters/Cache/RedisCacheAdapter').default; +var RedisCacheAdapter = require('../src/Adapters/Cache/RedisCacheAdapter').default; describe('RedisCacheAdapter', function() { var KEY = 'hello'; From 947471bc00c3b049278b61e0707b2f64cac13605 Mon Sep 17 00:00:00 2001 From: f0ster Date: Sat, 28 Jan 2017 16:32:41 -0800 Subject: [PATCH 4/6] changing timeout --- src/Adapters/Cache/RedisCacheAdapter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapters/Cache/RedisCacheAdapter.js b/src/Adapters/Cache/RedisCacheAdapter.js index 0347e97260..afdf430ff8 100644 --- a/src/Adapters/Cache/RedisCacheAdapter.js +++ b/src/Adapters/Cache/RedisCacheAdapter.js @@ -1,7 +1,7 @@ import redis from 'redis'; import logger from '../../logger'; -const DEFAULT_REDIS_TTL = 30 * 1000; // 30 seconds in milliseconds +const DEFAULT_REDIS_TTL = 30; // 30 seconds in milliseconds function debug() { logger.debug.apply(logger, ['RedisCacheAdapter', ...arguments]); From 6479b0612c2a0247105827beb3f1e85c3a86c5a5 Mon Sep 17 00:00:00 2001 From: Ryan F Date: Wed, 22 Feb 2017 15:52:24 -0800 Subject: [PATCH 5/6] updating default time --- spec/RedisCacheAdapter.spec.js | 4 ++-- src/Adapters/Cache/RedisCacheAdapter.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/RedisCacheAdapter.spec.js b/spec/RedisCacheAdapter.spec.js index 5dc8122b1f..82e17c22db 100644 --- a/spec/RedisCacheAdapter.spec.js +++ b/spec/RedisCacheAdapter.spec.js @@ -26,13 +26,13 @@ describe('RedisCacheAdapter', function() { it('should expire after ttl', (done) => { var cache = new RedisCacheAdapter({ - ttl: 10 + ttl: 100 }); cache.put(KEY, VALUE) .then(() => cache.get(KEY)) .then((value) => expect(value).toEqual(VALUE)) - .then(wait.bind(null, 50)) + .then(wait.bind(null, 1000)) .then(() => cache.get(KEY)) .then((value) => expect(value).toEqual(null)) .then(done); diff --git a/src/Adapters/Cache/RedisCacheAdapter.js b/src/Adapters/Cache/RedisCacheAdapter.js index afdf430ff8..bc1de941aa 100644 --- a/src/Adapters/Cache/RedisCacheAdapter.js +++ b/src/Adapters/Cache/RedisCacheAdapter.js @@ -1,7 +1,7 @@ import redis from 'redis'; import logger from '../../logger'; -const DEFAULT_REDIS_TTL = 30; // 30 seconds in milliseconds +const DEFAULT_REDIS_TTL = 30000; // 30 seconds in milliseconds function debug() { logger.debug.apply(logger, ['RedisCacheAdapter', ...arguments]); From cd8300c001a9949eb0a7f2c1e802b657b6e689f9 Mon Sep 17 00:00:00 2001 From: Arthur Cinader Date: Wed, 22 Feb 2017 22:10:14 -0700 Subject: [PATCH 6/6] Fix the redis cache spec to construct the cache with the anticipated ttl make timeout values really really small so our test run fast :). --- spec/RedisCacheAdapter.spec.js | 19 ++++++++++++++----- src/Adapters/Cache/RedisCacheAdapter.js | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/spec/RedisCacheAdapter.spec.js b/spec/RedisCacheAdapter.spec.js index 82e17c22db..9e941eff49 100644 --- a/spec/RedisCacheAdapter.spec.js +++ b/spec/RedisCacheAdapter.spec.js @@ -25,17 +25,26 @@ describe('RedisCacheAdapter', function() { }); it('should expire after ttl', (done) => { - var cache = new RedisCacheAdapter({ - ttl: 100 - }); + var cache = new RedisCacheAdapter(null, 1); cache.put(KEY, VALUE) .then(() => cache.get(KEY)) .then((value) => expect(value).toEqual(VALUE)) - .then(wait.bind(null, 1000)) + .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); + }); }); diff --git a/src/Adapters/Cache/RedisCacheAdapter.js b/src/Adapters/Cache/RedisCacheAdapter.js index bc1de941aa..0347e97260 100644 --- a/src/Adapters/Cache/RedisCacheAdapter.js +++ b/src/Adapters/Cache/RedisCacheAdapter.js @@ -1,7 +1,7 @@ import redis from 'redis'; import logger from '../../logger'; -const DEFAULT_REDIS_TTL = 30000; // 30 seconds in milliseconds +const DEFAULT_REDIS_TTL = 30 * 1000; // 30 seconds in milliseconds function debug() { logger.debug.apply(logger, ['RedisCacheAdapter', ...arguments]);