Skip to content

Commit

Permalink
feat: change cacher responses when the entry is not found. Resolves #824
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Jan 14, 2023
1 parent 03f598a commit 88d3b42
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 42 deletions.
76 changes: 47 additions & 29 deletions dev/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ let broker = new ServiceBroker({
type: "Memory",
options: {
max: 100,
ttl: 3
ttl: 3,
missingResponse: Symbol("MISSING")
}
}
});
Expand All @@ -26,44 +27,61 @@ broker.createService({
},
handler(ctx) {
this.logger.debug(kleur.yellow("Execute handler"));
return `Hello ${ctx.params.name}`;
return null; //`Hello ${ctx.params.name}`;
}
}
}
});

broker
.start()
.then(() => broker.call("greeter.hello", { name: "Moleculer" }))
.then(() => broker.call("greeter.hello", { name: "Moleculer" }))
.then(() => broker.call("greeter.hello", { name: "Moleculer", noCache: true }))
.then(() => broker.call("greeter.hello", { name: "Moleculer" }))
.then(() => broker.call("greeter.hello", { name: "Moleculer" }, { meta: { $cache: false } }))
(async () => {
await broker.start();

try {
broker.logger.info("---------------------", "(direct access)");
broker.logger.info("Value:", await broker.cacher.get('greeter.hello:name|"Moleculer"'));
broker.logger.info("---------------------", "(get, set)");
broker.logger.info("Res:", await broker.call("greeter.hello", { name: "Moleculer" }));
broker.logger.info("---------------------", "(found)");
broker.logger.info("Res:", await broker.call("greeter.hello", { name: "Moleculer" }));
broker.logger.info("---------------------", "(noCache)");
broker.logger.info(
"Res:",
await broker.call("greeter.hello", { name: "Moleculer", noCache: true })
);
broker.logger.info("---------------------", "(found)");
broker.logger.info("Res:", await broker.call("greeter.hello", { name: "Moleculer" }));
broker.logger.info("---------------------", "($cache: false)");
broker.logger.info(
"Res:",
await broker.call("greeter.hello", { name: "Moleculer" }, { meta: { $cache: false } })
);
broker.logger.info("---------------------", "(direct access)");
broker.logger.info("Value:", await broker.cacher.get('greeter.hello:name|"Moleculer"'));

.then(async () => {
/*for (let i = 0; i < 1000; i++) {
broker.cacher.set(`key-${i}`, i);
if (i % 10 == 0) {
broker.cacher.get(`key-${100}`);
broker.cacher.get(`key-${200}`);
broker.cacher.get(`key-${500}`);
broker.cacher.get(`key-${400}`);
broker.cacher.get(`key-${300}`);
}
}*/
broker.cacher.set(`key-${i}`, i);
if (i % 10 == 0) {
broker.cacher.get(`key-${100}`);
broker.cacher.get(`key-${200}`);
broker.cacher.get(`key-${500}`);
broker.cacher.get(`key-${400}`);
broker.cacher.get(`key-${300}`);
}
}*/

broker.logger.info("---------------------", "Cache entries:");
const keys = await broker.cacher.getCacheKeys();
broker.logger.info("Length:", keys.length);
broker.logger.info("keys:", keys);
})

.delay(35 * 1000)
.then(async () => {
broker.logger.info("=========================================");
const keys = await broker.cacher.getCacheKeys();
broker.logger.info("Length:", keys.length);
broker.logger.info("keys:", keys);
})
await broker.Promise.delay(35 * 1000);

.catch(err => broker.logger.error(err))
.then(() => broker.stop());
broker.logger.info("---------------------", "Cache entries:");
const keys2 = await broker.cacher.getCacheKeys();
broker.logger.info("Length:", keys2.length);
broker.logger.info("keys:", keys2);
} catch (err) {
broker.logger.error(err);
}
await broker.stop();
})();
8 changes: 5 additions & 3 deletions src/cachers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class Cacher {
this.opts = _.defaultsDeep(opts, {
ttl: null,
keygen: null,
maxParamsLength: null
maxParamsLength: null,
/** @type {any} Return with this if the key is missing in the cache */
missingResponse: undefined
});

/** @type {boolean} Flag indicating the connection status */
Expand Down Expand Up @@ -417,7 +419,7 @@ class Cacher {
}

return cachePromise.then(data => {
if (data != null) {
if (data !== this.opts.missingResponse) {
// Found in the cache! Don't call handler, return with the content
ctx.cachedResult = true;
return data;
Expand Down Expand Up @@ -460,7 +462,7 @@ class Cacher {
*/
middlewareWithoutLock(ctx, cacheKey, handler, opts) {
return this.get(cacheKey).then(content => {
if (content != null) {
if (content !== this.opts.missingResponse) {
// Found in the cache! Don't call handler, return with the content
ctx.cachedResult = true;
return content;
Expand Down
4 changes: 2 additions & 2 deletions src/cachers/memory-lru.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* moleculer
* Copyright (c) 2018 MoleculerJS (https://github.com/moleculerjs/moleculer)
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/

Expand Down Expand Up @@ -106,7 +106,7 @@ class MemoryLRUCacher extends BaseCacher {
} else {
timeEnd();
}
return this.broker.Promise.resolve(null);
return this.broker.Promise.resolve(this.opts.missingResponse);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/cachers/memory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* moleculer
* Copyright (c) 2018 MoleculerJS (https://github.com/moleculerjs/moleculer)
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/

Expand Down Expand Up @@ -95,7 +95,7 @@ class MemoryCacher extends BaseCacher {
this.metrics.increment(METRIC.MOLECULER_CACHER_EXPIRED_TOTAL);
this.cache.delete(key);
timeEnd();
return this.broker.Promise.resolve(null);
return this.broker.Promise.resolve(this.opts.missingResponse);
}
const res = this.clone ? this.clone(item.data) : item.data;
timeEnd();
Expand All @@ -104,7 +104,7 @@ class MemoryCacher extends BaseCacher {
} else {
timeEnd();
}
return this.broker.Promise.resolve(null);
return this.broker.Promise.resolve(this.opts.missingResponse);
}

/**
Expand Down Expand Up @@ -194,7 +194,7 @@ class MemoryCacher extends BaseCacher {
*/
getWithTTL(key) {
this.logger.debug(`GET ${key}`);
let data = null;
let data = this.opts.missingResponse;
let ttl = null;
if (this.cache.has(key)) {
this.logger.debug(`FOUND ${key}`);
Expand Down
6 changes: 3 additions & 3 deletions src/cachers/redis.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* moleculer
* Copyright (c) 2020 MoleculerJS (https://github.com/moleculerjs/moleculer)
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/

Expand Down Expand Up @@ -203,7 +203,7 @@ class RedisCacher extends BaseCacher {
}
}
timeEnd();
return null;
return this.opts.missingResponse;
});
}

Expand Down Expand Up @@ -335,7 +335,7 @@ class RedisCacher extends BaseCacher {
data = this.serializer.deserialize(data);
} catch (err) {
this.logger.error("Redis result parse error.", err, data);
data = null;
data = this.opts.missingResponse;
}
}
return { data, ttl };
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/json-extended.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class JSONExtSerializer extends BaseSerializer {
* @param {any} value
*/
reviver(key, value) {
if (typeof value === "string" && value.charAt(0) === "[") {
if (typeof value === "string" && value.charAt(0) === "[" && value.charAt(1) === "[") {
switch (value.slice(0, 6)) {
case PREFIX_BIGINT:
return BigInt(value.slice(6));
Expand Down

0 comments on commit 88d3b42

Please sign in to comment.