Skip to content

Commit 41c3fc2

Browse files
author
Aleksandr Bunin
committed
feat(addTTL): make it lazy
1 parent 22cb72b commit 41c3fc2

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

src/core/cache/decorators/helpers/add-emitter/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,12 @@ const addEmitter: AddEmitter = <T extends Cache<V, K>, V = unknown, K extends st
137137
clear: originalClear.bind(cacheWithEmitter),
138138

139139
subscribe: ((method, obj, cb): void => {
140-
emitter.on(method, handler);
141-
142-
function handler(cacheWithEmitter: object, e: MutationEvent) {
140+
emitter.on(method, (cacheWithEmitter: object, e: MutationEvent) => {
143141
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
144-
if (cacheWithEmitter === obj || {}.isPrototypeOf.call(cacheWithEmitter, obj)) {
142+
if (cacheWithEmitter === obj || cacheWithEmitter.isPrototypeOf(obj)) {
145143
cb(e);
146144
}
147-
}
145+
});
148146
})
149147
};
150148
};

src/core/cache/decorators/ttl/index.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,31 @@ export default function addTTL<
5454
} = addEmitter<TTLCache<V, K>, V, K>(<TTLCache<V, K>><unknown>cache);
5555

5656
const
57-
cacheWithTTL: TTLCache<V, K> = Object.create(cache),
58-
ttlTimers = new Map<K, number | NodeJS.Timeout>();
57+
ttlStore = new Map<K, number>(),
58+
dateStore = new Map<K, number>();
59+
60+
const
61+
cacheWithTTL: TTLCache<V, K> = Object.create(cache);
62+
63+
cacheWithTTL.get = (key: K) => {
64+
const
65+
dateSet = dateStore.get(key),
66+
ttl = ttlStore.get(key);
67+
68+
if (dateSet != null && ttl != null) {
69+
const
70+
expired = Date.now() - dateSet > ttl;
71+
72+
if (expired) {
73+
cacheWithTTL.remove(key);
74+
return;
75+
}
76+
}
77+
78+
return cache.get(key);
79+
};
80+
81+
cacheWithTTL.has = (key: K) => cacheWithTTL.get(key) !== undefined;
5982

6083
cacheWithTTL.set = (key: K, value: V, opts?: TTLDecoratorOptions & Parameters<T['set']>[2]) => {
6184
updateTTL(key, opts?.ttl);
@@ -68,9 +91,10 @@ export default function addTTL<
6891
};
6992

7093
cacheWithTTL.removeTTLFrom = (key: K) => {
71-
if (ttlTimers.has(key)) {
72-
clearTimeout(<number>ttlTimers.get(key));
73-
ttlTimers.delete(key);
94+
if (ttlStore.has(key) || dateStore.has(key)) {
95+
ttlStore.delete(key);
96+
dateStore.delete(key);
97+
7498
return true;
7599
}
76100

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

84-
removed.forEach((_, key) => {
85-
cacheWithTTL.removeTTLFrom(key);
86-
});
108+
if (filter == null) {
109+
ttlStore.clear();
110+
dateStore.clear();
111+
112+
} else {
113+
removed.forEach((_, key) => cacheWithTTL.removeTTLFrom(key));
114+
}
87115

88116
return removed;
89117
};
@@ -101,9 +129,11 @@ export default function addTTL<
101129
return cacheWithTTL;
102130

103131
function updateTTL(key: K, optionTTL?: number): void {
104-
if (optionTTL != null || ttl != null) {
105-
const time = optionTTL ?? ttl;
106-
ttlTimers.set(key, setTimeout(() => cacheWithTTL.remove(key), time));
132+
const time = optionTTL ?? ttl;
133+
134+
if (time != null) {
135+
ttlStore.set(key, time);
136+
dateStore.set(key, Date.now());
107137

108138
} else {
109139
cacheWithTTL.removeTTLFrom(key);

0 commit comments

Comments
 (0)