Skip to content

Commit

Permalink
Merge pull request #42 from akashchouhan16/dev
Browse files Browse the repository at this point in the history
🍁 valueOnly flag for get() and getM calls
  • Loading branch information
akashchouhan16 authored Nov 15, 2023
2 parents f921715 + bd3d0b5 commit 0b6c5f7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/config/cacheConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
15 changes: 10 additions & 5 deletions src/nodecache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -53,7 +55,10 @@ class NodeCache {
}

cacheConfig.cacheHit += 1
return cacheItem.value
if (cacheConfig.valueOnly) {
return cacheItem.value
}
return cacheItem
}

set(key, value, ttl) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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}`))
}
})

Expand Down
76 changes: 76 additions & 0 deletions test/nodecache.config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/nodecache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 0b6c5f7

Please sign in to comment.