Skip to content
Merged
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,29 @@ console.log( mykeys );

```

## Has key (HAS)

`myCache.has( key, [callback] )`

Returns boolean indicating if the key is cached.

```js
// async
myCache.has( 'myKey', function( err, exists ){
if( !err ){
console.log( exists );
// true
}
});

// sync
exists = myCache.has( 'myKey' );

console.log( exists );
// true

```

## Statistics (STATS):

`myCache.getStats()`
Expand Down
25 changes: 24 additions & 1 deletion _src/lib/node_cache.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ module.exports = class NodeCache extends EventEmitter
cb( null, undefined ) if cb?
return undefined


# ## mget
#
# get multiple cached keys at once and change the stats
Expand Down Expand Up @@ -393,6 +392,30 @@ module.exports = class NodeCache extends EventEmitter
cb( null, _keys )if cb?
return _keys

# ## has
#
# Check if a key is cached
#
# **Parameters:**
#
# * `key` ( String | Number ): cache key to check the ttl value
# * `[cb]` ( Function ): Callback function
#
# **Return**
#
# ( Boolean ): A boolean that indicates if the key is cached
#
# **Example:**
#
# _exists = myCache.has('myKey')
#
# # true
#
has: ( key, cb )=>
_exists = @data[ key ]? and @_check( key, @data[ key ] )
cb( null, _exists )if cb?
return _exists

# ## getStats
#
# get the stats
Expand Down
72 changes: 72 additions & 0 deletions _src/test/mocha_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->
done()
return

it "has a key", (done) ->
localCache.has state.key, (err, res) ->
should.not.exist err
res.should.eql true
done()
return

it "doesn not have a key", (done) ->
localCache.has 'non existing key', (err, res) ->
should.not.exist err
res.should.eql false
done()
return

it "get key names", (done) ->
localCache.keys (err, res) ->
state.n++
Expand Down Expand Up @@ -253,6 +267,16 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->
[state.key].should.eql res
return

it "has key", () ->
res = localCache.has(state.key)
res.should.eql true
return

it "does not have key", () ->
res = localCache.has('non existing key')
res.should.eql false
return

it "delete an undefined key", () ->
count = localCache.del "xxx"
0.should.eql count
Expand Down Expand Up @@ -1148,9 +1172,52 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->
key3: "k3_#{randomString 20}"
key4: "k4_#{randomString 20}"
key5: "k5_#{randomString 20}"
key6: "k6_#{randomString 20}"
now: Date.now()
state.keys = [state.key1, state.key2, state.key3, state.key4, state.key5]
return

describe "has validates expired ttl", () ->
it "set a key with ttl", () ->
localCacheTTL.set state.key6, state.val, 0.7, (err, res) ->
should.not.exist err
true.should.eql res
return

it "check this key immediately", () ->
localCacheTTL.has state.key6, (err, res) ->
should.not.exist err
res.should.eql true
return
return

it "before it times out", (done) ->
setTimeout(() ->
state.n++
res = localCacheTTL.has state.key6
res.should.eql true
localCacheTTL.get state.key6, (err, res) ->
should.not.exist err
state.val.should.eql res
done()
return
, 20)
return

it "and after it timed out", (done) ->
setTimeout(() ->
res = localCacheTTL.has state.key6
res.should.eql false

state.n++
localCacheTTL.get state.key6, (err, res) ->
should.not.exist err
should(res).be.undefined()
done()
return
return
, 800)
return

it "set a key with ttl", () ->
localCache.set state.key1, state.val, 0.7, (err, res) ->
Expand All @@ -1172,6 +1239,8 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->
it "before it times out", (done) ->
setTimeout(() ->
state.n++
res = localCache.has state.key1
res.should.eql true
localCache.get state.key1, (err, res) ->
should.not.exist err
state.val.should.eql res
Expand All @@ -1182,6 +1251,9 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->

it "and after it timed out", (done) ->
setTimeout(() ->
res = localCache.has state.key1
res.should.eql false

ts = localCache.getTtl state.key1
should.not.exist ts

Expand Down
9 changes: 9 additions & 0 deletions _src/test/typedefinition_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ interface TypeSample {
result = cache.keys(cb);
}

{
let cache: NodeCache;
let key: string | number;
let cb: Callback<boolean>;
let result: boolean;
result = cache.has(key);
result = cache.has(key, cb);
}

{
let cache: NodeCache;
let result: Stats;
Expand Down
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ declare class NodeCache extends events.EventEmitter implements NodeCache.NodeCac
*/
keys(cb?: Callback<string[]>): string[];

/**
* Check if a key is cached
* @param key cache key to check
* @param cb Callback function
* @returns Boolean indicating if the key is cached or not
*/
has(key: Key, cb?: Callback<boolean>): boolean;

/**
* get the stats
*
Expand Down