npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@opensnip/cachejs

v1.0.3

Published

Fast and lightweight caching library for javascript

Downloads

312

Readme

Cachejs

NPM Version NPM Install Size NPM Downloads

Cachejs is a fast and lightweight caching library for javascript.

Features

  • Super Fast
  • Lightweight
  • Multiple cache eviction policy (FIFO, LIFO, LRU, MRU)
  • TTL support
  • Custom cache-miss value

Installation

Install using npm:

$ npm i @opensnip/cachejs

Install using yarn:

$ yarn add @opensnip/cachejs

Example

A simple cachejs cache example:

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

// 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 cachejs. While creating a new cache we can set the configuration like eviction policy, 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:

  • evictionPolicy : eviction policy is can be any valid cache eviction policy, supported eviction policy are FIFO, LIFO, LRU, MRU
  • 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 to false then it will not run the interval even if the interval time is set.

Cachejs 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/cachejs");

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

Set a new data

In cachejs 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 cachejs returns undefined value, but undefined also can be used as a value for item. 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