Simple LRU Cache

0.0.2 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates basic cache initialization and common operations including setting, getting, deleting, and resetting entries.

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

view raw JSON →