node in memory cache is a lightweight Node.js package that provides a Redis-like caching system, offering persistent key-value storage with optional expiration times. It simplifies the management of strings, lists, sets, and hashes, all stored persistently on disk.
- String Operations: Set and retrieve string values with optional expiration times.
- List Operations: Push and pop elements from lists at the front or back.
- Set Operations: Add, remove, and list members of sets.
- Hash Operations: Store, retrieve, and delete fields within hashes.
- Persistent Storage: Automatically saves data to disk for durability across sessions.
- Expiration: Supports setting expiration times for keys.
Install via npm:
npm install node-in-memory-cache
import {nodejsCache} from 'node-in-memory-cache'
Sets a string value with an optional expiration time.
nodejsCache.set_string('key1', 'value1', '60'); // Set 'key1' with value 'value1' and expiration time of 60 seconds
Retrieves a string value based on the key.
const value = nodejsCache.get_string('key1'); // Retrieve value of 'key1'
console.log(value); // Outputs: 'value1' or 'nil' if expired or not found
Pushes an element to the front of a list.
nodejsCache.list_push_front('myList', 'value2'); // Push 'value2' to the front of 'myList'
Pushes an element to the back of a list.
nodejsCache.list_push_back('myList', 'value3'); // Push 'value3' to the back of 'myList'
Pops an element from the front of a list.
const poppedValue = nodejsCache.list_pop_front('myList'); // Pop from the front of 'myList'
console.log(poppedValue); // Outputs: 'value2' or 'nil' if list is empty
Pops an element from the back of a list.
const poppedValue = nodejsCache.list_pop_back('myList'); // Pop from the back of 'myList'
console.log(poppedValue); // Outputs: 'value3' or 'nil' if list is empty
Adds an element to a set.
nodejsCache.set_add('mySet', 'value4'); // Add 'value4' to 'mySet'
Removes an element from a set.
nodejsCache.set_remove('mySet', 'value4'); // Remove 'value4' from 'mySet'
Retrieves all members of a set.
const setMembers = nodejsCache.set_members('mySet'); // Retrieve members of 'mySet'
console.log(setMembers);
Sets a field within a hash.
nodejsCache.hash_set_field('myHash', 'field1', 'value5'); // Set 'field1' with 'value5' in 'myHash'
Retrieves the value of a field within a hash.
const hashValue = nodejsCache.hash_get_field('myHash', 'field1'); // Retrieve value of 'field1' in 'myHash'
console.log(hashValue);
Deletes a field from a hash.
nodejsCache.hash_delete_field('myHash', 'field1'); // Delete 'field1' from 'myHash'
Retrieves all fields and values from a hash.
const allFields = nodejsCache.hash_get_all('myHash'); // Retrieve all fields and values from 'myHash'
console.log(allFields);
Contributions are welcome! Feel free to open issues or submit pull requests.
This project is licensed under the MIT License. See the LICENSE file for more details.