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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,26 @@ myCache.getStats();
*/
```

## Flush the stats (FLUSH STATS):

`myCache.flushStats()`

Flush the stats.

```js
myCache.flushStats();
myCache.getStats();
/*
{
keys: 0, // global key count
hits: 0, // global hit count
misses: 0, // global miss count
ksize: 0, // global key size count in approximately bytes
vsize: 0 // global value size count in approximately bytes
}
*/
```

## Close the cache:

`myCache.close()`
Expand Down Expand Up @@ -351,6 +371,16 @@ myCache.on( "flush", function(){
});
```

## flush_stats

Fired when the cache stats has been flushed.

```js
myCache.on( "flush_stats", function(){
// ... do something ...
});
```


## Breaking changes

Expand Down
34 changes: 33 additions & 1 deletion _src/lib/node_cache.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ module.exports = class NodeCache extends EventEmitter
#
take: ( key )=>
_ret = @get(key)
if (_ret?)
if (_ret?)
@del(key)
return _ret

Expand Down Expand Up @@ -489,6 +489,38 @@ module.exports = class NodeCache extends EventEmitter
@emit( "flush" )

return


# ## flushStats
#
# flush the stats
#
# **Example:**
#
# myCache.flushStats()
#
# myCache.flushStats()
# # {
# # hits: 0,
# # misses: 0,
# # keys: 0,
# # ksize: 0,
# # vsize: 0
# # }
#
flushStats: ()=>

# reset stats
@stats =
hits: 0
misses: 0
keys: 0
ksize: 0
vsize: 0

@emit( "flush_stats" )

return

# ## close
#
Expand Down
21 changes: 21 additions & 0 deletions _src/test/mocha_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,28 @@ describe "`#{pkg.name}@#{pkg.version}` on `node@#{process.version}`", () ->
{}.should.eql localCache.data
return
return

describe "flushStats", () ->
cache = null
before () ->
cache = new nodeCache()
return

it "set cache and flush stats value", () ->
key = randomString 10
value = randomString 10
res = cache.set key,value
true.should.eql res
1.should.eql cache.getStats().keys
cache.flushStats()
0.should.eql cache.getStats().keys
cache.get key
1.should.eql cache.getStats().hits
cache.get randomString 10
1.should.eql cache.getStats().misses

return
return

describe "many", () ->
before () ->
Expand Down