Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit 9fc460d

Browse files
committed
Merge branch 'SassNinja-feature/custom-cache-key'
2 parents 2f56475 + fd2dc8b commit 9fc460d

File tree

8 files changed

+80
-7
lines changed

8 files changed

+80
-7
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ both items with id `123` would be saved under the same cache key... thus replaci
115115
##### env
116116
The default environement is production, but it is anoying when running test as the hooks output information to the console. Therefore if you youse this option, you can set `test` as an environement and the hooks will not output anything to the console. if you use `NODE_ENV` it will pick up the `process.env.NODE_ENV` variable. This is useful for CI or CLI.
117117

118+
##### immediateCacheKey
119+
By default the redis cache key gets determined in `redisAfterHook` based on the path. However if you're doing a lot of query manipulation you might want to set the cache key before anything else to keep its size as small as possible. You can achieve this by setting `immediateCacheKey: true` what will set the cache key in the `redisBeforeHook`. Then your hooks might look similar to:
120+
121+
```js
122+
{
123+
before: {
124+
find: [redisBefore({ immediateCacheKey: true }), someQueryManipulation()]
125+
},
126+
after: {
127+
find: [cache(), redisAfter()]
128+
}
129+
}
130+
```
131+
118132

119133
Available routes:
120134
```js

lib/library.js

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/library.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/library.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/library.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hooks/redis.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { parsePath } from './helpers/path';
44

55
const defaults = {
66
env: 'production',
7-
defaultDuration: 3600 * 24
7+
defaultDuration: 3600 * 24,
8+
immediateCacheKey: false
89
};
910

1011
export function before(options) { // eslint-disable-line no-unused-vars
@@ -32,6 +33,9 @@ export function before(options) { // eslint-disable-line no-unused-vars
3233
console.log(`> Expires on ${duration}.`);
3334
}
3435
} else {
36+
if (options.immediateCacheKey === true) {
37+
hook.params.cacheKey = path;
38+
}
3539
resolve(hook);
3640
}
3741
});
@@ -49,7 +53,7 @@ export function after(options) { // eslint-disable-line no-unused-vars
4953
if (!hook.result.cache.cached) {
5054
const duration = hook.result.cache.duration || options.defaultDuration;
5155
const client = hook.app.get('redisClient');
52-
const path = parsePath(hook, options);
56+
const path = hook.params.cacheKey || parsePath(hook, options);
5357

5458
// adding a cache object
5559
Object.assign(hook.result.cache, {

test/hooks/redis-after.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,37 @@ describe('Redis After Hook', () => {
7272
});
7373
});
7474

75+
it('caches a route using params.cacheKey instead of params.query', () => {
76+
const hook = a();
77+
const mock = {
78+
params: {
79+
query: { foo: 'bar', lorem: 'ipsum' },
80+
cacheKey: 'after-cache-key?foo=bar'
81+
},
82+
id: 'after-cache-key',
83+
path: '',
84+
result: {
85+
_sys: {
86+
status: 200
87+
},
88+
cache: {
89+
cached: false
90+
}
91+
},
92+
app: {
93+
get: (what) => {
94+
return client;
95+
}
96+
}
97+
};
98+
99+
return hook(mock).then(result => {
100+
const data = result.result;
101+
102+
expect(data.cache.key).to.equal('after-cache-key?foo=bar');
103+
});
104+
});
105+
75106
it('caches a parent route that returns an array', () => {
76107
const hook = a();
77108
const mock = {
@@ -104,6 +135,7 @@ describe('Redis After Hook', () => {
104135
expect(data.cache.key).to.equal('test-route');
105136
});
106137
});
138+
107139
it('caches a parent route with setting to remove path from key...', () => {
108140
const hook = a();
109141
const mock = {

test/hooks/redis-before.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ describe('Redis Before Hook', () => {
7474
));
7575
});
7676

77+
it('sets cacheKey in before hook if immediateCacheKey true', () => {
78+
const hook = b({ immediateCacheKey: true });
79+
const mock = {
80+
params: { query: { foo: 'bar' }},
81+
path: '',
82+
id: 'before-cache-key',
83+
app: {
84+
get: (what) => {
85+
return client;
86+
}
87+
}
88+
};
89+
90+
return hook(mock).then(result => {
91+
expect(result.params.cacheKey).to.be.equal('before-cache-key?foo=bar');
92+
});
93+
});
94+
7795
it('retrives a cached object', () => {
7896
const hook = b();
7997
const mock = {

0 commit comments

Comments
 (0)