memory-store-worker
v1.5.0
Published
A memory store worker for managing key-value pairs with memory limits.
Downloads
38
Maintainers
Readme
Memory Store Worker v1.5
Memory Store Worker is a high-performance Node.js library designed for storing and retrieving temporary data in memory. The library utilizes Worker Threads to handle asynchronous operations efficiently, optimizing system resources and improving processing speed. It is ideal for applications that need to manage transient states without relying on external databases.
What's New in v1.5
- Cache Support in the Main Thread: New
Cache
class allows in-memory caching directly in the main thread. - Improved Performance: Asynchronous processing using worker threads and cache optimizations.
- Bug Fixes: Various stability improvements.
Features
- Temporary Data Storage: Store and retrieve key-value pairs in memory with optional expiration times.
- Memory Management: Set memory limits to prevent excessive resource consumption.
- Cache Management: Main thread caching support with expiration handling.
- Asynchronous Operations: Perform non-blocking operations using Worker Threads.
- Bulk Insertions: Insert multiple key-value pairs simultaneously with optional expiration times.
- Data Deletion: Delete specific keys or clear all stored values in memory.
- Existence Check: Check whether a key exists in memory.
- Data Export: Export values along with their remaining expiration times.
Installation
To install the library, use npm:
npm install memory-store-worker
Usage
Initialization
You can start using the library by creating an instance of MemoryStoreWorker or Cache with optional memory limit configurations:
const MemoryStoreWorker = require("memory-store-worker");
const memoryStoreWorker = new MemoryStoreWorker(1024); // 1024MB memory limit
const Cache = require("memory-store-worker").Cache;
const cache = new Cache(512); // 512MB memory limit for the cache
Methods
MemoryStoreWorker
setValue(key, value, expiryTime)
Store a key-value pair in memory with an optional expiration time.
await memoryStoreWorker.setValue('exampleKey', 'exampleValue', 5000); // Expires after 5000ms
updateValue(key, value, expiryTime)
Update the value for an existing key and optionally reset its expiration time.
await memoryStoreWorker.updateValue('exampleKey', 'newExampleValue', 8000); // Updates and expires after 8000ms
getValue(key)
Retrieve the value associated with a specific key.
await memoryStoreWorker.updateValue('exampleKey', 'newExampleValue', 8000); // Updates and expires after 8000ms
has(key)
Check whether the key exists in memory.
const exists = await memoryStoreWorker.has('exampleKey');
deleteValue(key)
Delete a key-value pair from memory.
await memoryStoreWorker.deleteValue('exampleKey');
deleteAlls()
Delete all key-value pairs stored in memory.
await memoryStoreWorker.deleteAlls();
store()
Retrieve the current state of the entire memory store.
const storeSnapshot = await memoryStoreWorker.store();
insertMany(keyValuePairs, expiryTimes)
Insert multiple key-value pairs at once, with optional expiration times for each key.
const keyValuePairs = {
key1: 'value1',
key2: 'value2',
key3: 'value3'
};
const expiryTimes = {
key1: 5000,
key2: 10000
};
await memoryStoreWorker.insertMany(keyValuePairs, expiryTimes);
exportValue(key)
Export a key's value along with its remaining expiration time.
const exportedValue = await memoryStoreWorker.exportValue('key1');
console.log(exportedValue); // { key: 'key1', value: 'value1', expiryTime: 5000 }
exportAll()
Export all key-value pairs with their remaining expiration times.
const exportedData = await memoryStoreWorker.exportAll();
console.log(exportedData); // { key1: { value: 'value1', expiryTime: 5000 }, key2: { value: 'value2', expiryTime: 10000 } }
Cache
init(initStore)
Initialize the cache with an optional pre-defined store.
await cache.init({ key1: 'value1', key2: 'value2' });
setValue(key, value, expiryTime)
Set a value in the cache with optional expiration.
await cache.setValue('key1', 'value1', 10000); // Expires after 10000ms
.....
Event Listeners
You can listen to various events emitted during operations:
const memoryStore = new MemoryStoreWorker();
memoryStore.on('setValue', ({ key, value, expiryTime }) => {
console.log(`Set value for key: ${key}`);
});
memoryStore.on('updateValue', ({ key, value, expiryTime }) => {
console.log(`Value updated for key: ${key}`);
});
memoryStore.on('getValue', ({ key, value }) => {
console.log(`Retrieved value for key: ${key}`);
});
memoryStore.on('deleteValue', ({ key }) => {
console.log(`Deleted value for key: ${key}`);
});
memoryStore.on('exportValue', ({ key, value }) => {
console.log(`Exported value for key: ${key}, value: ${value}`);
});
Example Usage
const MemoryStoreWorker = require('memory-store-worker');
const memoryStoreWorker = new MemoryStoreWorker({ memoryLimitMB: 200 }); // 200MB limit
const Cache = require('memory-store-worker').Cache;
const cache = new Cache(512); // 512MB limit
async function run() {
await memoryStoreWorker.setValue('key1', 'value1');
await memoryStoreWorker.setValue('key2', 'value2', 5000); // Expire after 5000ms
console.log(await memoryStoreWorker.getValue('key1')); // 'value1'
console.log(await memoryStoreWorker.has('key1')); // true
await memoryStoreWorker.deleteValue('key1');
console.log(await memoryStoreWorker.getValue('key1')); // undefined
await cache.setValue('cacheKey1', 'cacheValue1', 10000);
console.log(await cache.get('cacheKey1')); // 'cacheValue1'
}
run();
Conclusion
Memory Store Worker is a powerful tool for managing in-memory data in Node.js applications. Its efficient use of Worker Threads and main thread cache functionality offers robust performance for applications that need fast, transient data management without reliance on external storage.