Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-cache

Package Overview
Dependencies
Maintainers
2
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-cache - npm Package Compare versions

Comparing version 4.2.1 to 5.0.0-alpha.0

36

_src/test/typedefinition_test.ts

@@ -0,4 +1,3 @@

import NodeCache = require("../../");
import NodeCache = require('../../');
import Options = NodeCache.Options;

@@ -24,6 +23,5 @@ import Stats = NodeCache.Stats;

let key: string;
let cb: Callback<TypeSample>;
let result: TypeSample | undefined;
result = cache.get<TypeSample>(key);
result = cache.get<TypeSample>(key, cb);
result = cache.get<TypeSample>(key);
}

@@ -34,6 +32,5 @@

let keys: string[];
let cb: Callback<{ [key: string]: TypeSample }>;
let result: { [key: string]: TypeSample };
result = cache.mget<TypeSample>(keys);
result = cache.mget<TypeSample>(keys, cb);
result = cache.mget<TypeSample>(keys);
}

@@ -46,8 +43,7 @@

let ttl: number | string;
let cb: Callback<boolean>;
let result: boolean;
result = cache.set<TypeSample>(key, value);
result = cache.set<TypeSample>(key, value, ttl);
result = cache.set<TypeSample>(key, value, ttl, cb);
result = cache.set<TypeSample>(key, value, cb);
result = cache.set<TypeSample>(key, value, ttl);
result = cache.set<TypeSample>(key, value);
}

@@ -58,6 +54,5 @@

let keys: string | string[];
let cb: Callback<number>;
let result: number;
result = cache.del(keys);
result = cache.del(keys, cb);
result = cache.del(keys);
}

@@ -69,8 +64,7 @@

let ttl: number;
let cb: Callback<boolean>;
let result: boolean;
result = cache.ttl(key);
result = cache.ttl(key, ttl);
result = cache.ttl(key, ttl, cb);
result = cache.ttl(key, cb);
result = cache.ttl(key, ttl);
result = cache.ttl(key);
}

@@ -80,6 +74,5 @@

let cache: NodeCache;
let cb: Callback<string[]>;
let result: string[];
result = cache.keys();
result = cache.keys(cb);
result = cache.keys();
}

@@ -89,2 +82,10 @@

let cache: NodeCache;
let key: string | number;
let result: boolean;
result = cache.has(key);
result = cache.has(key);
}
{
let cache: NodeCache;
let result: Stats;

@@ -96,9 +97,6 @@ result = cache.getStats();

let cache: NodeCache;
let cb: Callback<Boolean>;
let key: string;
let number: number;
let result1: number | undefined;
let result2: Boolean;
result1 = cache.getTtl(key);
result2 = cache.getTtl(key, cb);
}

@@ -105,0 +103,0 @@

@@ -16,2 +16,8 @@ // Type definitions for node-cache 4.1

