Skip to content

Commit

Permalink
IPLAYER-45548: Replace cache key package version with schema version (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cjewell47 authored Oct 15, 2024
1 parent 276b4e8 commit 3ec280d
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ Both `maxage` and `staleIfError` accept an options object.
|`connectionCircuitBreakerOptions`|object|maxAge,staleIfError| When present an instance of [Levee](https://github.com/krakenjs/levee) will be created with these configuration options to use on connection to cache.|
|`includeCacheStatusInCtx`|boolean|maxAge,staleIfError| When present, a `cacheStatus` array - recording all cache events, will be set in `context` for use by other plugins. `includeCacheStatusInCtx` is `false` by default.|

## Cache version

The cache verison is stored in the `config.json`, this is distinct from the library version in the `package.json`. The cache version is used in the cache key and is intended to reduce cache fragmentation in a scenario where multiple different versions of this library might be in use across a single estate.

The cache version **must** be incremented if a change is made to the data stored in the cache that would be incompatible with the existing version. Otherwise it should not be changed.

## Cache Key Structure

The cache uses `catbox` to provide a simple pluggable interface, this supports segmenting the cache as well as IDs, thus the following segments are used:
Expand Down
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cache": {
"version": "4.3.1"
}
}
3 changes: 2 additions & 1 deletion lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
const _ = require('lodash');
const bluebird = require('bluebird');
const events = require('./events');
const VERSION = require('../package').version;
const VERSION = require('../config').cache.version;

const TimeoutError = bluebird.TimeoutError;

function createCacheKey(segment, ctx, opts) {
/** The `VERSION` should be manually incremented in the config if there are changes to the data */
const versionedSegment = `http-transport:${VERSION}:${segment}`;
const requiredIdPart = `${ctx.req.getMethod()}:${ctx.req.getUrl()}`;
const varyOn = _.get(opts, 'varyOn');
Expand Down
2 changes: 1 addition & 1 deletion test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { events } = require('../');

const sandbox = sinon.createSandbox();
const SEGMENT = 'body';
const VERSION = require('../package').version;
const VERSION = require('../config').cache.version;
const bodySegment = {
segment: `http-transport:${VERSION}:body`,
id: 'GET:http://www.example.com/'
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Catbox = require('@hapi/catbox');
const Memory = require('@hapi/catbox-memory').Engine;
const bluebird = require('bluebird');

const VERSION = require('../package').version;
const VERSION = require('../config').cache.version;
const httpTransportCache = require('../');

const bodySegment = {
Expand Down
2 changes: 1 addition & 1 deletion test/maxAge.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { events } = cache;

const api = nock('http://www.example.com');

const VERSION = require('../package').version;
const VERSION = require('../config').cache.version;

const defaultHeaders = {
'cache-control': 'max-age=60'
Expand Down
2 changes: 1 addition & 1 deletion test/staleIfError.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const toError = require('@bbc/http-transport-to-error');
const cache = require('../');
const events = require('../').events;

const VERSION = require('../package').version;
const VERSION = require('../config').cache.version;
const api = nock('http://www.example.com');

const sandbox = sinon.createSandbox();
Expand Down

0 comments on commit 3ec280d

Please sign in to comment.