Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@hapi/catbox-memory
Advanced tools
@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.
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();
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 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 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.
catbox-memory is part of the hapi ecosystem and was designed to work seamlessly with the hapi web framework and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out hapi – they work even better together.
FAQs
Memory adapter for catbox
The npm package @hapi/catbox-memory receives a total of 565,831 weekly downloads. As such, @hapi/catbox-memory popularity was classified as popular.
We found that @hapi/catbox-memory demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.