GithubHelp home page GithubHelp logo

opensnip / lrujs Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 87 KB

Fast and lightweight lru cache for javascript

License: MIT License

JavaScript 100.00%
caching-library library lru lru-cache lrucache lrujs opensnip javascript nodejs

lrujs's Introduction

Lrujs

NPM Version NPM Install Size NPM Downloads

Lrujs is a fast and lightweight lru (least-recently-used) caching library for javascript.

Features

  • Super Fast
  • Lightweight
  • TTL support
  • Handle cache-miss

Installation

Install using npm:

$ npm i @opensnip/lrujs

Install using yarn:

$ yarn add @opensnip/lrujs

Example

A simple lru cache example:

const Cache = require("@opensnip/lrujs");

// Create cache object
const cache = new Cache();

// Add data in cache
cache.set("a", 10);

// Check data exists in cache
cache.has("a"); // true

// Get data from cache
console.log(cache.get("a")); // 10

// Get all data from cache
cache.forEach(function (data) {
  console.log(data); // { a: 10 }
});

// Get all data to array
console.log(cache.toArray()); // [ { a: 10 } ]

// Delete data from cache
cache.delete("a");

// Delete all data from cache
cache.clear();

Create a new cache

To create a new cache we need to create a new instance of lrujs. While creating a new cache we can set the configuration like cache max length and ttl, but it is not mandatory and if we not set any configuration then the default values are used.

Defination:

const cache = new Cache(options);

Where options are the following:

  • maxLength : max length is a cache max length, max length is a positive integer value. The default value is 250, if the value is 0 then it will not check the max length.
  • ttl : is cache expires time in milliseconds, the default value is 0 and if value if 0 it will not check the ttl.
  • interval : interval is the time interval in milliseconds, after every interval all the expired items are automatically removed. Default value is 60000 and if value is 0 then it will not removes expired items automatically, but don't worry expired items are treated as missing, and deleted when they are fetched.
  • enableInterval : enableInterval is a boolean value that is used to enable and disable the interval, the default value is false and if value is explicitly set false then it will not run the interval even if the interval time is set.

Lrujs support TTL, but it is not a TTL cache, and also does not make strong TTL guarantees. When ttl interval is set, expired items are removed from cache periodically.

Example:

const Cache = require("@opensnip/lrujs");

// Create cache object
const cache = new Cache({
  maxLength: 10,
  ttl: 1000,
  interval: 60000,
});

Set a new data

In lrujs any value (both objects and primitive values) may be used as either a key or a value, duplicate keys not allowed and if duplicate item is inserted it will be replaced by the new item.

// Add new data in cache
cache.set("a", 10);

// Add new data in cache
cache.set("user", { name: "abc" });

// Add duplicate data
cache.set("a", 20); // Replace the old value

Set ttl for single data

By default the configuration TTL value is used for every item, but we can set TTL for a single item.

// Add new data in cache
cache.set("b", 10, { ttl: 200 }); // Expires after 200 ms

Get data from cache

By default on cache miss lrujs returns undefined value, but undefined is also can be used as value for items. In this case you can return a custom value on cache miss.

// Add new data in cache
cache.set("a", 10);

// Get data
cache.get("a"); // 10

Customize cache-miss value:

// Add new data in cache
cache.set("a", undefined);

cache.get("a"); // undefined
cache.get("b"); // undefined

// Set custom return value
cache.get("a", function (err, value) {
  if (err) return null;
  return value;
}); // undefined

cache.get("b", function (err, value) {
  if (err) return null;
  return value;
}); // null

Check data exists in cache

Check weather item exists in the cache or not.

// Add new data in cache
cache.set("a", undefined);

// Check data exists or not
cache.has("a"); // true
cache.has("b"); // false

Delete data from cache

Remove data from cache.

// Delete data
cache.delete("a");

Delete all data from cache

Remove all data from the cache.

// Delete all data
cache.clear();

Get all data from cache

Get all data from the cache.

// Add new data in cache
cache.set("a", 10);

// Get all data
cache.forEach(function (data) {
  console.log(data); // { a: 10 }
});

// OR

for (let data of cache) {
  console.log(data); // { a: 10 }
}

Get data as array

// Add new data in cache
cache.set("a", 10);

// Get all data
console.log(cache.toArray()); // [ { a: 10 } ]

License

MIT License

lrujs's People

Contributors

rajkumardusad avatar

Watchers

 avatar

Forkers

h2atecnologia

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.