Skip to content

IBLCATALOG-616: quick draft for a new refresh method - #94

Open
notten-bbc wants to merge 4 commits into
masterfrom
IBLCATALOG-616-ceych-refresh
Open

IBLCATALOG-616: quick draft for a new refresh method#94
notten-bbc wants to merge 4 commits into
masterfrom
IBLCATALOG-616-ceych-refresh

Conversation

@notten-bbc

Copy link
Copy Markdown

No description provided.

@BenSymons
BenSymons marked this pull request as ready for review August 17, 2026 13:07
Comment thread test/lib/ceych.js
it('sets the TTL if the second argument is an integer', async () => {
const func = ceych.wrap(wrappable, 5);

return func()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lots of tests used a return then a .then to assert on the cache client. We refactored it to async await to make it much simpler

Comment thread test/lib/ceych.js
cacheClient.get.onFirstCall().returns(null)
.onSecondCall().returns({ item: 1 })
.onThirdCall().returns(null);
const dropStub = sandbox.stub().resolves();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

many of the tests re-mocked the whole of the cache client for some reason. We changed this so it only re-mocked the get method making it much cleaner

Comment thread test/lib/ceych.js

describe('ceych', () => {
let ceych;
const wrappable = sandbox.stub().returns(Promise.resolve(1));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the mocks weren't defined in the beforeEach which was causing issues with the tests

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new “manual cache update” API on Ceych and updates tests/docs to support and describe this behavior.

Changes:

  • Add Ceych#set() for manually overwriting a cache entry (with randomized TTL and optional StatsD metric).
  • Refactor existing tests to reuse a shared Catbox client setup and modernize some assertions to async/await.
  • Update README docs and add a packageManager pin in package.json.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
lib/ceych.js Adds set() API and refactors option parsing helper used by invalidate().
test/lib/ceych.js Adds tests for set() and refactors cache client stubbing/setup across suites.
README.md Documents the new ceych.set API (section added/edited).
package.json Adds packageManager field (pnpm pin).
Suppressed comments (2)

test/lib/ceych.js:300

  • ceych.set() returns the underlying cache.set() promise; the tests should await it to avoid race conditions (and to fail properly if cache.set() rejects).
      // Manually set a new value in the cache
      ceych.set(wrappable, [], 100);

      await wrapped();

test/lib/ceych.js:322

  • This test stubs Math.random outside the Sinon sandbox (so it won't be restored in afterEach), and it asserts a TTL value in seconds even though the cache layer is called with TTL in milliseconds elsewhere (memoize multiplies by 1000).
    it('sets the TTL to a random value between defaultTTL and defaultTTL / 2', async () => {
      const cacheKey = createCacheKey(wrappable, [], '');
      const wrapped = ceych.wrap(wrappable);
      sinon.stub(Math, 'random').returns(1);

      ceych.set(wrappable, [], 100);

      sinon.assert.calledOnce(cacheClient.set);
      const setArgs = cacheClient.set.getCall(0).args;
      assert.strictEqual(setArgs[0].id, cacheKey.id);
      assert.strictEqual(setArgs[1], 100);
      assert.strictEqual(setArgs[2], 15);
    });

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread lib/ceych.js
Comment on lines +35 to 38
function getOptions(opts) {
if (!opts) {
throw new Error('Incorrect invalidate opts received, you must pass a function or options object to invalidate.');
}
Comment thread lib/ceych.js Outdated
Comment thread test/lib/ceych.js
Comment thread README.md Outdated
Comment on lines +80 to +84
#### `ceych.set()

Use this to manually sets the cache entry for the given function and args combination. You can use this to overrwrite an existing cache entry to a newer one.

The new cache key will have a TTL set randomly between this.defaultTtl/2 and this.defaultTtl. This is to ensure that when manually setting a lot of cache keys at the same time, they don't end up all expiring at the same time and causing lots of caches misses.
Comment thread lib/ceych.js Outdated
Comment on lines +129 to +134
* The new cache key will have a TTL set randomly between this.defaultTtl/2 and this.defaultTtl.
* This is to ensure that when manually setting a lot of cache keys at the same time, they don't end
* up all expiring at the same time and causing lots of caches misses.
* @param {function | {func: function, suffix: string}} funcOrOpts Either a function or options including `func` and optional `suffix`.
* @param {...any} functionArgs The args that identify the target cache key.
* @param {*} updatedValue The value to store in cache.
BenSymons and others added 2 commits August 17, 2026 14:27
Co-authored-by: Nicolas Otten <nicolas.otten@bbc.co.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants