|
| 1 | +import { pEvent } from 'p-event' |
| 2 | +import { test } from '@japa/runner' |
| 3 | +import EventEmitter from 'node:events' |
| 4 | + |
| 5 | +import { CacheFactory } from '../factories/cache_factory.js' |
| 6 | + |
| 7 | +test.group('Expire', () => { |
| 8 | + test('[{name}] - expire a key from the cache') |
| 9 | + .with([ |
| 10 | + { |
| 11 | + name: 'l1', |
| 12 | + factory: () => new CacheFactory().merge({ grace: '2m' }).withMemoryL1().create(), |
| 13 | + }, |
| 14 | + { |
| 15 | + name: 'l2', |
| 16 | + factory: () => new CacheFactory().merge({ grace: '2m' }).withRedisL2().create(), |
| 17 | + }, |
| 18 | + { |
| 19 | + name: 'l1/l2', |
| 20 | + factory: () => new CacheFactory().merge({ grace: '2m' }).withL1L2Config().create(), |
| 21 | + }, |
| 22 | + ]) |
| 23 | + .run(async ({ assert }, { factory }) => { |
| 24 | + const { cache } = factory() |
| 25 | + |
| 26 | + await cache.set({ key: 'hello', value: 'world' }) |
| 27 | + await cache.expire({ key: 'hello' }) |
| 28 | + |
| 29 | + const r1 = await cache.get({ key: 'hello', grace: false }) |
| 30 | + const r2 = await cache.get({ key: 'hello' }) |
| 31 | + |
| 32 | + assert.deepEqual(r1, undefined) |
| 33 | + assert.deepEqual(r2, 'world') |
| 34 | + }) |
| 35 | + |
| 36 | + test('expire should publish an message to the bus', async ({ assert }) => { |
| 37 | + const [cache1] = new CacheFactory().merge({ grace: '3m' }).withL1L2Config().create() |
| 38 | + const [cache2] = new CacheFactory().merge({ grace: '3m' }).withL1L2Config().create() |
| 39 | + const [cache3] = new CacheFactory().merge({ grace: '3m' }).withL1L2Config().create() |
| 40 | + |
| 41 | + await cache1.set({ key: 'hello', value: 'world' }) |
| 42 | + await cache2.get({ key: 'hello' }) |
| 43 | + await cache3.get({ key: 'hello' }) |
| 44 | + |
| 45 | + await cache1.expire({ key: 'hello' }) |
| 46 | + |
| 47 | + const r1 = await cache1.get({ key: 'hello', grace: false }) |
| 48 | + const r2 = await cache2.get({ key: 'hello', grace: false }) |
| 49 | + const r3 = await cache3.get({ key: 'hello', grace: false }) |
| 50 | + |
| 51 | + const r4 = await cache1.get({ key: 'hello' }) |
| 52 | + const r5 = await cache2.get({ key: 'hello' }) |
| 53 | + const r6 = await cache3.get({ key: 'hello' }) |
| 54 | + |
| 55 | + assert.deepEqual(r1, undefined) |
| 56 | + assert.deepEqual(r2, undefined) |
| 57 | + assert.deepEqual(r3, undefined) |
| 58 | + |
| 59 | + assert.deepEqual(r4, 'world') |
| 60 | + assert.deepEqual(r5, 'world') |
| 61 | + assert.deepEqual(r6, 'world') |
| 62 | + }) |
| 63 | + |
| 64 | + test('expire should emit an event', async ({ assert }) => { |
| 65 | + const emitter = new EventEmitter() |
| 66 | + const [cache] = new CacheFactory().merge({ grace: '3m', emitter }).withL1L2Config().create() |
| 67 | + |
| 68 | + const eventPromise = pEvent(emitter, 'cache:expire') |
| 69 | + |
| 70 | + await cache.expire({ key: 'hello' }) |
| 71 | + |
| 72 | + const event = await eventPromise |
| 73 | + assert.deepEqual(event, { key: 'hello', store: 'primary' }) |
| 74 | + }) |
| 75 | +}) |
0 commit comments