Ewen's map-cache library
  • JavaScript 100%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2024-11-20 16:03:04 +01:00
conf adding JSdoc in cache 2024-11-20 16:03:04 +01:00
lib adding JSdoc in cache 2024-11-20 16:03:04 +01:00
src adding JSdoc in cache 2024-11-20 16:03:04 +01:00
test adding JSdoc in cache 2024-11-20 16:03:04 +01:00
.gitignore add coverage to .gitignore 2023-09-27 16:44:20 +02:00
.gitlab-ci.yml coverage 2023-10-03 15:23:08 +00:00
.npmrc adding npmrc 2023-09-28 10:49:29 +02:00
index.js initial commit 2023-09-20 11:25:07 +02:00
package-lock.json version 0.6.0 2024-11-20 15:36:22 +01:00
package.json adding JSdoc in cache 2024-11-20 16:03:04 +01:00
README.md adding JSdoc in cache 2024-11-20 16:03:04 +01:00

map-cache

pipeline status coverage report

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 = unlimited
  • checkperiod: (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. 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.
  • 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