-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a57b8f6
commit 5e88efe
Showing
13 changed files
with
202 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
'bentocache': minor | ||
--- | ||
|
||
Add a new `expire` method. | ||
|
||
This method is slightly different from `delete`: | ||
|
||
When we delete a key, it is completely removed and forgotten. This means that even if we use grace periods, the value will no longer be available. | ||
|
||
`expire` works like `delete`, except that instead of completely removing the value, we just mark it as expired but keep it for the grace period. For example: | ||
|
||
```ts | ||
// Set a value with a grace period of 6 minutes | ||
await cache.set({ | ||
key: 'hello', | ||
value: 'world', | ||
grace: '6m' | ||
}) | ||
|
||
// Expire the value. It is kept in the cache but marked as STALE for 6 minutes | ||
await cache.expire({ key: 'hello' }) | ||
|
||
// Here, a get with grace: false will return nothing, because the value is stale | ||
const r1 = await cache.get({ key: 'hello', grace: false }) | ||
|
||
// Here, a get with grace: true will return the value, because it is still within the grace period | ||
const r2 = await cache.get({ key: 'hello' }) | ||
|
||
assert.deepEqual(r1, undefined) | ||
assert.deepEqual(r2, 'world') | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { pEvent } from 'p-event' | ||
import { test } from '@japa/runner' | ||
import EventEmitter from 'node:events' | ||
|
||
import { CacheFactory } from '../factories/cache_factory.js' | ||
|
||
test.group('Expire', () => { | ||
test('[{name}] - expire a key from the cache') | ||
.with([ | ||
{ | ||
name: 'l1', | ||
factory: () => new CacheFactory().merge({ grace: '2m' }).withMemoryL1().create(), | ||
}, | ||
{ | ||
name: 'l2', | ||
factory: () => new CacheFactory().merge({ grace: '2m' }).withRedisL2().create(), | ||
}, | ||
{ | ||
name: 'l1/l2', | ||
factory: () => new CacheFactory().merge({ grace: '2m' }).withL1L2Config().create(), | ||
}, | ||
]) | ||
.run(async ({ assert }, { factory }) => { | ||
const { cache } = factory() | ||
|
||
await cache.set({ key: 'hello', value: 'world' }) | ||
await cache.expire({ key: 'hello' }) | ||
|
||
const r1 = await cache.get({ key: 'hello', grace: false }) | ||
const r2 = await cache.get({ key: 'hello' }) | ||
|
||
assert.deepEqual(r1, undefined) | ||
assert.deepEqual(r2, 'world') | ||
}) | ||
|
||
test('expire should publish an message to the bus', async ({ assert }) => { | ||
const [cache1] = new CacheFactory().merge({ grace: '3m' }).withL1L2Config().create() | ||
const [cache2] = new CacheFactory().merge({ grace: '3m' }).withL1L2Config().create() | ||
const [cache3] = new CacheFactory().merge({ grace: '3m' }).withL1L2Config().create() | ||
|
||
await cache1.set({ key: 'hello', value: 'world' }) | ||
await cache2.get({ key: 'hello' }) | ||
await cache3.get({ key: 'hello' }) | ||
|
||
await cache1.expire({ key: 'hello' }) | ||
|
||
const r1 = await cache1.get({ key: 'hello', grace: false }) | ||
const r2 = await cache2.get({ key: 'hello', grace: false }) | ||
const r3 = await cache3.get({ key: 'hello', grace: false }) | ||
|
||
const r4 = await cache1.get({ key: 'hello' }) | ||
const r5 = await cache2.get({ key: 'hello' }) | ||
const r6 = await cache3.get({ key: 'hello' }) | ||
|
||
assert.deepEqual(r1, undefined) | ||
assert.deepEqual(r2, undefined) | ||
assert.deepEqual(r3, undefined) | ||
|
||
assert.deepEqual(r4, 'world') | ||
assert.deepEqual(r5, 'world') | ||
assert.deepEqual(r6, 'world') | ||
}) | ||
|
||
test('expire should emit an event', async ({ assert }) => { | ||
const emitter = new EventEmitter() | ||
const [cache] = new CacheFactory().merge({ grace: '3m', emitter }).withL1L2Config().create() | ||
|
||
const eventPromise = pEvent(emitter, 'cache:expire') | ||
|
||
await cache.expire({ key: 'hello' }) | ||
|
||
const event = await eventPromise | ||
assert.deepEqual(event, { key: 'hello', store: 'primary' }) | ||
}) | ||
}) |