A basic in memory cache module with tags support for Javascript/NodeJS projects. It's an extension of super useful and neat node cache library. It has basic set
, get
and del
, delByTags
methods and works a little bit like memcached. In addition to this, we can tag item while adding to cache and remove it based on tags as well. Keys can have a timeout (ttl
) after which they expire and are deleted from the cache. All keys are stored in a single object so the practical limit is at around 1m keys.
Concept inspired by Drupal 8’s cache tags
npm install js-cache-tags --save
Or just require the js-cache-tags.min.js
file to get the base class.
const JSCacheTags = require( "js-cache-tags" );
const myCache = new JSCacheTags();
const JSCacheTags = require( "js-cache-tags" );
const myCache = new JSCacheTags({ stdTTL: 100, checkperiod: 120 });
stdTTL
: (default:0
) the standard ttl as number in seconds for every generated cache element.0
= unlimitedcheckperiod
: (default:600
) The period in seconds, as a number, used for the automatic delete check interval.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. Iftrue
you'll get a copy of the cached variable. Iffalse
you'll save and get just the reference. Note:true
is recommended, because it'll behave like a server-based caching. You should setfalse
if you want to save mutable objects or other complex types with mutability involved and wanted. Here's a simple code exmaple showing the different behaviordeleteOnExpire
: (default:true
) whether variables will be deleted automatically when they expire. Iftrue
the variable will be deleted. Iffalse
the variable will remain. You are encouraged to handle the variable upon the eventexpired
by yourself.
myCache.set(key, val, [tags], [ttl], [callback])
Sets a key
value
pair. You can attach tags
(array). Also possible to define a ttl
(in seconds).
Returns true
on success.
obj = { name: "Viresh", age: 35 };
tags = ["tech geek", "foodie"]
myCache.set("myKey", obj, tags, 100, (err, success) => {
if (!err && success) {
console.log(success);
// true
// ... do something ...
}
});
obj = { name: "Viresh", age: 30 };
tags = [{"city": "Pune"}, {"country": "India"}]
success = myCache.set("myKey", obj, tags, 1000);
myCache.get(key, [callback], [errorOnMissing])
Gets a saved value from the cache. Returns a undefined
if not found or expired.
If the value was found then it returns an object with value
.
try {
myCache.get("myKey", (err, value) => {
if (!err) {
if (value == undefined) {
// key not found
} else {
console.log(value);
//{ name: "Viresh", age: 35 };
// ... do something ...
}
}
});
} catch(err) {
// ENOTFOUND: Key `not-existing-key` not found
}
myCache.getByTags(tags, [callback], [errorOnMissing])
Gets the items from cache by tags. Returns an empty array if not found. If the tag
was found then it returns array of values
for which tag was matched.
myCache.getByTags(["tech geek"], (err, values) => {
if (!err) {
console.log(values);
//[{ name: "Viresh", age: 35 }];
// ... do something ...
}
});
myCache.getByTags([{"city": "Pune"}], (err, values) => {
if (!err) {
console.log(values);
//[{ name: "Viresh", age: 35 }];
// ... do something ...
}
});
myCache.delByTags(tags, [callback] )
Delete item from cache by tags. Returns the number of deleted entries.
myCache.delByTags(["tech geek"], (err, count) => {
if (!err) {
console.log(count); // 1
// ... do something ...
}
});
myCache.delByTags([{"city": "Pune"}, {"country": "India"}], (err, count) => {
if (!err) {
console.log(count); // 1
// ... do something ...
}
});
myCache.delByTags([123, 456], (err, count) => {
if(!err){
console.log(count); // 1
// ... do something ...
}
});
js-cache-tags is extension to node-cache library (https://github.com/mpneuried/nodecache). Many thanks to Mathias Peter.