Skip to content

Commit

Permalink
add maxAge option for alfy.cache - fixes #3 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren authored and sindresorhus committed Jul 13, 2016
1 parent 2969a26 commit 1272827
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 4 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Conf = require('conf');
const hookStd = require('hook-std');
const loudRejection = require('loud-rejection');
const cleanStack = require('clean-stack');
const CacheConf = require('./lib/cache-conf');

// prevent caching of this module so module.parent is always accurate
delete require.cache[__filename];
Expand Down Expand Up @@ -96,7 +97,7 @@ alfy.config = new Conf({
cwd: alfy.alfred.data
});

alfy.cache = new Conf({
alfy.cache = new CacheConf({
configName: 'cache',
cwd: alfy.alfred.cache
});
Expand Down
42 changes: 42 additions & 0 deletions lib/cache-conf.js
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;
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"files": [
"index.js",
"run-node.sh"
"run-node.sh",
"lib"
],
"keywords": [
"alfred",
Expand All @@ -43,6 +44,7 @@
},
"devDependencies": {
"ava": "*",
"delay": "^1.3.1",
"xo": "*"
}
}
24 changes: 23 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Type: `Object`

Persist cache data.

Exports a [`conf` instance](https://github.com/sindresorhus/conf#instance) with the correct cache path set.
Exports a modified [`conf` instance](https://github.com/sindresorhus/conf#instance) with the correct cache path set.

Example:

Expand All @@ -154,6 +154,28 @@ alfy.cache.get('unicorn');
//=> '🦄'
```

##### maxAge

The `set` method of this instance accepts an optional third argument where you can provide a `maxAge` option. `maxAge` is
the number of milliseconds the value is valid in the cache.

Example:

```js
const delay = require('delay');

alfy.cache.set('foo', 'bar', {maxAge: 5000});

alfy.cache.get('foo');
//=> 'bar'

// Wait 5 seconds
await delay(5000);

alfy.cache.get('foo');
//=> undefined
```

#### debug

Type: `boolean`
Expand Down
34 changes: 34 additions & 0 deletions test/cache.js
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);
});
2 changes: 1 addition & 1 deletion test.js → test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import hookStd from 'hook-std';

process.env.AVA = true;
const m = require('./');
const m = require('../');

test('default', t => {
t.false(m.debug);
Expand Down

0 comments on commit 1272827

Please sign in to comment.