Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy ttl cache #442

Open
wants to merge 5 commits into
base: v4
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Changelog

_Note: Gaps between patch versions are faulty, broken or test releases._

## v4.0.0-alpha.46 (2024-09-18)

#### :house: Internal

* Changed the logic of the `addTTL` decorator to be lazy `core/cache/docorators/ttl`

## v4.0.0-alpha.45 (2024-09-09)

#### :bug: Bug Fix
Expand Down
6 changes: 2 additions & 4 deletions src/core/cache/decorators/helpers/add-emitter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,12 @@ const addEmitter: AddEmitter = <T extends Cache<V, K>, V = unknown, K extends st
clear: originalClear.bind(cacheWithEmitter),

subscribe: ((method, obj, cb): void => {
emitter.on(method, handler);

function handler(cacheWithEmitter: object, e: MutationEvent) {
emitter.on(method, (cacheWithEmitter: object, e: MutationEvent) => {
shining-mind marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (cacheWithEmitter === obj || {}.isPrototypeOf.call(cacheWithEmitter, obj)) {
cb(e);
}
}
});
})
};
};
Expand Down
7 changes: 5 additions & 2 deletions src/core/cache/decorators/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import { INDEX_STORAGE_NAME } from 'core/cache/decorators/persistent/engines/con

describe('core/cache/decorators', () => {
it('complex test', async () => {
jest.spyOn(Date, 'now').mockReturnValue(0);

const opts = {
loadFromStorage: 'onInit'
};
Expand All @@ -44,6 +42,11 @@ describe('core/cache/decorators', () => {
});

await new Promise((r) => setTimeout(r, 50));

expect(await persistentCache.get('bar')).toBe(undefined);

await new Promise((r) => setTimeout(r, 0));

expect(await asyncLocal.get(INDEX_STORAGE_NAME)).toEqual({
baz: Number.MAX_SAFE_INTEGER
});
Expand Down
6 changes: 6 additions & 0 deletions src/core/cache/decorators/ttl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Changelog
> - :house: [Internal]
> - :nail_care: [Polish]

## v4.0.0-alpha.46 (2024-09-18)

#### :house: Internal

* Changed the logic of the `addTTL` decorator to be lazy

## v3.47.0 (2021-05-17)

#### :bug: Bug Fix
Expand Down
54 changes: 37 additions & 17 deletions src/core/cache/decorators/ttl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,29 @@ export default function addTTL<
} = addEmitter<TTLCache<V, K>, V, K>(<TTLCache<V, K>><unknown>cache);

const
cacheWithTTL: TTLCache<V, K> = Object.create(cache),
ttlTimers = new Map<K, number | NodeJS.Timeout>();
store = new Map<K, { ttl: number; date: number }>();

const
cacheWithTTL: TTLCache<V, K> = Object.create(cache);

cacheWithTTL.get = (key: K) => {
const
info = store.get(key);

if (info != null) {
const
expired = Date.now() - info.date > info.ttl;

if (expired) {
cacheWithTTL.remove(key);
shining-mind marked this conversation as resolved.
Show resolved Hide resolved
return;
}
}

return cache.get(key);
};

cacheWithTTL.has = (key: K) => cacheWithTTL.get(key) !== undefined;

cacheWithTTL.set = (key: K, value: V, opts?: TTLDecoratorOptions & Parameters<T['set']>[2]) => {
updateTTL(key, opts?.ttl);
Expand All @@ -67,23 +88,18 @@ export default function addTTL<
return originalRemove(key);
};

cacheWithTTL.removeTTLFrom = (key: K) => {
if (ttlTimers.has(key)) {
clearTimeout(<number>ttlTimers.get(key));
ttlTimers.delete(key);
return true;
}

return false;
};
cacheWithTTL.removeTTLFrom = (key: K) => store.delete(key);

cacheWithTTL.clear = (filter?: ClearFilter<V, K>) => {
const
removed = originalClear(filter);

removed.forEach((_, key) => {
cacheWithTTL.removeTTLFrom(key);
});
if (filter == null) {
store.clear();

} else {
removed.forEach((_, key) => cacheWithTTL.removeTTLFrom(key));
}

return removed;
};
Expand All @@ -101,9 +117,13 @@ export default function addTTL<
return cacheWithTTL;

function updateTTL(key: K, optionTTL?: number): void {
if (optionTTL != null || ttl != null) {
const time = optionTTL ?? ttl;
ttlTimers.set(key, setTimeout(() => cacheWithTTL.remove(key), time));
const time = optionTTL ?? ttl;

if (time != null) {
store.set(key, {
ttl: time,
date: Date.now()
});

} else {
cacheWithTTL.removeTTLFrom(key);
Expand Down
Loading