node-cache
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
clone | 2.1.2 | 5.68 kB | MIT | prod |
Node-cache is a simple, high-performance internal caching module for Node.js. It functions like the well-known Memcached solution, supporting set
, get
, and delete
methods to manage the cache. Each key in the cache can have a defined timeout (Time to Live or TTL). After this time has expired, the key will automatically be eliminated from the cache. Node-cache features a practical limit of around one million keys as all keys are stored in a single object.
To use Node-cache, start by installing it in your project with NPM using the following command: npm install node-cache --save
. Next, require it in your Node.js file and instantiate it:
const NodeCache = require( "node-cache" );
const myCache = new NodeCache();
You can store keys and retrieve them as follows:
// store a key
myCache.set("myKey", "myValue", 10000); // ttl is set to 10 seconds here
// retrieve a key
let value = myCache.get("myKey");
You can also delete keys using the del
method and check if a key exists using the has
method:
// delete a key
myCache.del("myKey");
// check if a key exists
let exists = myCache.has('myKey');
The Node-cache documentation can be found within the Node-cache repository on GitHub at git://github.com/node-cache/node-cache.git. The documentation covers every aspect of the package, from installation to usage examples, and includes in-depth explanations of every available method. It also provides event handling samples, warning about breaking changes in different versions, and compatibility information.