1.0.2 • Published 1 year ago

@meluiz/memojs v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

memojs

A simple and fast internal memory cache for node.js that has set, get and delete. Keys can have a timout based on expires time. All keys are stored in a single object so, the practical limit is aroud 1m keys.

Install

With npm

npm install --save @meluiz/memojs

Or yarn

yarn add --save @meluiz/memojs

Usage

const cache = require("@meluiz/memojs");

// Now just use cache
cache.set("foo", "bar");
console.log(cache.get("foo"));

// You can make more than simplest
cache.set("foo", "bar", 60, function (key, value) {
  console.log("key: ", key);
  console.log("value: ", value);
}); // time in seconds

// Create a new instance
const newCache = new cache.Cache();

newCache.set("name", "John");
console.log(newCache.get("name"));

Which should print

bar
key: foo
value: bar
John

Api

Cache.set = function(key, value, time, callback)

Store a value and delete after timeout. Callback function is called after timeout too. If you wanna create infinite cache, you can set time as -1.

ArgsType
keystring
valueany
time?number
callback?function(key, value): void

Cache.get = function(key)

Retrieves a value for a given key or return null if not exists.

ArgsType
keystring

Cache.del = function(key, force)

Delete a key and return boolean specifying whether or not the key was deleted.

ArgsType
keystring
forceboolean?

Cache.clear = function()

Delete all keys and return boolean specifying whether or not the key was deleted.

Cache.has = function()

Return a boolean to specific key.

Cache.keys = string[]

Return all keys stored.

Cache.size = number

Return the length stored keys.