type ValueSetItem<T = any> = {
key: Key;
val: T;
ttl?: number;
}
declare namespace NodeCache {

@@ -73,2 +79,11 @@ interface NodeCache {

/**
* set multiple cached keys at once and change the stats
*
* @param keyValueSet an array of object which includes key,value and ttl
*/
mset<T>(
keyValueSet: ValueSetItem<T>[],
): boolean;
/**
* remove keys

@@ -139,2 +154,3 @@ * @param keys cache key to delete or a array of cache keys

objectValueSize?: number;
promiseValueSize?: number;
arrayValueSize?: number;

@@ -146,2 +162,4 @@ stdTTL?: number;

deleteOnExpire?: boolean;
enableLegacyCallbacks?: boolean;
maxKeys?: number;
}

@@ -231,2 +249,11 @@

/**
* set multiple cached keys at once and change the stats
*
* @param keyValueSet an array of object which includes key,value and ttl
*/
mset<T>(
keyValueSet: ValueSetItem<T>[],
): boolean;
/**
* remove keys

@@ -274,2 +301,10 @@ * @param keys cache key to delete or a array of cache keys

/**
* 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

@@ -292,2 +327,2 @@ *

export = NodeCache;
export = NodeCache;
/*
* node-cache 4.2.1 ( 2019-07-24 )
* node-cache 5.0.0-alpha.0 ( 2019-09-18 )
* https://github.com/mpneuried/nodecache

@@ -15,4 +15,4 @@ *

exports.version = '4.2.1';
exports.version = '5.0.0-alpha.0';
}).call(this);
/*
* node-cache 4.2.1 ( 2019-07-24 )
* node-cache 5.0.0-alpha.0 ( 2019-09-18 )
* https://github.com/mpneuried/nodecache

@@ -11,23 +11,7 @@ *

(function() {
// lodash requires
var EventEmitter, NodeCache, _assignIn, _isArray, _isFunction, _isNumber, _isObject, _isString, _size, _template, clone,
var EventEmitter, NodeCache, clone,
splice = [].splice,
boundMethodCheck = function(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new Error('Bound instance method accessed before binding'); } },
indexOf = [].indexOf;
_assignIn = require("lodash/assignIn");
_isArray = require("lodash/isArray");
_isString = require("lodash/isString");
_isFunction = require("lodash/isFunction");
_isNumber = require("lodash/isNumber");
_isObject = require("lodash/isObject");
_size = require("lodash/size");
_template = require("lodash/template");
clone = require("clone");

@@ -49,10 +33,6 @@

// * `key` ( String | Number ): cache key
// * `[cb]` ( Function ): Callback function
// * `[errorOnMissing=false]` ( Boolean ) return a error to the `cb` or throw it if no `cb` is used. Otherwise the get will return `undefined` on a miss.
// **Example:**
// myCache.get "myKey", ( err, val )->
// console.log( err, val )
// return
// myCache.get "myKey", ( err, val )

@@ -67,9 +47,6 @@ this.get = this.get.bind(this);

// * `keys` ( String|Number[] ): an array of keys
// * `[cb]` ( Function ): Callback function
// **Example:**
// myCache.mget [ "foo", "bar" ], ( err, val )->
// console.log( err, val )
// return
// myCache.mget [ "foo", "bar" ]

@@ -86,13 +63,31 @@ this.mget = this.mget.bind(this);

// * `[ ttl ]` ( Number | String ): ( optional ) The time to live in seconds.
// * `[cb]` ( Function ): Callback function
// **Example:**
// myCache.set "myKey", "my_String Value", ( err, success )->
// console.log( err, success )
// myCache.set "myKey", "my_String Value"
// myCache.set "myKey", "my_String Value", "10", ( err, success )->
// console.log( err, success )
// myCache.set "myKey", "my_String Value", "10"
this.set = this.set.bind(this);
// ## mset
// set multiple keys at once
// **Parameters:**
// * `keyValueSet` ( Object[] ): an array of object which includes key,value and ttl
// **Example:**
// myCache.mset(
// [
// {
// key: "myKey",
// val: "myValue",
// ttl: [ttl in seconds]
// }
// ])
this.mset = this.mset.bind(this);
// ## del

@@ -105,3 +100,2 @@

// * `keys` ( String | Number | String|Number[] ): cache key to delete or a array of cache keys
// * `[cb]` ( Function ): Callback function

@@ -114,7 +108,4 @@ // **Return**

// myCache.del( "myKey" )
// myCache.del( "myKey" )
// myCache.del( "myKey", ( err, delCount )->
// console.log( err, delCount )
// return
this.del = this.del.bind(this);

@@ -131,3 +122,2 @@ // ## ttl

// * `ttl` ( Number ): ( optional -> options.stdTTL || 0 ) The time to live in seconds
// * `[cb]` ( Function ): Callback function

@@ -140,6 +130,5 @@ // **Return**

// myCache.ttl( "myKey" ) // will set ttl to default ttl
// myCache.ttl( "myKey" ) // will set ttl to default ttl
// myCache.ttl( "myKey", 1000, ( err, keyFound )->
// console.log( err, success )
// myCache.ttl( "myKey", 1000 )

@@ -154,3 +143,2 @@ this.ttl = this.ttl.bind(this);

// * `key` ( String | Number ): cache key to check the ttl value
// * `[cb]` ( Function ): Callback function

@@ -163,8 +151,4 @@ // **Return**

// ts = myCache.getTtl( "myKey" )
// myCache.getTtl( "myKey" )
// myCache.getTtl( "myKey",( err, ttl )->
// console.log( err, ttl )
// return
this.getTtl = this.getTtl.bind(this);

@@ -175,6 +159,2 @@ // ## keys

// **Parameters:**
// * `[cb]` ( Function ): Callback function
// **Return**

@@ -191,2 +171,21 @@

this.keys = this.keys.bind(this);
// ## has
// Check if a key is cached
// **Parameters:**
// * `key` ( String | Number ): cache key to check the ttl value
// **Return**
// ( Boolean ): A boolean that indicates if the key is cached
// **Example:**
// _exists = myCache.has('myKey')
// # true
this.has = this.has.bind(this);
// ## getStats

@@ -277,3 +276,3 @@

// module options
this.options = _assignIn({
this.options = Object.assign({
// convert all elements to string

@@ -294,4 +293,33 @@ forceString: false,

// whether values should be deleted automatically at expiration
deleteOnExpire: true
deleteOnExpire: true,
// enable legacy callbacks
enableLegacyCallbacks: false,
// max amount of keys that are being stored
maxKeys: -1
}, this.options);
// generate functions with callbacks (legacy)
if (this.options.enableLegacyCallbacks) {
console.warn("WARNING! node-cache legacy callback support will drop in v6.x");
["get", "mget", "set", "del", "ttl", "getTtl", "keys", "has"].forEach((methodKey) => {
var oldMethod;
// reference real function
oldMethod = this[methodKey];
this[methodKey] = function(...args) {
var cb, err, ref, res;
ref = args, [...args] = ref, [cb] = splice.call(args, -1);
// return a callback if cb is defined and a function
if (typeof cb === "function") {
try {
res = oldMethod(...args);
cb(null, res);
} catch (error1) {
err = error1;
cb(err);
}
} else {
return oldMethod(...args, cb);
}
};
});
}
// statistics container

@@ -312,18 +340,8 @@ this.stats = {

get(key, cb, errorOnMissing) {
var _err, _ret, err;
get(key) {
var _ret, err;
boundMethodCheck(this, NodeCache);
// handle passing in errorOnMissing without cb
if (typeof cb === "boolean" && arguments.length === 2) {
errorOnMissing = cb;
cb = void 0;
}
// handle invalid key types
if ((err = this._isInvalidKey(key)) != null) {
if (cb != null) {
cb(err);
return;
} else {
throw err;
}
throw err;
}

@@ -334,23 +352,7 @@ // get data and incremet stats

_ret = this._unwrap(this.data[key]);
if (cb != null) {
// return data
cb(null, _ret);
}
// return data
return _ret;
} else {
// if not found return a error
// if not found return undefined
this.stats.misses++;
if (this.options.errorOnMissing || errorOnMissing) {
_err = this._error("ENOTFOUND", {
key: key
}, cb);
if (_err != null) {
throw _err;
}
return;
} else {
if (cb != null) {
cb(null, void 0);
}
}
return void 0;

@@ -360,12 +362,9 @@ }

mget(keys, cb) {
mget(keys) {
var _err, err, i, key, len, oRet;
boundMethodCheck(this, NodeCache);
// convert a string to an array of one key
if (!_isArray(keys)) {
if (!Array.isArray(keys)) {
_err = this._error("EKEYSTYPE");
if (cb != null) {
cb(_err);
}
return _err;
throw _err;
}

@@ -378,8 +377,3 @@ // define return

if ((err = this._isInvalidKey(key)) != null) {
if (cb != null) {
cb(err);
return;
} else {
throw err;
}
throw err;
}

@@ -395,19 +389,20 @@ // get data and increment stats

}
if (cb != null) {
// return all found keys
cb(null, oRet);
}
// return all found keys
return oRet;
}
set(key, value, ttl, cb) {
var err, existent;
set(key, value, ttl) {
var _err, err, existent;
boundMethodCheck(this, NodeCache);
// check if cache is overflowing
if (this.options.maxKeys > -1 && this.stats.keys >= this.options.maxKeys) {
_err = this._error("ECACHEFULL");
throw _err;
}
// force the data to string
if (this.options.forceString && !_isString(value)) {
if (this.options.forceString && !typeof value === "string") {
value = JSON.stringify(value);
}
// remap the arguments if `ttl` is not passed
if (arguments.length === 3 && _isFunction(ttl)) {
cb = ttl;
if (arguments.length === 3 && typeof ttl === "function") {
ttl = this.options.stdTTL;

@@ -417,8 +412,3 @@ }

if ((err = this._isInvalidKey(key)) != null) {
if (cb != null) {
cb(err);
return;
} else {
throw err;
}
throw err;
}

@@ -441,6 +431,34 @@ // internal helper variables

this.emit("set", key, value);
if (cb != null) {
// return true
cb(null, true);
// return true
return true;
}
mset(keyValueSet) {
var _err, err, i, j, key, keyValuePair, len, len1, ttl, val;
boundMethodCheck(this, NodeCache);
// check if cache is overflowing
if (this.options.maxKeys > -1 && this.stats.keys + keyValueSet.length >= this.options.maxKeys) {
_err = this._error("ECACHEFULL");
throw _err;
}
// loop over keyValueSet to validate key and ttl
for (i = 0, len = keyValueSet.length; i < len; i++) {
keyValuePair = keyValueSet[i];
({key, val, ttl} = keyValuePair);
// check if there is ttl and it's a number
if (ttl && typeof ttl !== "number") {
_err = this._error("ETTLTYPE");
throw _err;
}
// handle invalid key types
if ((err = this._isInvalidKey(key)) != null) {
throw err;
}
}
for (j = 0, len1 = keyValueSet.length; j < len1; j++) {
keyValuePair = keyValueSet[j];
({key, val, ttl} = keyValuePair);
this.set(key, val, ttl);
}
return true;

@@ -453,3 +471,3 @@ }

// convert keys to an array of itself
if (!_isArray(keys)) {
if (!Array.isArray(keys)) {
keys = [keys];

@@ -462,8 +480,3 @@ }

if ((err = this._isInvalidKey(key)) != null) {
if (cb != null) {
cb(err);
return;
} else {
throw err;
}
throw err;
}

@@ -482,10 +495,4 @@ // only delete if existent

this.emit("del", key, oldVal.v);
} else {
// if the key has not been found return an error
this.stats.misses++;
}
}
if (cb != null) {
cb(null, delCount);
}
return delCount;

@@ -511,5 +518,2 @@ }

if (!key) {
if (cb != null) {
cb(null, false);
}
return false;

@@ -519,8 +523,3 @@ }

if ((err = this._isInvalidKey(key)) != null) {
if (cb != null) {
cb(err);
return;
} else {
throw err;
}
throw err;
}

@@ -535,11 +534,5 @@ // check for existant data and update the ttl value

}
if (cb != null) {
cb(null, true);
}
return true;
} else {
if (cb != null) {
// return false if key has not been found
cb(null, false);
}
// return false if key has not been found
return false;

@@ -553,5 +546,2 @@ }

if (!key) {
if (cb != null) {
cb(null, void 0);
}
return void 0;

@@ -561,8 +551,3 @@ }

if ((err = this._isInvalidKey(key)) != null) {
if (cb != null) {
cb(err);
return;
} else {
throw err;
}
throw err;
}

@@ -572,11 +557,5 @@ // check for existant data and update the ttl value

_ttl = this.data[key].t;
if (cb != null) {
cb(null, _ttl);
}
return _ttl;
} else {
if (cb != null) {
// return undefined if key has not been found
cb(null, void 0);
}
// return undefined if key has not been found
return void 0;

@@ -586,12 +565,16 @@ }

keys(cb) {
keys() {
var _keys;
boundMethodCheck(this, NodeCache);
_keys = Object.keys(this.data);
if (cb != null) {
cb(null, _keys);
}
return _keys;
}
has(key) {
var _exists;
boundMethodCheck(this, NodeCache);
_exists = (this.data[key] != null) && this._check(key, this.data[key]);
return _exists;
}
getStats() {

@@ -660,4 +643,4 @@ boundMethodCheck(this, NodeCache);

if (data.t !== 0 && data.t < Date.now()) {
_retval = false;
if (this.options.deleteOnExpire) {
_retval = false;
this.del(key);

@@ -731,3 +714,3 @@ }

_getKeyLength(key) {
return key.length;
return key.toString().length;
}

@@ -737,3 +720,3 @@

boundMethodCheck(this, NodeCache);
if (_isString(value)) {
if (typeof value === "string") {
// if the value is a String get the real length

@@ -744,6 +727,6 @@ return value.length;

return JSON.stringify(value).length;
} else if (_isArray(value)) {
} else if (Array.isArray(value)) {
// if the data is an Array multiply each element with a defined default length
return this.options.arrayValueSize * value.length;
} else if (_isNumber(value)) {
} else if (typeof value === "number") {
return 8;

@@ -754,5 +737,7 @@ } else if (typeof (value != null ? value.then : void 0) === "function") {

return this.options.promiseValueSize;
} else if (_isObject(value)) {
} else if (typeof value === "object") {
// if the data is an Object multiply each element with a defined default length
return this.options.objectValueSize * _size(value);
return this.options.objectValueSize * Object.keys(value).length;
} else if (typeof value === "boolean") {
return 8;
} else {

@@ -764,3 +749,3 @@ // default fallback

_error(type, data = {}, cb) {
_error(type, data = {}) {
var error;

@@ -774,9 +759,4 @@ boundMethodCheck(this, NodeCache);

error.data = data;
if (cb && _isFunction(cb)) {
// return the error
cb(error, null);
} else {
// if no callback is defined return the error object
return error;
}
// return the error object
return error;
}

@@ -791,12 +771,20 @@

_errMsg = ref[_errT];
this.ERRORS[_errT] = _template(_errMsg);
this.ERRORS[_errT] = this.createErrorMessage(_errMsg);
}
}
createErrorMessage(errMsg) {
return function(args) {
return errMsg.replace("__key", args.type);
};
}
};
NodeCache.prototype._ERRORS = {
"ENOTFOUND": "Key `<%= key %>` not found",
"EKEYTYPE": "The key argument has to be of type `string` or `number`. Found: `<%= type %>`",
"EKEYSTYPE": "The keys argument has to be an array."
"ENOTFOUND": "Key `__key` not found",
"ECACHEFULL": "Cache max key size exceeded",
"EKEYTYPE": "The key argument has to be of type `string` or `number`. Found: `__key`",
"EKEYSTYPE": "The keys argument has to be an array.",
"ETTLTYPE": "The ttl argument has to be a number."
};

@@ -803,0 +791,0 @@

@@ -28,3 +28,3 @@ {

],
"version": "4.2.1",
"version": "5.0.0-alpha.0",
"author": "mpneuried <mp@tcs.de>",

@@ -60,4 +60,3 @@ "maintainers": [

"dependencies": {
"clone": "2.x",
"lodash": "^4.17.15"
"clone": "2.x"
},

@@ -64,0 +63,0 @@ "devDependencies": {

@@ -1,3 +0,2 @@

node-cache
===========
![Logo](./logo/logo.png)

@@ -30,3 +29,3 @@ [![Build Status](https://secure.travis-ci.org/mpneuried/nodecache.svg?branch=master)](http://travis-ci.org/mpneuried/nodecache)

```bash
npm install node-cache --save
npm install node-cache --save
```

@@ -51,3 +50,2 @@

`0` = no periodic check.
- `errorOnMissing`: *(default: `false`)* en/disable throwing or passing an error to the callback if attempting to `.get` a missing or expired value.
- `useClones`: *(default: `true`)* en/disable cloning of variables. If `true` you'll get a copy of the cached variable. If `false` you'll save and get just the reference.

@@ -58,2 +56,4 @@ **Note:** `true` is recommended, because it'll behave like a server-based caching. You should set `false` if you want to save mutable objects or other complex types with mutability involved and wanted.

If `true` the variable will be deleted. If `false` the variable will remain. You are encouraged to handle the variable upon the event `expired` by yourself.
- `enableLegacyCallbacks`: *(default: `false`)* re-enables the usage of callbacks instead of sync functions. Adds an additional `cb` argument to each function which resolves to `(err, result)`. will be removed in node-cache v6.x.
- `maxKeys`: *(default: `-1`)* specifies a maximum amount of keys that can be stored in the cache. If a new item is set and the cache is full, an error is thrown and the key will not be saved in the cache. -1 disables the key limit.

@@ -67,7 +67,7 @@ ```js

*Key-validation*: The keys can be given as either `string` or `number`, but are casted to a `string` internally anyway.
All other types will either throw an error or call the callback with an error.
All other types will throw an error.
## Store a key (SET):
`myCache.set( key, val, [ ttl ], [callback] )`
`myCache.set( key, val, [ ttl ] )`

@@ -79,18 +79,3 @@ Sets a `key` `value` pair. It is possible to define a `ttl` (in seconds).

obj = { my: "Special", variable: 42 };
myCache.set( "myKey", obj, function( err, success ){
if( !err && success ){
console.log( success );
// true
// ... do something ...
}
});
```
> Note: If the key expires based on it's `ttl` it will be deleted entirely from the internal data object.
**Since `1.0.0`**:
Callback is now optional. You can also use synchronous syntax.
```js
obj = { my: "Special", variable: 42 };
success = myCache.set( "myKey", obj, 10000 );

@@ -100,6 +85,8 @@ // true

> Note: If the key expires based on it's `ttl` it will be deleted entirely from the internal data object.
## Retrieve a key (GET):
`myCache.get( key, [callback] )`
`myCache.get( key )`

@@ -111,22 +98,5 @@ Gets a saved value from the cache.

```js
myCache.get( "myKey", function( err, value ){
if( !err ){
if(value == undefined){
// key not found
}else{
console.log( value );
//{ my: "Special", variable: 42 }
// ... do something ...
}
}
});
```
**Since `1.0.0`**:
Callback is now optional. You can also use synchronous syntax.
```js
value = myCache.get( "myKey" );
if ( value == undefined ){
// handle miss!
// handle miss!
}

@@ -138,3 +108,3 @@ // { my: "Special", variable: 42 }

The return format changed to a simple value and a `ENOTFOUND` error if not found *( as `callback( err )` or on sync call as result instance of `Error` )*.
The return format changed to a simple value and a `ENOTFOUND` error if not found *( as result instance of `Error` )

@@ -151,5 +121,5 @@ **Since `2.1.0`**:

try{
value = myCache.get( "not-existing-key", true );
value = myCache.get( "not-existing-key", true );
} catch( err ){
// ENOTFOUND: Key `not-existing-key` not found
// ENOTFOUND: Key `not-existing-key` not found
}

@@ -160,3 +130,3 @@ ```

`myCache.mget( [ key1, key2, ... ,keyn ], [callback] )`
`myCache.mget( [ key1, key2, ..., keyn ] )`

@@ -168,26 +138,8 @@ Gets multiple saved values from the cache.

```js
myCache.mget( [ "myKeyA", "myKeyB" ], function( err, value ){
if( !err ){
console.log( value );
/*
{
"myKeyA": { my: "Special", variable: 123 },
"myKeyB": { the: "Glory", answer: 42 }
}
*/
// ... do something ...
}
});
```
**Since `1.0.0`**:
Callback is now optional. You can also use synchronous syntax.
```js
value = myCache.mget( [ "myKeyA", "myKeyB" ] );
/*
{
"myKeyA": { my: "Special", variable: 123 },
"myKeyB": { the: "Glory", answer: 42 }
}
{
"myKeyA": { my: "Special", variable: 123 },
"myKeyB": { the: "Glory", answer: 42 }
}
*/

