Skip to content

Add policy event #4219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,23 @@ The internally generated events are (identified by their `tags`):
- `connection` `client` `error` - a `clientError` event was received from the HTTP or HTTPS
listener. The event data is the error object received.

##### <a name="server.events.cachePolicy" /> `'cachePolicy'` Event

The `'cachePolicy'` event type is emitted when a server [cache policy](https://hapi.dev/module/catbox/api#policy)
is created via [`server.cache()`](#server.cache()) or a [`server.method()`](#server.method()) with caching enabled is registered.
The `'cachePolicy'` event handler uses the function signature `function(cachePolicy, cache, segment)` where:

- `cachePolicy` - the catbox [cache policy](https://hapi.dev/module/catbox/api#policy).
- `cache` - the [cache provision](#server.options.cache) name used when the policy was created or `undefined` if the default cache was used.
- `segment` - the segment name used when the policy was created.

```js
server.events.on('cachePolicy', (cachePolicy, cache, segment) => {

console.log(`New cache policy created using cache: ${cache === undefined ? 'default' : cache} and segment: ${segment}`);
});
```

##### <a name="server.events.request" /> `'request'` Event

The `'request'` event type emits internal request events generated by the framework as well as
Expand Down
6 changes: 5 additions & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const internals = {
max: 99999
},
events: [
{ name: 'cachePolicy', spread: true },
{ name: 'log', channels: ['app', 'internal'], tags: true },
{ name: 'request', channels: ['app', 'internal', 'error'], tags: true, spread: true },
'response',
Expand Down Expand Up @@ -610,7 +611,10 @@ exports = module.exports = internals.Core = class {
Hoek.assert(!cache.segments[segment] || cache.shared || options.shared, 'Cannot provision the same cache segment more than once');
cache.segments[segment] = true;

return new Catbox.Policy(options, cache.client, segment);
const policy = new Catbox.Policy(options, cache.client, segment);
this.events.emit('cachePolicy', [policy, options.cache, segment]);

return policy;
}

log(tags, data) {
Expand Down
38 changes: 38 additions & 0 deletions test/methods.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const Catbox = require('@hapi/catbox');
const CatboxMemory = require('@hapi/catbox-memory');
const Code = require('@hapi/code');
const Hapi = require('..');
Expand Down Expand Up @@ -208,6 +209,43 @@ describe('Methods', () => {
expect(result2.gen).to.equal(0);
});

it('emits a cache policy event on cached methods with default cache provision', async () => {

const method = function (id) {

return { id };
};

const server = Hapi.server();
const cachePolicyEvent = server.events.once('cachePolicy');

server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 } });

const [policy, cacheName, segment] = await cachePolicyEvent;
expect(policy).to.be.instanceOf(Catbox.Policy);
expect(cacheName).to.equal(undefined);
expect(segment).to.equal('#test');
});

it('emits a cache policy event on cached methods with named cache provision', async () => {

const method = function (id) {

return { id };
};

const server = Hapi.server();
await server.cache.provision({ provider: CatboxMemory, name: 'named' });
const cachePolicyEvent = server.events.once('cachePolicy');

server.method('test', method, { cache: { cache: 'named', expiresIn: 1000, generateTimeout: 10 } });

const [policy, cacheName, segment] = await cachePolicyEvent;
expect(policy).to.be.instanceOf(Catbox.Policy);
expect(cacheName).to.equal('named');
expect(segment).to.equal('#test');
});

it('caches method value (async)', async () => {

let gen = 0;
Expand Down
27 changes: 27 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,33 @@ describe('Server', () => {
const value2 = await server.plugins.test.get('a');
expect(value2).to.equal(null);
});

it('emits a cache policy event with default cache provision', async () => {

const server = Hapi.server();
const cachePolicyEvent = server.events.once('cachePolicy');

const cache = server.cache({ segment: 'test', expiresIn: 1000 });

const [policy, cacheName, segment] = await cachePolicyEvent;
expect(policy).to.shallow.equal(cache);
expect(cacheName).to.equal(undefined);
expect(segment).to.equal('test');
});

it('emits a cache policy event with named cache provision', async () => {

const server = Hapi.server();
await server.cache.provision({ provider: CatboxMemory, name: 'named' });
const cachePolicyEvent = server.events.once('cachePolicy');

const cache = server.cache({ cache: 'named', segment: 'test', expiresIn: 1000 });

const [policy, cacheName, segment] = await cachePolicyEvent;
expect(policy).to.shallow.equal(cache);
expect(cacheName).to.equal('named');
expect(segment).to.equal('test');
});
});

describe('cache.provision()', () => {
Expand Down