
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@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 775,047 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.