@@ -202,3 +154,3 @@ ```

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

@@ -208,14 +160,2 @@ Delete a key. Returns the number of deleted entries. A delete will never fail.

```js
myCache.del( "myKey", function( err, count ){
if( !err ){
console.log( count ); // 1
// ... do something ...
}
});
```
**Since `1.0.0`**:
Callback is now optional. You can also use synchronous syntax.
```js
value = myCache.del( "A" );

@@ -227,3 +167,3 @@ // 1

`myCache.del( [ key1, key2, ... ,keyn ], [callback] )`
`myCache.del( [ key1, key2, ..., keyn ] )`

@@ -233,14 +173,2 @@ Delete multiple keys. Returns the number of deleted entries. A delete will never fail.

```js
myCache.del( [ "myKeyA", "myKeyB" ], function( err, count ){
if( !err ){
console.log( count ); // 2
// ... do something ...
}
});
```
**Since `1.0.0`**:
Callback is now optional. You can also use synchronous syntax.
```js
value = myCache.del( "A" );

@@ -258,3 +186,3 @@ // 1

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

@@ -268,22 +196,10 @@ Redefine the ttl of a key. Returns true if the key has been found and changed. Otherwise returns false.

myCache = new NodeCache( { stdTTL: 100 } )
myCache.ttl( "existendKey", 100, function( err, changed ){
if( !err ){
console.log( changed ); // true
// ... do something ...
}
});
changed = myCache.ttl( "existentKey", 100 )
// true
myCache.ttl( "missingKey", 100, function( err, changed ){
if( !err ){
console.log( changed ); // false
// ... do something ...
}
});
changed2 = myCache.ttl( "missingKey", 100 )
// false
myCache.ttl( "existendKey", function( err, changed ){
if( !err ){
console.log( changed ); // true
// ... do something ...
}
});
changed3 = myCache.ttl( "existentKey" )
// true
```

