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.
To install rebusted-cache
in your Node.js project, use npm:
npm install rebusted-cache
const multicasecache = require('rebusted-cache');
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" }
}
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. |
Creates a new rebusted-cache
instance with the specified options.
-
options
(object): The configuration object for the cache.
Retrieves the cached value for a given key. If the entry has expired, it will be deleted and undefined
will be returned.
-
key
(string): The cache key.
- A promise that resolves with the cached value, or
undefined
if not found or expired.
Sets a value in the cache for a given key with the specified TTL.
-
key
(string): The cache key. -
value
(any): The value to store.
- A promise that resolves when the value is stored.
Deletes the cached entry for a given key.
-
key
(string): The cache key.
- A promise that resolves when the key is deleted.
Clears the entire cache.
- A promise that resolves when the cache is cleared.
Updates the access time for a given key.
-
key
(string): The cache key.
- A promise that resolves when the access time is updated.
Evicts a cache entry based on the selected eviction policy (LRU, FIFO, or Random).
- A promise that resolves when eviction is performed.
Starts a background task that periodically prunes expired cache entries.
- void
Prunes expired cache entries.
- A promise that resolves when expired entries are pruned.
Closes the Redis client connection.
- A promise that resolves when the connection is closed.
rebusted-cache is released under the MIT License.