Ewen's map-cache library
- JavaScript 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| conf | ||
| lib | ||
| src | ||
| test | ||
| .gitignore | ||
| .gitlab-ci.yml | ||
| .npmrc | ||
| index.js | ||
| package-lock.json | ||
| package.json | ||
| README.md | ||
map-cache
Simpler and faster NodeJS caching inspired by Node-Cache
A caching module that has an API similar to Node-Cache, but is written in ES5 and based on native javascript Map. Similarly to Node-Cache, it has set, get and delete methods and keys can have a timeout (ttl) after which they expire and are deleted from the cache.
Note: map-cache is aiming to similar performances as a pure Map and does not provide support for cloning values. Statistics are not implemented yet.
Options
map-cache options are similar to node-cache
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.deleteOnExpire: (default:true) whether variables will be deleted automatically when they expire. Iftruethe variable will be deleted. Iffalsethe variable will remain. You are encouraged to handle the variable upon the eventexpiredby yourself.maxKeys: (default:0) 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.validateKey: (default: keys should be strings). A function which takes a key as a parameter and should throw an error if the key is not valid.
Usage
ES5 modules
import Cache from "map-cache";
const cache = new Cache();
Commonjs
const Cache = require( "node-cache" )
const cache = new Cache()
SET a key
cache.set(key, val, ttl?)
// Sets a key/value pair, with an optionnal ttl (in seconds)
cache
.set("key1", "val1")
.set("key2", "val2", 60)
SET a key
cache.set(key, val, ttl?)
cache
.set("key1", "foo")
.set("key2", "bar", 1)
GET a key
cache.get(key)
let val1 = cache.get("key1")
// foo
let val2 = cache.get("key3")
// undefined
Performances
Writing cache
clonescache mset x 272 ops/sec ±11.07% (63 runs sampled)
objectcache mset x 384 ops/sec ±5.26% (81 runs sampled)
MapCache mset x 1,753 ops/sec ±3.42% (77 runs sampled)
nativeMap set x 1,674 ops/sec ±3.35% (80 runs sampled)
Reading cache
clonescache mget x 409 ops/sec ±6.32% (71 runs sampled)
objectcache mget x 440 ops/sec ±7.88% (67 runs sampled)
MapCache mget x 791 ops/sec ±1.46% (91 runs sampled)
nativeMap get x 2,368 ops/sec ±2.18% (92 runs sampled)
Change log
- 0.6.1
- Adding Templated JSdoc
- 0.6.0
- Readme improvements
- Adding a flushAndClose method, which flush the cache then close it
- 0.5.0
- default validation method is faster and accepts empty string
- mget is now validating keys
- Adding a mget_hits_miss method
- Adding a Change log section in README