What is @wry/caches?
The @wry/caches package provides a simple and efficient caching mechanism designed to store and retrieve values based on keys. It is particularly useful for optimizing performance by reducing the need to recompute or fetch data that has been previously processed or retrieved.
What are @wry/caches's main functionalities?
Simple Key-Value Cache
This feature allows you to create a simple cache where you can store and retrieve values using keys. The example demonstrates setting a value with a key and then retrieving it.
{"const {Cache} = require('@wry/caches'); const cache = new Cache(); cache.set('key', 'value'); console.log(cache.get('key')); // Outputs: 'value'"}
Cache Entry Expiration
This feature supports setting an expiration time for cache entries. The example shows how to set a cache entry that expires after 3000 milliseconds.
{"const {Cache} = require('@wry/caches'); const cache = new Cache(); cache.set('key', 'value', {maxAge: 3000}); setTimeout(() => { console.log(cache.get('key')); // Outputs: undefined }, 3100);"}
Other packages similar to @wry/caches
node-cache
node-cache is an in-memory key-value store similar to @wry/caches but with additional features like TTL support for each cache object, and the ability to list all stored keys. It provides a more feature-rich API compared to @wry/caches.
lru-cache
lru-cache implements a cache mechanism that removes the least recently used items first. This is particularly useful for applications that need to manage memory usage carefully, which is a different approach compared to the simple key-value store provided by @wry/caches.
@wry/caches
Various cache implementations, including but not limited to
-
StrongCache
: A standard Map
-like cache with a least-recently-used (LRU)
eviction policy and a callback hook for removed entries.
-
WeakCache
: Another LRU cache that holds its keys only weakly, so entries can be removed
once no longer retained elsewhere in the application.