rebusted-cache

1.0.0 • Public • Published

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.

Package Sidebar

Install

npm i rebusted-cache

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

16.9 kB

Total Files

3

Last publish

Collaborators

  • yahya-the-rebuster