diff --git a/src/config/cacheConfig.js b/src/config/cacheConfig.js index ce059bb..f485f61 100644 --- a/src/config/cacheConfig.js +++ b/src/config/cacheConfig.js @@ -15,10 +15,15 @@ * stdTTL: To configure a default ttl with every key value pair saved to the cache instance. Accepts numeric data in ms (>0). Setting stdTTL, will overwrite default ttl = 0 to stdTTL value. stdTTL value of 0 implies ttl is infinite and the key-value will never expire from cache. + +* valueOnly: To configure the response from the get() and getM() API. + Accepts boolean. By default, it's set to true and get calls will only the associated value for a key. + When set to false, get() or its derivative getM() call will return {value, ttl} object for a particular key. * */ let cacheConfig = { forceString: true, + valueOnly: true, maxKeys: -1, stdTTL: 0, diff --git a/src/nodecache.js b/src/nodecache.js index 40cc70f..cb3991c 100644 --- a/src/nodecache.js +++ b/src/nodecache.js @@ -15,11 +15,13 @@ class NodeCache { this.cache = {} this.logger = new Logger({ ...options }) - let { forceString, maxKeys, stdTTL } = options + let { forceString, maxKeys, stdTTL, valueOnly } = options + cacheConfig.valueOnly = valueOnly !== undefined && typeof valueOnly === "boolean" ? valueOnly : cacheConfig.valueOnly cacheConfig.forceString = forceString !== undefined && typeof forceString === "boolean" ? forceString : cacheConfig.forceString cacheConfig.maxKeys = maxKeys !== undefined && typeof maxKeys === "number" && maxKeys > 0 ? maxKeys : cacheConfig.maxKeys cacheConfig.stdTTL = (stdTTL && typeof stdTTL === "number" && stdTTL >= 0) ? stdTTL : cacheConfig.stdTTL + this.config = { ...cacheConfig } if (isMainThread) { this.worker = new Worker(path.join(__dirname, "/worker/worker.js")) @@ -53,7 +55,10 @@ class NodeCache { } cacheConfig.cacheHit += 1 - return cacheItem.value + if (cacheConfig.valueOnly) { + return cacheItem.value + } + return cacheItem } set(key, value, ttl) { @@ -105,7 +110,7 @@ class NodeCache { if (!Array.isArray(values)) { throw new Error(CONSTANTS.INVALID_SETM_INPUT) } else if (values.length === 0) { - return false + return [false] } if (cacheConfig.maxKeys > 0 && cacheConfig.maxKeys <= values.length) { @@ -115,7 +120,7 @@ class NodeCache { let responseObject = [] for (let data of values) { let { key, value, ttl } = data - responseObject.push(this.set(key, value, ttl ? ttl : 0)) + responseObject.push(this.set(key, value, ttl)) } return responseObject @@ -157,7 +162,7 @@ class NodeCache { } resolve(this.cache) } catch (error) { - reject(`Refresh failed to update cache: ${error.message}`) + reject(new Error(`Refresh failed to update cache: ${error.message}`)) } }) diff --git a/test/nodecache.config.test.js b/test/nodecache.config.test.js index a5662b4..362ae03 100644 --- a/test/nodecache.config.test.js +++ b/test/nodecache.config.test.js @@ -244,6 +244,82 @@ describe("NodeCache params for instance config", () => { expect(cache.cache[6]).toBeUndefined() }) }) + describe("NodeCache valueOnly configurations: Default (true) case", () => { + let cache + beforeEach(() => { + cache = new NodeCache({ + // valueOnly: true // By default valueOnly is set to true -> backward compatibility with older npm versions. + }) + }) + afterEach(() => { + cache.close() + }) + + test("When not valueOnly flag with the instance", () => { + expect(cache.config["valueOnly"]).toEqual(true) + }) + + test("When using Get() and Set() calls - true case", () => { + cache.set(151123, { _id: "nC11Hque81", value: "test-data" }) + expect(cache.get(151123)).toStrictEqual({ _id: "nC11Hque81", value: "test-data" }) + + cache.set(1511232, { _id: "nC11Hque82", value: "test-data-2" }, 60 * 1000) + expect(cache.get(1511232)).toStrictEqual({ _id: "nC11Hque82", value: "test-data-2" }) + }) + + test("When using getM() and setM() calls - true case", () => { + const input = [{ key: 1511235, value: "test-data-5" }, { key: 1511236, value: "test-data-6", ttl: 2 * 60 * 1000 }] + cache.setM(input) + + const responses = cache.getM([1511235, 1511236]) + responses.forEach(response => { + expect(response).toStrictEqual(expect.any(String)) + }) + }) + }) + + describe("NodeCache valueOnly configurations: New (false) case", () => { + let cache + beforeEach(() => { + cache = new NodeCache({ + valueOnly: false + }) + }) + afterEach(() => { + cache.close() + }) + + test("When not valueOnly flag with the instance", () => { + expect(cache.config["valueOnly"]).toEqual(false) + }) + + test("When using get() and set() calls - false case", () => { + cache.set(1511233, { _id: "nC11Hque83", value: "test-data-3" }) + expect(cache.get(1511233)).toStrictEqual({ + value: expect.any(Object), + ttl: expect.any(Number) + }) + + cache.set(1511234, { _id: "nC11Hque84", value: "test-data-4" }, 60 * 1000) + expect(cache.get(1511234)).toStrictEqual({ + value: expect.any(Object), + ttl: expect.any(Number) + }) + }) + + test("When using getM() and setM() calls - false case", () => { + const input = [{ key: 1511235, value: "test-data-5" }, { key: 1511236, value: "test-data-6", ttl: 2 * 60 * 1000 }] + cache.setM(input) + + const responses = cache.getM([1511235, 1511236]) + responses.forEach(response => { + expect(response).toStrictEqual({ + value: expect.any(String), + ttl: expect.any(Number) + }) + }) + }) + }) describe("NodeCache Logger configurations for the instance (type: custom)", () => { let cache diff --git a/test/nodecache.test.js b/test/nodecache.test.js index 1e42a3b..d53d2e7 100644 --- a/test/nodecache.test.js +++ b/test/nodecache.test.js @@ -149,7 +149,7 @@ describe("NodeCache public APIs", () => { }) test("NodeCache::setM with empty array", () => { - expect(cache.setM([])).toEqual(false) + expect(cache.setM([])).toEqual([false]) }) test("NodeCache::setM with valid input", () => {