What is @hapi/catbox-memory?
@hapi/catbox-memory is a memory-based caching module for the hapi.js framework. It provides a simple and efficient way to store and retrieve data in memory, making it useful for scenarios where quick access to cached data is required.
What are @hapi/catbox-memory's main functionalities?
Basic Caching
This code demonstrates how to set up a basic in-memory cache using @hapi/catbox-memory. It initializes a Catbox client with the memory engine, sets a value in the cache, and retrieves it.
const Catbox = require('@hapi/catbox');
const CatboxMemory = require('@hapi/catbox-memory');
const client = new Catbox.Client(CatboxMemory);
const start = async () => {
await client.start();
const key = { id: 'example', segment: 'examples' };
await client.set(key, 'my cached value', 10000); // Cache for 10 seconds
const cached = await client.get(key);
console.log(cached.item); // Outputs: 'my cached value'
};
start();
Cache Expiration
This code demonstrates how to set a cache expiration time. The cached value will expire after 5 seconds, and attempting to retrieve it after 6 seconds will return null.
const Catbox = require('@hapi/catbox');
const CatboxMemory = require('@hapi/catbox-memory');
const client = new Catbox.Client(CatboxMemory);
const start = async () => {
await client.start();
const key = { id: 'example', segment: 'examples' };
await client.set(key, 'my cached value', 5000); // Cache for 5 seconds
setTimeout(async () => {
const cached = await client.get(key);
console.log(cached); // Outputs: null (cache expired)
}, 6000);
};
start();
Cache Deletion
This code demonstrates how to delete a cache entry. After setting a value in the cache, it is deleted using the drop method, and subsequent retrieval returns null.
const Catbox = require('@hapi/catbox');
const CatboxMemory = require('@hapi/catbox-memory');
const client = new Catbox.Client(CatboxMemory);
const start = async () => {
await client.start();
const key = { id: 'example', segment: 'examples' };
await client.set(key, 'my cached value', 10000); // Cache for 10 seconds
await client.drop(key); // Delete the cache entry
const cached = await client.get(key);
console.log(cached); // Outputs: null (cache deleted)
};
start();
Other packages similar to @hapi/catbox-memory
node-cache
node-cache is a simple and efficient in-memory caching module for Node.js. It provides similar functionality to @hapi/catbox-memory, including setting, getting, and deleting cache entries with expiration times. However, node-cache is a standalone module and does not require the hapi.js framework.
memory-cache
memory-cache is another in-memory caching module for Node.js. It offers basic caching functionalities such as setting, getting, and deleting cache entries with expiration times. Like node-cache, it is a standalone module and does not depend on any specific framework.
lru-cache
lru-cache is a caching module that implements a Least Recently Used (LRU) cache algorithm. It provides more advanced caching strategies compared to @hapi/catbox-memory, making it suitable for scenarios where memory usage needs to be optimized by evicting the least recently used items.