0.0.4 • Published 5 years ago

node-tagged-cache v0.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Fast NodeJS in-memory caching with tagging

Probably the fastest NodeJS caching system yet.
It is much like memcached or redis but provides you with ability to set tags for cache entries and drop them all together when you need it.
Note that this module does not clone stored arrays/objects and operates only with their refs.

Performance

As the entries store node-tagged-cache uses Map due to it's total overperforming regular objects. All below performance tests made on Win10, Intel I7 5820K, 16GB DDR4 RAM.
All numbers are OPS (operations per second).
500000 rounds peer test.

storagesetgetcheckdelete
obj3,846,15312,820,51214,285,71411,111,111
objV24,237,28813,888,88814,285,71415,151,515
map3,816,79355,555,55550,000,0007,246,376

The other thing that appeared to be a real showstopper in terms of performance - Date.now() calls, which been used in cache itself and in cache controller. So intermediate timestamp cache was implemented, performance impact of which you can see below.
And, furthermore, below you can see performance comparision with two most popular caching libs i know about.

Note: node-cache is in useClones: false, mode, but cache-base always copies it's values;

modulesetmsetgetmgethasmhas
Tagged Cache (w/o timestamp) (w/o tags)'1,179,245''2,463,054''1,510,574''2,717,391''1,533,742''2,392,344'
Tagged Cache (w/o timestamp)'1,302,083''2,392,344''1,146,788''1,298,701''1,070,663''1,388,888'
Tagged Cache (w/o tags)'2,631,578''3,333,333''5,208,333''2,906,976''5,494,505''3,355,704'
Tagged Cache'2,976,190''3,378,378''1,845,018''1,436,781''1,976,284''1,529,051'
node-cache'284,900''none''1,312,335''1,098,901''none''none'
cache-base'507,614''none''892,857''none''5,319,148''none'

You can simply run performance tests on your own machine.

node benchmarks/storage.js
node benchmarks/cache.js

Install

npm i node-tagged-cache

Usage

import TaggedCache, {enableTimestampCache, disableTimestampCache} from "node-tagged-cache";
// default module's export is already created instance of cache with default options
// {
//  defaultTTL: 600000, // 10m
//  cleanupInterval: 60000, // 1m
// }

enableTimestampCache(); // this is not straight requirement but needed for performance improvement

TaggedCache.mset({
                    foo: "bar",
                    baz: "bax"
                },
                3600000, // 1h
                ["awesomeTag", "moreAwesomeTag"]);
TaggedCache.mget(["foo", "baz"]); // { foo: "bar", baz: "bax" }

TaggedCache.tags.drop("awesomeTag");
TaggedCache.mhas(["foo", "baz"]); // { foo: false, baz: false }

disableTimestampCache(); // before the script should exit;