Skip to content

Commit

Permalink
made adTTL lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Bunin committed Sep 18, 2024
1 parent 3852c1b commit 8d775fb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
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

* Made the `addTTL` functionality lazy `core/cache/docorators/ttl`

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

#### :bug: Bug Fix
Expand Down
8 changes: 3 additions & 5 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) => {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (cacheWithEmitter === obj || {}.isPrototypeOf.call(cacheWithEmitter, obj)) {
if (cacheWithEmitter === obj || cacheWithEmitter.isPrototypeOf(obj)) {
cb(e);
}
}
});
})
};
};
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

* Made `addTTL` functionality lazy

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

#### :bug: Bug Fix
Expand Down
52 changes: 41 additions & 11 deletions src/core/cache/decorators/ttl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,31 @@ 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>();
ttlStore = new Map<K, number>(),
dateStore = new Map<K, number>();

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

cacheWithTTL.get = (key: K) => {
const
dateSet = dateStore.get(key),
ttl = ttlStore.get(key);

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

if (expired) {
cacheWithTTL.remove(key);
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 @@ -68,9 +91,10 @@ export default function addTTL<
};

cacheWithTTL.removeTTLFrom = (key: K) => {
if (ttlTimers.has(key)) {
clearTimeout(<number>ttlTimers.get(key));
ttlTimers.delete(key);
if (ttlStore.has(key) || dateStore.has(key)) {
ttlStore.delete(key);
dateStore.delete(key);

return true;
}

Expand All @@ -81,9 +105,13 @@ export default function addTTL<
const
removed = originalClear(filter);

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

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

return removed;
};
Expand All @@ -101,9 +129,11 @@ 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) {
ttlStore.set(key, time);
dateStore.set(key, Date.now());

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

0 comments on commit 8d775fb

Please sign in to comment.