@@ -293,3 +209,3 @@

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

@@ -300,3 +216,3 @@ Receive the ttl of a key.

- `0` if this key has no ttl
- a timestamp in ms until the key expires
- a timestamp in ms representing the time at which the key will expire

@@ -313,7 +229,3 @@ ```js

myCache.getTtl( "ttlKey", function( err, ts ){
if( !err ){
// ts wil be approximately 1456000600000
}
});
ts = myCache.getTtl( "ttlKey" )
// ts wil be approximately 1456000600000

@@ -326,3 +238,2 @@

// ts = undefined
```

@@ -332,3 +243,3 @@

`myCache.keys( [callback] )`
`myCache.keys()`

@@ -338,11 +249,2 @@ Returns an array of all existing keys.

```js
// async
myCache.keys( function( err, mykeys ){
if( !err ){
console.log( mykeys );
// [ "all", "my", "keys", "foo", "bar" ]
}
});
// sync
mykeys = myCache.keys();

@@ -352,3 +254,15 @@

// [ "all", "my", "keys", "foo", "bar" ]
```
## Has key (HAS)
`myCache.has( key )`
Returns boolean indicating if the key is cached.
```js
/* sync */
exists = myCache.has( 'myKey' );
console.log( exists );
```

@@ -364,11 +278,11 @@

myCache.getStats();
/*
{
keys: 0, // global key count
hits: 0, // global hit count
misses: 0, // global miss count
ksize: 0, // global key size count
vsize: 0 // global value size count
}
*/
/*
{
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
}
*/
```

@@ -385,11 +299,11 @@

myCache.getStats();
/*
{
keys: 0, // global key count
hits: 0, // global hit count
misses: 0, // global miss count
ksize: 0, // global key size count
vsize: 0 // global value size count
}
*/
/*
{
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
}
*/
```

@@ -416,3 +330,3 @@

myCache.on( "set", function( key, value ){
// ... do something ...
// ... do something ...
});

@@ -428,3 +342,3 @@ ```

myCache.on( "del", function( key, value ){
// ... do something ...
// ... do something ...
});

@@ -440,3 +354,3 @@ ```

myCache.on( "expired", function( key, value ){
// ... do something ...
// ... do something ...
});

@@ -451,3 +365,3 @@ ```

myCache.on( "flush", function(){
// ... do something ...
// ... do something ...
});

@@ -454,0 +368,0 @@ ```

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc