All Projects → arbazsiddiqui → Lru Cache Node

arbazsiddiqui / Lru Cache Node

Licence: mit
A lighting fast cache manager for node with least-recently-used policy.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Lru Cache Node

Strapi Middleware Cache
🔌 A cache middleware for https://strapi.io
Stars: ✭ 146 (+21.67%)
Mutual labels:  hacktoberfest, cache
Drone Cache
A Drone plugin for caching current workspace files between builds to reduce your build times
Stars: ✭ 194 (+61.67%)
Mutual labels:  hacktoberfest, cache
Pokeapi Js Wrapper
PokeAPI browser wrapper, fully async with built-in cache
Stars: ✭ 129 (+7.5%)
Mutual labels:  hacktoberfest, cache
Kinto.js
An Offline-First JavaScript Client for Kinto.
Stars: ✭ 268 (+123.33%)
Mutual labels:  hacktoberfest, cache
React Esi
React ESI: Blazing-fast Server-Side Rendering for React and Next.js
Stars: ✭ 537 (+347.5%)
Mutual labels:  hacktoberfest, cache
Hazelcast Hibernate
A distributed second-level cache for Hibernate
Stars: ✭ 24 (-80%)
Mutual labels:  hacktoberfest, cache
Layercache
Caching made simple for Android and Java.
Stars: ✭ 155 (+29.17%)
Mutual labels:  hacktoberfest, cache
Memento
Memento is a development-only tool that caches HTTP calls once they have been executed.
Stars: ✭ 380 (+216.67%)
Mutual labels:  hacktoberfest, cache
Bigcache
Efficient cache for gigabytes of data written in Go.
Stars: ✭ 5,304 (+4320%)
Mutual labels:  hacktoberfest, cache
Offix
GraphQL Offline Client and Server
Stars: ✭ 694 (+478.33%)
Mutual labels:  hacktoberfest, cache
Typerep Map
⚡️Efficient implementation of Map with types as keys
Stars: ✭ 85 (-29.17%)
Mutual labels:  hacktoberfest, cache
Sudoku Solver
GUI Sudoku Solver using Pygame
Stars: ✭ 120 (+0%)
Mutual labels:  hacktoberfest
Pg ha migrations
Enforces DDL/migration safety in Ruby on Rails project with an emphasis on explicitly choosing trade-offs and avoiding unnecessary magic.
Stars: ✭ 119 (-0.83%)
Mutual labels:  hacktoberfest
Useful Dev Tools
⭐️ A list with useful tools that help many Developers. Hacktoberfest ⭐️
Stars: ✭ 119 (-0.83%)
Mutual labels:  hacktoberfest
Unshort.link
Prevent short link services from tracking you by unshortening the urls for your
Stars: ✭ 119 (-0.83%)
Mutual labels:  hacktoberfest
Wp Graphql Yoast Seo
This is an extension to the WPGraphQL plugin for Yoast SEO
Stars: ✭ 120 (+0%)
Mutual labels:  hacktoberfest
Pythonalgorithms
All Algorithms implemented in Python 3
Stars: ✭ 120 (+0%)
Mutual labels:  hacktoberfest
Hacktoberfest2021
Raise Genuine PRs, Your PRs will be accepted, Star This Repo, You aren't allowed to Update README.md
Stars: ✭ 119 (-0.83%)
Mutual labels:  hacktoberfest
Pytrustnfe
Módulo NF-e Python
Stars: ✭ 118 (-1.67%)
Mutual labels:  hacktoberfest
Mangaki
Site de recommandation de mangas et d'anime
Stars: ✭ 118 (-1.67%)
Mutual labels:  hacktoberfest

lru-cache-node

Build Status codecov npm

A lighting fast cache manager for node with least-recently-used policy.

A super fast cache for node with LRU policy. Cache will keep on adding values until the maxSize is reached.

After that it will start popping out the Least recently used/accessed value from the cache in order to set the new ones.

Supports expiry and stale.

Implemented using doubly-linked-list and hashmap with O(1) time complexity for gets and sets.

Install

$ npm install --save lru-cache-node

Usage

const Cache = require('lru-cache-node');

let cache = new Cache(3); //set max size of cache as three

cache.set('a', 7); //sets a value in cache with 'a' as key and 7 as value
cache.set('b', 5);
cache.set('c', 3); 
/*
  [ { key: 'c', value: 3 },
    { key: 'b', value: 5 },
    { key: 'a', value: 7 } ]
 */
 
cache.set('d', 10) // pops out a
/*
  [ { key: 'd', value: 10 },
    { key: 'c', value: 3 },
    { key: 'b', value: 5 } ]
 */
 
cache.get("b") //returns 5 and makes it most recently used
/*
  [ { key: 'b', value: 5 },
    { key: 'd', value: 10 },
    { key: 'c', value: 3 } ]
 */

cache.peek("d") //returns 10 but doesnt resets the order
/*
  [ { key: 'b', value: 5 },
    { key: 'd', value: 10 },
    { key: 'c', value: 3 } ]
 */

let cache = new Cache(3, 10); //Initialize Cache with size 3 and expiry for keys as 10ms
const sleep = ms => new Promise(r => setTimeout(r, ms));

cache.set('a', 7); //valid for 10ms
cache.get('a'); //returns 7 and resets 10ms counter
await sleep(15);
cache.get('a'); //null

cache.set('b', 5, 30); //overwrites cache's default expiry of 10ms and uses 30ms
await sleep(15);
cache.get('b'); //returns 5 and resets the expiry of b back to 30ms
await sleep(35);
cache.get('b'); //null

API

cache(maxSize, maxAge, stale)

maxSize

Type: Number
Default: Infinity

Maximum size of the cache.

maxAge

Type: Number
Default: Infinity

Default expiry for all keys for the cache. It does not proactively deletes expired keys, but will return null when an expired key is being accessed.

stale

Type: Boolean
Default: false

If set to true, will return the value of expired key before deleting it from cache.

set(key, value, maxAge)

key

Key to be set.

value

Value for the key.

maxAge

Expiry of the key. Will override cache's maxAge if specified.

get(key)

Returns the value for the key. If not key does not exist will return null.

Both set() and get() will update the "recently used"-ness and expiry of the key.

peek(key)

Returns the value for the key, without making the key most recently used. If not key does not exist will return null.

delete(key)

Deletes the key from the cache.

contains(key)

Returns a boolean indication if the value exists in cache or not.

getSize()

Returns the current size of cache.

reset()

Clears the whole cache and reinitialize it.

toArray()

Returns an array form of the catch.

let cache = new Cache();
cache.set("a", 5);
cache.set("b", 4);
cache.set("c", 0);
cache.toArray()
/*
[ { key: 'c', value: 0 },
  { key: 'b', value: 4 },
  { key: 'a', value: 5 } ]
*/

forEach(callback)

Takes a function and iterates over all the keys in the cache, in order of recentness. Callback takes key, value and index as params.

let cache = new Cache();
cache.set("a", 1);
cache.set("b", 2);
cache.set("c", 3);
cache.forEach((key, value, index) => {
	console.log(key, value, index)
})
/*
c 3 0
b 2 1
a 1 2
*/
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].