A caching library for JavaScript and TypeScript that simplifies caching operations in your applications.
You can install npm-cache-it
via npm or yarn:
npm install npm-cache-it
# or
yarn add npm-cache-it
To use npm-cache-it
in your JavaScript or TypeScript project, follow these steps:
const {
getCachedData,
putDataInCache,
putDataInCacheWithTTL,
clearCacheData,
} = require('npm-cache-it');
Cache data with an optional time-to-live (TTL) of 0 seconds.
putDataInCache('myPrefix', 'myKey', 'someData');
Cache data with a specified time-to-live (TTL) in seconds.
putDataInCacheWithTTL('myPrefix', 'myKey', 'someData', 60);
Retrieve cached data from the cache.
const cachedData = getCachedData('myPrefix', 'myKey');
Clear cached data.
clearCacheData('myPrefix', 'myKey');
const prefix = 'myPrefix';
const key = 'myKey';
const compoundKey = getCompoundKey(prefix, key);
console.log(`Compound Cache Key: ${compoundKey}`);
const prefix = 'myPrefix';
const key = 'myKey';
const isExpired = checkCacheExpiry(prefix, key);
console.log(`Key ${key} has expired: ${isExpired}`);
const prefix = 'myPrefix';
const key = 'myKey';
const seconds = 3600; // 1 hour
const isSet = setCacheTTL(prefix, key, seconds);
console.log(`TTL for key ${key} set: ${isSet}`);
incrementCacheValue
: Increment the value associated with a specific key in the cache by a specified delta.
const prefix = 'myPrefix';
const key = 'myKey';
const delta = 5;
const newValue = incrementCacheValue(prefix, key, delta);
console.log(`New value for key ${key}: ${newValue}`);
decrementCacheValue
: Decrement the value associated with a specific key in the cache by a specified delta.
const prefix = 'myPrefix';
const key = 'myKey';
const delta = 2;
const newValue = decrementCacheValue(prefix, key, delta);
console.log(`New value for key ${key}: ${newValue}`);
const prefix = 'myPrefix';
const matchingKeys = getKeysWithPrefix(prefix);
console.log(`Cache keys matching prefix ${prefix}: ${matchingKeys.join(', ')}`);
const prefix = 'myPrefix';
flushCacheWithPrefix(prefix);
console.log(`All cache entries with prefix ${prefix} have been flushed.`);
const allKeys = getAllCacheKeys();
console.log(`All cache keys: ${allKeys.join(', ')}`);
flushAll()
Replace 'myPrefix' and 'myKey' with your specific cache prefix and key when using these functions. These functions allow you to easily cache and retrieve data in your JavaScript or TypeScript applications.
addElementToSet('myPrefix', 'mySetKey', 'someElement');
addElementsToSet('myPrefix', 'mySetKey', ['element1', 'element2', 'element3']);
addElementToSetWithTTL('myPrefix', 'mySetKey', 'someElement', 3600); // Cache with a TTL of 1 hour
addElementsToSetWithTTL
**: Add multiple elements to a Set within the cache with a Time-To-Live (TTL).
addElementsToSetWithTTL('myPrefix', 'mySetKey', ['element1', 'element2'], 1800); // Cache with a TTL of
removeElementFromSet('myPrefix', 'mySetKey', 'elementToRemove');
removeElementsFromSet('myPrefix', 'mySetKey', ['element1', 'element2']);
clearSet('myPrefix', 'mySetKey');
const elements = getAllElementsInSet('myPrefix', 'mySetKey');
const setSize = getSetSize('myPrefix', 'mySetKey');
const exists = hasElementInSet('myPrefix', 'mySetKey', 'elementToCheck');
unionSets('myPrefix', ['sourceKey1', 'sourceKey2'], 'mergedSetKey');
const commonElements = intersectSets('myPrefix', ['sourceKey1', 'sourceKey2']);
const differingElements = differenceSets('myPrefix', 'sourceKeyA', 'sourceKeyB');
addToHashSet('myPrefix', 'myHashSetKey', 'field1', 'value1');
addToHashSetWithTTL('myPrefix', 'myHashSetKey', 'field1', 'value1', 3600); // Cache with a TTL of 1 hour
const value = getFromHashSet('myPrefix', 'myHashSetKey', 'field1');
const fieldNames = getFieldNamesFromHashSet('myPrefix', 'myHashSetKey');
const values = getValuesFromHashSet('myPrefix', 'myHashSetKey');
removeFromHashSet('myPrefix', 'myHashSetKey', 'field1');
clearHashSet('myPrefix', 'myHashSetKey');
const size = getHashSetSize('myPrefix', 'myHashSetKey');
Certainly! Here's a summary of the provided cache operations with function names and one-liner descriptions:
madd([{ key: 'key1', val: 'value1' }, { key: 'key2', val: 'value2' }]);
maddWithTTL
: Add an array of key-value pairs to the cache with an optional Time-to-Live (TTL) for each pair.
maddWithTTL([{ key: 'key1', val: 'value1', ttl: 3600 }, { key: 'key2', val: 'value2' }]);
mget(['key1', 'key2']);
mremove(['key1', 'key2']);
const size = mgetSize();
const exists = misKeyExists('key1');
Retrieve cached data from the cache.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key to identify the cached data.
Cache data with an optional time-to-live (TTL) of 0 seconds.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key to identify the cached data. -
data (any)
: The data to be cached.
Cache data with a specified time-to-live (TTL) in seconds.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key to identify the cached data. -
data (any)
: The data to be cached. -
seconds (number)
: The TTL in seconds.
Clear cached data.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key to identify the cached data.
Generates a compound cache key by combining a prefix and a key.
-
prefix (string)
: The prefix for the cache key. -
key (string)
: The specific key. -
{string}
: Return the compound cache key.
Checks if a specific key has expired in the cache.
-
prefix (string)
: The prefix for the cache key. -
key (string)
: The specific key to check for expiration.* -
{boolean}
: Returns true if the key has expired, otherwise false.
Sets the Time-to-Live (TTL) for a specific key in the cache.
-
prefix (string)
: The prefix for the cache key. -
key (string)
: The specific key to set TTL for. -
seconds (number)
: Time-to-Live (TTL) in seconds. Use 0 for no expiration. -
{boolean}
: Returns true if the TTL was set successfully, otherwise false.
Increments the value associated with a specific key in the cache by a specified delta.
-
prefix (string)
: The prefix for the cache key. -
key (string)
: The specific key to increment the value for. -
delta (number)
: The amount by which to increment the value. -
{number | undefined}
: The new value associated with the key after incrementing, or undefined if the key was not found.
Decrements the value associated with a specific key in the cache by a specified delta.
-
prefix (string)
: The prefix for the cache key. -
key (string)
: The specific key to decrement the value for. -
delta (number)
: The amount by which to decrement the value. -
{number | undefined}
: The new value associated with the key after decrementing, or undefined if the key was not found.
Gets an array of cache keys that match a specified prefix pattern.
-
prefix (string)
: The prefix to search for in cache keys. -
{string[]}
: Returns an array of cache keys matching the specified prefix pattern.
Flushes all cache entries with a specific prefix.
-
prefix (string)
: The prefix for the cache entries to flush.
Gets an array of all cache keys.
-
{string[]}
: Returns An array of all cache keys.
Flushes all cache entries .
Add a single element to a Set within the cache. If the Set does not exist, it will be created.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the Set. -
element (T)
: The element to add to the Set. - Returns
boolean
: Returnstrue
if the element was added successfully, otherwisefalse
.
Add multiple elements to a Set within the cache.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the Set. -
elements (T[])
: An array of elements to add to the Set. - Returns
boolean
: Returnstrue
if all elements were added successfully, otherwisefalse
.
Add a single element to a Set within the cache with a Time-to-Live (TTL). If the Set does not exist, it will be created.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the Set. -
element (T)
: The element to add to the Set. -
seconds (number)
: Time-to-Live (TTL) in seconds. Use0
for no expiration. - Returns
boolean
: Returnstrue
if the element was added successfully, otherwisefalse
.
Add multiple elements to a Set within the cache with a Time-To-Live (TTL).
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the Set. -
elements (T[])
: An array of elements to add to the Set. -
ttl (number)
: Time-To-Live (TTL) in seconds for the cached Set. - Returns
boolean
: Returnstrue
if all elements were added successfully, otherwisefalse
.
Remove a specific element from a Set within the cache.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the Set. -
element (T)
: The element to remove from the Set. - Returns
boolean
: Returnstrue
if the element was successfully removed, otherwisefalse
.
Remove multiple elements from a Set within the cache.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the Set. -
elements (T[])
: An array of elements to remove from the Set. - Returns
boolean
: Returnstrue
if all elements were removed successfully, otherwisefalse
.
Clear (remove) all elements from a Set within the cache.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the Set. - Returns
boolean
: Returnstrue
if the Set was cleared successfully, otherwisefalse
.
Retrieve all elements from a Set within the cache.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the Set. - Returns
Set<T> | undefined
: The Set containing all elements, orundefined
if not found.
Retrieve the size (number of elements) of a Set stored in the cache.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the Set. - Returns
number
: The size of the Set in the cache.
Check if a specific element exists in a Set within the cache.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the Set. -
element (T)
: The element to check for existence. - Returns
boolean
: Returnstrue
if the element exists in the Set, otherwisefalse
.
Merge two or more sets from different keys into a new set.
-
prefix (string)
: A string prefix for the new cache key. -
sourceKeys (string[])
: An array of source keys to merge sets from. - Returns
boolean
: Returnstrue
if the merge operation was successful, otherwisefalse
.
Find the common elements between two or more sets.
-
prefix (string)
: A string prefix for the cache keys. -
sourceKeys (string[])
: An array of source keys to find the intersection of sets. - Returns
Set<T> | undefined
: The set containing common elements, orundefined
if not found.
Find the elements that are in one set but not in another.
-
prefix (string)
: A string prefix for the cache keys. -
sourceKeyA (string)
: The source key for the first set. -
sourceKeyB (string)
: The source key for the second set. - Returns
Set<T> | undefined
: The set containing elements that are in set A but not in set B, orundefined
if not found.
Add a key-value pair to a HashSet in the cache without Time-to-Live (TTL).
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the HashSet. -
field (string)
: The field name within the HashSet. -
value (any)
: The value associated with the field. - Returns
boolean
: Returnstrue
if the key-value pair was added successfully, otherwisefalse
.
addToHashSetWithTTL(prefix: string, key: string, field: string, value: any, seconds: number): boolean
Add a key-value pair to a HashSet in the cache with Time-to-Live (TTL).
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the HashSet. -
field (string)
: The field name within the HashSet. -
value (any)
: The value associated with the field. -
seconds (number)
: Time-to-Live (TTL) in seconds. Use0
for no expiration. - Returns
boolean
: Returnstrue
if the key-value pair was added successfully, otherwisefalse
.
Get the value associated with a field in a HashSet.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the HashSet. -
field (string)
: The field name within the HashSet. - Returns
any
: The value associated with the field, orundefined
if not found.
Get all field names from a HashSet in the cache.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the HashSet. - Returns
string[]
: An array of all field names in the HashSet.
Get all values from a HashSet in the cache.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the HashSet. - Returns
any[]
: An array of all values in the HashSet.
Remove a field from a HashSet.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the HashSet. -
field (string)
: The field name within the HashSet to remove. - Returns
boolean
: Returnstrue
if the field was removed successfully, otherwisefalse
.
Clear (remove) all fields from a HashSet in the cache.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the HashSet. - Returns
boolean
: Returnstrue
if the HashSet was cleared successfully, otherwisefalse
.
Get the size (number of fields) of a HashSet in the cache.
-
prefix (string)
: A string prefix for the cache key. -
key (string)
: The key for the HashSet. - Returns
number
: The size (number of fields) of the HashSet.
Add key-value pairs to the cache without Time-to-Live (TTL).
-
items (Array<{ key: string, val: any }>)
: An array of objects with key-value pairs to add to the cache. - Returns
boolean
: Returnstrue
if all key-value pairs were added successfully.
Add key-value pairs to the cache with optional Time-to-Live (TTL).
-
items (Array<{ key: string, val: any, ttl?: number }>)
: An array of objects with key-value pairs to add to the cache. Each object can optionally specify attl
property for Time-to-Live in seconds. - Returns
boolean
: Returnstrue
if all key-value pairs were added successfully.
Get the values associated with an array of keys from the cache.
-
keys (Array<string>)
: An array of keys to retrieve values from the cache. - Returns
Array<any>
: An array of values corresponding to the keys.
Remove key-value pairs from the cache.
-
keys (Array<string>)
: An array of keys to remove from the cache. - Returns
boolean
: Returnstrue
if all keys were removed successfully.
Get the size (number of key-value pairs) in the cache.
- Returns
number
: The size (number of key-value pairs) in the cache.
Check if a key exists in the cache.
-
key (string)
: The key to check for existence in the cache. - Returns
boolean
: Returnstrue
if the key exists in the cache, otherwisefalse
.
Contributions to npm-cache-it
are welcome! If you'd like to contribute to this project, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix:
git checkout -b feature/your-feature-name.
- Commit your changes:
git commit -m "Add your feature or fix".
- Push your branch to your fork:
git push origin feature/your-feature-name.
- Create a pull request on the original repository.
Please follow the Code of Conduct and Contributing Guidelines when contributing.