-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
2969a26
commit 1272827
Showing
6 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
const Conf = require('conf'); | ||
|
||
const isExpired = item => item && item.timestamp && item.timestamp < Date.now(); | ||
|
||
class CacheConf extends Conf { | ||
|
||
get(key) { | ||
const ret = super.get(key); | ||
|
||
if (isExpired(ret)) { | ||
super.delete(key); | ||
return; | ||
} | ||
|
||
return ret.data; | ||
} | ||
|
||
set(key, val, opts) { | ||
opts = opts || {}; | ||
|
||
super.set(key, { | ||
timestamp: opts.maxAge && Date.now() + opts.maxAge, | ||
data: val | ||
}); | ||
} | ||
|
||
has(key) { | ||
if (!super.has(key)) { | ||
return false; | ||
} | ||
|
||
if (isExpired(super.get(key))) { | ||
super.delete(key); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
module.exports = CacheConf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import test from 'ava'; | ||
import delay from 'delay'; | ||
|
||
process.env.AVA = true; | ||
const m = require('..'); | ||
|
||
test('no cache', t => { | ||
m.cache.set('foo', 'bar'); | ||
|
||
t.is(m.cache.get('foo'), 'bar'); | ||
t.true(m.cache.has('foo')); | ||
}); | ||
|
||
test('maxAge option', t => { | ||
m.cache.set('hello', {hello: 'world'}, {maxAge: 300000}); | ||
|
||
const age = m.cache.store.hello.timestamp - Date.now(); | ||
|
||
t.true(age <= 300000 && age >= 299000); | ||
t.true(m.cache.has('hello')); | ||
t.deepEqual(m.cache.get('hello'), {hello: 'world'}); | ||
}); | ||
|
||
test('expired data', async t => { | ||
m.cache.set('expire', {foo: 'bar'}, {maxAge: 5000}); | ||
|
||
t.true(m.cache.has('expire')); | ||
t.deepEqual(m.cache.get('expire'), {foo: 'bar'}); | ||
|
||
await delay(5000); | ||
|
||
t.false(m.cache.has('expire')); | ||
t.falsy(m.cache.store.expire); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters