Skip to content

Commit 846954d

Browse files
authored
Added cache evict for delete Domains, incl. major improvements (#43)
1 parent 6fc1e2c commit 846954d

File tree

4 files changed

+233
-75
lines changed

4 files changed

+233
-75
lines changed

src/helpers/cache/index.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ export default class Cache {
3636
this.#workerManager.setOnCacheUpdates((updates) =>
3737
this.#handleCacheUpdates(updates));
3838

39+
this.#workerManager.setOnCacheDeletions((deletions) =>
40+
this.#handleCacheDeletions(deletions));
41+
3942
this.#workerManager.setOnCacheVersionRequest((domainId) =>
4043
this.#handleCacheVersionRequest(domainId));
4144

45+
this.#workerManager.setOnCachedDomainIdsRequest(() =>
46+
this.#handleCachedDomainIdsRequest());
47+
4248
this.#workerManager.setOnError((error) => {
4349
Logger.error('Cache worker error:', error);
4450
});
@@ -47,10 +53,8 @@ export default class Cache {
4753
}
4854

4955
async stopScheduledUpdates() {
50-
if (this.#workerManager) {
51-
await this.#workerManager.stop();
52-
this.#workerManager = null;
53-
}
56+
await this.#workerManager?.stop();
57+
this.#workerManager = null;
5458
}
5559

5660
async #updateCache(domain) {
@@ -62,7 +66,6 @@ export default class Cache {
6266

6367
this.#set(domain._id, {
6468
data: reduceSnapshot(result.data.domain),
65-
lastUpdate: domain.lastUpdate,
6669
version: result.data.domain.version
6770
});
6871
}
@@ -71,19 +74,25 @@ export default class Cache {
7174
for (const update of updates) {
7275
this.#set(update.domainId, {
7376
data: update.data,
74-
lastUpdate: update.lastUpdate,
7577
version: update.version
7678
});
7779
}
7880
}
7981

82+
#handleCacheDeletions(deletions) {
83+
for (const domainId of deletions) {
84+
this.#instance.delete(String(domainId));
85+
}
86+
}
87+
8088
#handleCacheVersionRequest(domainId) {
8189
const cached = this.#instance.get(String(domainId));
82-
const cachedVersion = cached?.lastUpdate || null;
83-
84-
if (this.#workerManager) {
85-
this.#workerManager.sendCacheVersionResponse(domainId, cachedVersion);
86-
}
90+
this.#workerManager.sendCacheVersionResponse(domainId, cached?.version);
91+
}
92+
93+
#handleCachedDomainIdsRequest() {
94+
const domainIds = Array.from(this.#instance.keys());
95+
this.#workerManager.sendCachedDomainIdsResponse(domainIds);
8796
}
8897

8998
#set(key, value) {

src/helpers/cache/worker-manager.js

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ export const EVENT_TYPE = {
1212
STOPPED: 'stopped',
1313
READY: 'ready',
1414
CACHE_UPDATES: 'cache-updates',
15+
CACHE_DELETIONS: 'cache-deletions',
1516
REQUEST_CACHE_VERSION: 'request-cache-version',
1617
CACHE_VERSION_RESPONSE: 'cache-version-response',
18+
REQUEST_CACHED_DOMAIN_IDS: 'request-cached-domain-ids',
19+
CACHED_DOMAIN_IDS_RESPONSE: 'cached-domain-ids-response',
1720
ERROR: 'error'
1821
};
1922

@@ -30,53 +33,69 @@ export class CacheWorkerManager {
3033
constructor(options = {}) {
3134
this.worker = null;
3235
this.status = STATUS_TYPE.STOPPED;
36+
this.onCacheUpdates = null;
37+
this.onCacheDeletions = null;
38+
this.onCachedDomainIdsRequest = null;
39+
this.onError = null;
3340
this.options = {
3441
interval: this.DEFAULT_INTERVAL,
3542
...options
3643
};
37-
this.onCacheUpdates = null;
38-
this.onError = null;
44+
}
45+
46+
#buildEvents(resolve) {
47+
return new Map([
48+
[EVENT_TYPE.READY, () => {
49+
this.worker.postMessage({ type: EVENT_TYPE.START });
50+
}],
51+
[EVENT_TYPE.STARTED, () => {
52+
this.status = STATUS_TYPE.RUNNING;
53+
resolve();
54+
}],
55+
[EVENT_TYPE.STOPPED, () => {
56+
this.status = STATUS_TYPE.STOPPED;
57+
}],
58+
[EVENT_TYPE.CACHE_UPDATES, (message) => {
59+
if (this.onCacheUpdates) {
60+
this.onCacheUpdates(message.updates);
61+
}
62+
}],
63+
[EVENT_TYPE.CACHE_DELETIONS, (message) => {
64+
if (this.onCacheDeletions) {
65+
this.onCacheDeletions(message.deletions);
66+
}
67+
}],
68+
[EVENT_TYPE.REQUEST_CACHE_VERSION, (message) => {
69+
if (this.onCacheVersionRequest) {
70+
this.onCacheVersionRequest(message.domainId);
71+
}
72+
}],
73+
[EVENT_TYPE.REQUEST_CACHED_DOMAIN_IDS, () => {
74+
if (this.onCachedDomainIdsRequest) {
75+
this.onCachedDomainIdsRequest();
76+
}
77+
}],
78+
[EVENT_TYPE.ERROR, (message) => {
79+
if (this.onError) {
80+
this.onError(new Error(message.error));
81+
}
82+
}]
83+
]);
3984
}
4085

4186
start() {
4287
return new Promise((resolve, reject) => {
4388
const workerPath = join(__dirname, 'worker.js');
89+
const eventHandlers = this.#buildEvents(resolve);
90+
4491
this.worker = new Worker(workerPath, {
4592
workerData: this.options
4693
});
4794

4895
this.worker.on('message', (message) => {
49-
switch (message.type) {
50-
case EVENT_TYPE.READY:
51-
this.worker.postMessage({ type: EVENT_TYPE.START });
52-
break;
53-
54-
case EVENT_TYPE.STARTED:
55-
this.status = STATUS_TYPE.RUNNING;
56-
resolve();
57-
break;
58-
59-
case EVENT_TYPE.STOPPED:
60-
this.status = STATUS_TYPE.STOPPED;
61-
break;
62-
63-
case EVENT_TYPE.CACHE_UPDATES:
64-
if (this.onCacheUpdates) {
65-
this.onCacheUpdates(message.updates);
66-
}
67-
break;
68-
69-
case EVENT_TYPE.REQUEST_CACHE_VERSION:
70-
if (this.onCacheVersionRequest) {
71-
this.onCacheVersionRequest(message.domainId);
72-
}
73-
break;
74-
75-
case EVENT_TYPE.ERROR:
76-
if (this.onError) {
77-
this.onError(new Error(message.error));
78-
}
79-
break;
96+
const handler = eventHandlers.get(message.type);
97+
if (handler) {
98+
handler(message);
8099
}
81100
});
82101

@@ -129,8 +148,6 @@ export class CacheWorkerManager {
129148
};
130149

131150
this.worker.on('message', onMessage);
132-
133-
// Send stop message
134151
this.worker.postMessage({ type: EVENT_TYPE.STOP });
135152
});
136153
}
@@ -143,10 +160,18 @@ export class CacheWorkerManager {
143160
this.onCacheUpdates = callback;
144161
}
145162

163+
setOnCacheDeletions(callback) {
164+
this.onCacheDeletions = callback;
165+
}
166+
146167
setOnCacheVersionRequest(callback) {
147168
this.onCacheVersionRequest = callback;
148169
}
149170

171+
setOnCachedDomainIdsRequest(callback) {
172+
this.onCachedDomainIdsRequest = callback;
173+
}
174+
150175
sendCacheVersionResponse(domainId, cachedVersion) {
151176
if (this.worker && this.status === STATUS_TYPE.RUNNING) {
152177
this.worker.postMessage({
@@ -157,6 +182,15 @@ export class CacheWorkerManager {
157182
}
158183
}
159184

185+
sendCachedDomainIdsResponse(domainIds) {
186+
if (this.worker && this.status === STATUS_TYPE.RUNNING) {
187+
this.worker.postMessage({
188+
type: EVENT_TYPE.CACHED_DOMAIN_IDS_RESPONSE,
189+
domainIds
190+
});
191+
}
192+
}
193+
160194
setOnError(callback) {
161195
this.onError = callback;
162196
}

0 commit comments

Comments
 (0)