Skip to content

Commit 05ac357

Browse files
Add policy event (hapijs#4219)
* Add support for policy event * Update policy event docs to include server methods * Add policy event tests when used with server.method() * Rename policy event to cachePolicy
1 parent f075a70 commit 05ac357

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

API.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,23 @@ The internally generated events are (identified by their `tags`):
480480
- `connection` `client` `error` - a `clientError` event was received from the HTTP or HTTPS
481481
listener. The event data is the error object received.
482482

483+
##### <a name="server.events.cachePolicy" /> `'cachePolicy'` Event
484+
485+
The `'cachePolicy'` event type is emitted when a server [cache policy](https://hapi.dev/module/catbox/api#policy)
486+
is created via [`server.cache()`](#server.cache()) or a [`server.method()`](#server.method()) with caching enabled is registered.
487+
The `'cachePolicy'` event handler uses the function signature `function(cachePolicy, cache, segment)` where:
488+
489+
- `cachePolicy` - the catbox [cache policy](https://hapi.dev/module/catbox/api#policy).
490+
- `cache` - the [cache provision](#server.options.cache) name used when the policy was created or `undefined` if the default cache was used.
491+
- `segment` - the segment name used when the policy was created.
492+
493+
```js
494+
server.events.on('cachePolicy', (cachePolicy, cache, segment) => {
495+
496+
console.log(`New cache policy created using cache: ${cache === undefined ? 'default' : cache} and segment: ${segment}`);
497+
});
498+
```
499+
483500
##### <a name="server.events.request" /> `'request'` Event
484501

485502
The `'request'` event type emits internal request events generated by the framework as well as

lib/core.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const internals = {
3636
max: 99999
3737
},
3838
events: [
39+
{ name: 'cachePolicy', spread: true },
3940
{ name: 'log', channels: ['app', 'internal'], tags: true },
4041
{ name: 'request', channels: ['app', 'internal', 'error'], tags: true, spread: true },
4142
'response',
@@ -613,7 +614,10 @@ exports = module.exports = internals.Core = class {
613614
Hoek.assert(!cache.segments[segment] || cache.shared || options.shared, 'Cannot provision the same cache segment more than once');
614615
cache.segments[segment] = true;
615616

616-
return new Catbox.Policy(options, cache.client, segment);
617+
const policy = new Catbox.Policy(options, cache.client, segment);
618+
this.events.emit('cachePolicy', [policy, options.cache, segment]);
619+
620+
return policy;
617621
}
618622

619623
log(tags, data) {

test/methods.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const Catbox = require('@hapi/catbox');
34
const CatboxMemory = require('@hapi/catbox-memory');
45
const Code = require('@hapi/code');
56
const Hapi = require('..');
@@ -208,6 +209,43 @@ describe('Methods', () => {
208209
expect(result2.gen).to.equal(0);
209210
});
210211

212+
it('emits a cache policy event on cached methods with default cache provision', async () => {
213+
214+
const method = function (id) {
215+
216+
return { id };
217+
};
218+
219+
const server = Hapi.server();
220+
const cachePolicyEvent = server.events.once('cachePolicy');
221+
222+
server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 } });
223+
224+
const [policy, cacheName, segment] = await cachePolicyEvent;
225+
expect(policy).to.be.instanceOf(Catbox.Policy);
226+
expect(cacheName).to.equal(undefined);
227+
expect(segment).to.equal('#test');
228+
});
229+
230+
it('emits a cache policy event on cached methods with named cache provision', async () => {
231+
232+
const method = function (id) {
233+
234+
return { id };
235+
};
236+
237+
const server = Hapi.server();
238+
await server.cache.provision({ provider: CatboxMemory, name: 'named' });
239+
const cachePolicyEvent = server.events.once('cachePolicy');
240+
241+
server.method('test', method, { cache: { cache: 'named', expiresIn: 1000, generateTimeout: 10 } });
242+
243+
const [policy, cacheName, segment] = await cachePolicyEvent;
244+
expect(policy).to.be.instanceOf(Catbox.Policy);
245+
expect(cacheName).to.equal('named');
246+
expect(segment).to.equal('#test');
247+
});
248+
211249
it('caches method value (async)', async () => {
212250

213251
let gen = 0;

test/server.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,33 @@ describe('Server', () => {
193193
const value2 = await server.plugins.test.get('a');
194194
expect(value2).to.equal(null);
195195
});
196+
197+
it('emits a cache policy event with default cache provision', async () => {
198+
199+
const server = Hapi.server();
200+
const cachePolicyEvent = server.events.once('cachePolicy');
201+
202+
const cache = server.cache({ segment: 'test', expiresIn: 1000 });
203+
204+
const [policy, cacheName, segment] = await cachePolicyEvent;
205+
expect(policy).to.shallow.equal(cache);
206+
expect(cacheName).to.equal(undefined);
207+
expect(segment).to.equal('test');
208+
});
209+
210+
it('emits a cache policy event with named cache provision', async () => {
211+
212+
const server = Hapi.server();
213+
await server.cache.provision({ provider: CatboxMemory, name: 'named' });
214+
const cachePolicyEvent = server.events.once('cachePolicy');
215+
216+
const cache = server.cache({ cache: 'named', segment: 'test', expiresIn: 1000 });
217+
218+
const [policy, cacheName, segment] = await cachePolicyEvent;
219+
expect(policy).to.shallow.equal(cache);
220+
expect(cacheName).to.equal('named');
221+
expect(segment).to.equal('test');
222+
});
196223
});
197224

198225
describe('cache.provision()', () => {

0 commit comments

Comments
 (0)