Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions _src/lib/node_cache.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module.exports = class NodeCache extends EventEmitter
enableLegacyCallbacks: false
# max amount of keys that are being stored
maxKeys: -1
# auto replace oldest cache when maxKeys is reached
replaceOldestKey: false
, @options )

# generate functions with callbacks (legacy)
Expand Down Expand Up @@ -160,8 +162,18 @@ module.exports = class NodeCache extends EventEmitter
set: ( key, value, ttl )=>
# check if cache is overflowing
if (@options.maxKeys > -1 && @stats.keys >= @options.maxKeys)
_err = @_error( "ECACHEFULL" )
throw _err
# if replaceOldest then delete the oldest key
if(@options.replaceOldestKey)
oldestDate = Date.now()
oldestKey
for _key, _val of @data
if _val.d <= oldestDate
oldestDate = _val.d
oldestKey = _key
@del(oldestKey)
else
_err = @_error( "ECACHEFULL" )
throw _err

# force the data to string
if @options.forceString and not typeof value is "string"
Expand Down Expand Up @@ -632,6 +644,7 @@ module.exports = class NodeCache extends EventEmitter
oReturn =
t: livetime
v: if asClone then clone( value ) else value
d: now

# ## _unwrap
#
Expand Down
41 changes: 40 additions & 1 deletion _src/test/mocha_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ localCacheMaxKeys = new nodeCache({
maxKeys: 2
})

localCacheReplaceOldestKey = new nodeCache({
maxKeys: 2,
replaceOldestKey: true
})

localCacheTTL = new nodeCache({
stdTTL: 0.3,
checkperiod: 0
Expand Down Expand Up @@ -381,7 +386,41 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->
return

return

describe "replaceOldest: replaces oldest key when set to true", () ->
before () ->
state =
key1: randomString(10)
key2: randomString(10)
key3: randomString(10)
value1: randomString(10)
value2: randomString(10)
value3: randomString(10)
return
it "remove oldest key to allow new cache when replaceOldest is true", (done) ->
setTimeout(()->
localCacheReplaceOldestKey.set(state.key1, state.value1)
, 100)
setTimeout(()->
localCacheReplaceOldestKey.set(state.key2, state.value2)
, 200)
setTimeout(()->
localCacheReplaceOldestKey.set(state.key3, state.value3)
, 300)
setTimeout(() ->
# oldest key key2 from previous test should be removed
should([state.key2, state.key3]).eql(localCacheReplaceOldestKey.keys())
done()
return
, 500)
return
it "throw error when maxSize if full and replaceOldest is false", () ->
localCacheReplaceOldestKey.options.replaceOldestKey = false
(()=> localCacheReplaceOldestKey.set(state.key2, state.value2).should.throw({
name: "ECACHEFULL"
message: "Cache max keys amount exceeded"
}))
return
return
describe "correct and incorrect key types", () ->
describe "number", () ->
before () ->
Expand Down
11 changes: 11 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ declare namespace NodeCache {
* @memberof Options
*/
maxKeys?: number;

/**
* allow replacement of oldest key
*
* If `true`: set operations won't throw an error when max amount of keys is reached. It'll delete the oldest key to create space
* If `false`: set operation will throw an error when max amount of keys is reached.
*
* @type {boolean}
* @memberof Options
*/
replaceOldestKey?: boolean
}

interface Stats {
Expand Down
Loading