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

rebusted-cache

v1.0.0

Published

A distributed cache system built using Redis with LRU, FIFO, and Random eviction policies.

Downloads

69

Readme

rebusted-cache

rebusted-cache is a Node.js caching library that provides an efficient caching mechanism using Redis, with support for eviction policies (LRU, FIFO, Random) and Time-To-Live (TTL) management. It allows you to store, retrieve, and manage cache data with ease and ensures your cache remains within size limits while allowing for customized eviction strategies.

Installation

To install rebusted-cache in your Node.js project, use npm:

npm install rebusted-cache

Usage

Importing

const multicasecache = require('rebusted-cache');

Example

const cache = new multicasecache({
  maxSize: 500,
  ttl: 100000,
  evictionPolicy: "LRU",
  onEvict: (key, value) => {
    console.log(`Evicted: ${key} with value ${value}`);
  },
  pruneIntervalTime: 5000,
  redisConfig: { host: "localhost", port: 6379 },
});

async function run() {
  await cache.set("user:123", { name: "Alice" });
  const user = await cache.get("user:123");
  console.log(user); // { name: "Alice" }
}

Configuration Options

| Option | Type | Default Value | Description | | ------------------- | -------------------------- | ------------------------------------------ | ----------- | | maxSize | number | 1000 | The maximum size of the cache (number of entries). | | ttl | number (milliseconds) | 60000 | Time-to-live for cache entries (in milliseconds). | | onEvict | function | null | Callback function to call when an entry is evicted. | | evictionPolicy | string ("LRU", "FIFO", "Random") | "LRU" | The eviction policy to use when the cache exceeds the size limit. | | pruneIntervalTime | number (milliseconds) | 1000 | The interval time for pruning expired cache entries (in milliseconds). | | redisConfig | object | { host: "localhost", port: 6379 } | Configuration object for the Redis client connection. |

Methods

constructor(options)

Creates a new rebusted-cache instance with the specified options.

Parameters:

  • options (object): The configuration object for the cache.

get(key)

Retrieves the cached value for a given key. If the entry has expired, it will be deleted and undefined will be returned.

Parameters:

  • key (string): The cache key.

Returns:

  • A promise that resolves with the cached value, or undefined if not found or expired.

set(key, value)

Sets a value in the cache for a given key with the specified TTL.

Parameters:

  • key (string): The cache key.
  • value (any): The value to store.

Returns:

  • A promise that resolves when the value is stored.

delete(key)

Deletes the cached entry for a given key.

Parameters:

  • key (string): The cache key.

Returns:

  • A promise that resolves when the key is deleted.

clear()

Clears the entire cache.

Returns:

  • A promise that resolves when the cache is cleared.

updateAccessTime(key)

Updates the access time for a given key.

Parameters:

  • key (string): The cache key.

Returns:

  • A promise that resolves when the access time is updated.

evict()

Evicts a cache entry based on the selected eviction policy (LRU, FIFO, or Random).

Returns:

  • A promise that resolves when eviction is performed.

startTTLPruning()

Starts a background task that periodically prunes expired cache entries.

Returns:

  • void

pruneExpired()

Prunes expired cache entries.

Returns:

  • A promise that resolves when expired entries are pruned.

destroy()

Closes the Redis client connection.

Returns:

  • A promise that resolves when the connection is closed.

License

rebusted-cache is released under the MIT License.