Skip to content
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
3 changes: 3 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:
- name: Install dependencies
run: pnpm install

- name: Approve Builds
run: pnpm approve-builds

- name: Build
run: pnpm build

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/deploy-website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:

- name: Install Modules
run: pnpm install

- name: Approve Builds
run: pnpm approve-builds

- name: Build Website
run: pnpm website:build
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ jobs:
- name: Build
run: pnpm build

- name: Approve Builds
run: pnpm approve-builds

- name: Test
run: pnpm test:ci
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"c8": "^10.1.3",
"vitest": "^3.0.4",
"wrangler": "^3.105.1"
},
"pnpm": {
"onlyBuiltDependencies": ["esbuild", "protobufjs", "sqlite3", "workerd"]
}
}
2 changes: 1 addition & 1 deletion packages/cache-manager/test/wrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('wrap', () => {

expect(await cache.wrap(data.key, async () => ++value, getTtlFunction, config.refreshThreshold)).toEqual(11); // 1st call should be cached
expect(getTtlFunction).toHaveBeenNthCalledWith(1, 11); // Ttl func called 1st time when cache empty
await sleep(1001);
await sleep(1500);
expect(await cache.wrap(data.key, async () => ++value, getTtlFunction, config.refreshThreshold)).toEqual(11); // Trigger background refresh. stale value returned
expect(getTtlFunction).toHaveBeenNthCalledWith(2, 12); // Ttl func called 2nd time triggered by refreshThreshold on fresh item
});
Expand Down
33 changes: 17 additions & 16 deletions packages/cacheable/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,31 @@ describe('cacheable options and properties', async () => {
const getResult = await cacheable.get('key');
expect(getResult).toEqual('value');
});
test('should be able to set KeyvStorageAdapter', async () => {
const keyvRedis = new KeyvRedis('redis://localhost:6379');
const cacheable = new Cacheable({secondary: keyvRedis});
expect(cacheable.secondary).toBeDefined();
const setResult = await cacheable.set('key', 'value');
test('should be able to set primary KeyvStorageAdapter', async () => {
const primary = new KeyvRedis('redis://localhost:6379');
const cacheable = new Cacheable({primary});
const key = 'key12345-opt';
expect(cacheable.primary).toBeDefined();
const setResult = await cacheable.set(key, 'value');
expect(setResult).toEqual(true);
const getResult = await cacheable.get('key');
const getResult = await cacheable.get(key);
expect(getResult).toEqual('value');
await cacheable.delete('key');
const getResult2 = await cacheable.get('key');
await cacheable.delete(key);
const getResult2 = await cacheable.get(key);
expect(getResult2).toBeUndefined();
});
test('should be able to set primary KeyvStorageAdapter', async () => {
const keyvRedis = new KeyvRedis('redis://localhost:6379');
const cacheable = new Cacheable({primary: keyvRedis});

test('should be able to set secondary KeyvStorageAdapter', async () => {
const secondary = new KeyvRedis('redis://localhost:6379');
const cacheable = new Cacheable({secondary});
const key = 'key12345-optsec';
expect(cacheable.primary).toBeDefined();
const setResult = await cacheable.set('key', 'value');
const setResult = await cacheable.set(key, 'value');
expect(setResult).toEqual(true);
const getResult = await cacheable.get('key');
const getResult = await cacheable.get(key);
expect(getResult).toEqual('value');
await cacheable.delete('key');
const getResult2 = await cacheable.get('key');
expect(getResult2).toBeUndefined();
});

test('should be able to set ttl default', async () => {
const cacheable = new Cacheable({ttl: 1000});
expect(cacheable.ttl).toEqual(1000);
Expand Down
2 changes: 1 addition & 1 deletion packages/node-cache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export type NodeCacheOptions = {
stdTTL?: number; // The standard ttl as number in seconds for every generated cache element. 0 = unlimited. If string, it will be parsed as shorthand and default to milliseconds if it is a number as a string.
checkperiod?: number; // Default is 600, 0 means no periodic check
useClones?: boolean; // Default is true
deleteOnExpire?: boolean; // Default is true, if this is set to true it will delete the key when it expires.
deleteOnExpire?: boolean; // Default is true, if false it will keep the key and not delete during an interval check and the value on get() will be undefined
maxKeys?: number; // Default is -1 (unlimited). If this is set it will throw and error if you try to set more keys than the max.
};
```
Expand Down
8 changes: 6 additions & 2 deletions packages/node-cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type NodeCacheOptions = {
*/
useClones?: boolean;
/**
* Delete all expired items at the set interval. Default is true.
* Delete expired items during an interval check or a single item on a get request. Default is true.
*/
deleteOnExpire?: boolean;
/**
Expand Down Expand Up @@ -440,7 +440,11 @@ export class NodeCache extends Hookified {
private checkData(): void {
for (const [key, value] of this.store.entries()) {
if (value.ttl > 0 && value.ttl < Date.now()) {
this.del(key);
if (this.options.deleteOnExpire) {
this.del(key);
}

this.emit('expired', this.formatKey(key), value.value);
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions packages/node-cache/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,23 @@ describe('NodeCache', () => {
expect(cache.get('foo')).toBe('bar');
await sleep(300);
expect(cache.get('foo')).toBe(undefined);
cache.close();
});

test('should not delete if expired even on interval', async () => {
const cache = new NodeCache({checkperiod: 1, deleteOnExpire: false});
let expiredKey = '';
cache.on('expired', key => {
expiredKey = key as string;
});
cache.set('foo-expired', 'bar', 0.25);
cache.set('baz-expired', 'qux', 2);
expect(cache.getStats().keys).toBe(2);
await sleep(1000);
expect(cache.getStats().keys).toBe(2);
expect(expiredKey).toBe('foo-expired');
const expiredValue = cache.get('foo-expired') as string;
expect(expiredValue).toBe(undefined);
cache.close();
});
});
Loading