Simple LRU Cache
The `simple-lru-cache` package offers a minimalistic and fast Least Recently Used (LRU) cache implementation for Node.js. Its core functionality revolves around prioritizing recently accessed keys, ejecting the least recently used key only when the cache reaches its configured maximum size (`maxSize`). Despite its explicit goal of simplicity and speed, the package is currently at version 0.0.2 and has not seen updates for over eight years, indicating it is abandoned. This makes it unsuitable for modern production environments due to potential security vulnerabilities, lack of compatibility with newer Node.js versions, and absence of modern features like time-based expiration (TTL). Its key differentiator was intended to be its extreme simplicity and speed compared to more feature-rich alternatives like `lru-cache`, but its current state makes it a significant risk. Developers should consider maintained alternatives or native JavaScript `Map` objects for simple caching needs.
Common errors
-
TypeError: simple_lru_cache__WEBPACK_IMPORTED_MODULE_0__ is not a constructor
cause Attempting to import `SimpleCache` using ES module syntax (`import`) in an environment where the package is only available as CommonJS.fixUse CommonJS `require` syntax instead: `var SimpleCache = require("simple-lru-cache");` -
ReferenceError: SimpleCache is not defined
cause The `SimpleCache` class was not properly imported or required before being used to instantiate a cache.fixEnsure `var SimpleCache = require("simple-lru-cache");` is at the top of your script file before any usage of `SimpleCache`. -
(No explicit error, but unexpected behavior where items don't disappear after a time limit)
cause Misunderstanding that `simple-lru-cache` only uses an LRU eviction policy and does not support time-to-live (TTL) for cache entries.fixBe aware that items are only removed when the cache's `maxSize` is reached and they are the least recently used. Implement external TTL logic if time-based expiration is necessary.
Warnings
- breaking This package is abandoned, with the last commit over 8 years ago. It is highly unrecommended for production use due to a complete lack of maintenance, potential security vulnerabilities, and likely incompatibility with modern Node.js versions and best practices.
- gotcha The package is strictly CommonJS-only and does not provide ESM exports. Attempting to `import` it in an ESM module will lead to runtime errors.
- gotcha This cache implementation does not support time-to-live (TTL) for entries. Items are only evicted based on the Least Recently Used policy when the `maxSize` limit is exceeded.
- gotcha The cache stores direct references to objects. Modifying an object retrieved from the cache will also modify the object within the cache itself. There is no automatic deep cloning.
- gotcha The extremely low version (0.0.2) and long-term inactivity indicate that this library has not been robustly tested or evolved with community feedback, potentially containing unknown bugs or edge cases.
Install
-
npm install simple-lru-cache -
yarn add simple-lru-cache -
pnpm add simple-lru-cache
Imports
- SimpleCache
import SimpleCache from 'simple-lru-cache'
var SimpleCache = require("simple-lru-cache")
Quickstart
var SimpleCache = require("simple-lru-cache");
// Initialize the cache with a maximum size of 1000 items
var cache = new SimpleCache({"maxSize":1000});
// Add a key-value pair to the cache
cache.set("hello","world");
// Retrieve a value by its key
var value = cache.get("hello");
console.log(`Retrieved: ${value}`); // Output: Retrieved: world
// Add more items to trigger LRU eviction if maxSize is reached
for (let i = 0; i < 1001; i++) {
cache.set(`key${i}`, `value${i}`);
}
// Delete a specific key from the cache
cache.del("hello");
console.log(`'hello' after deletion: ${cache.get('hello')}`); // Output: 'hello' after deletion: undefined
// Reset the entire cache, removing all entries
cache.reset();
console.log(`Cache size after reset: ${cache.size()}`); // Output: Cache size after reset: 0