What is memory-cache?
The memory-cache npm package is a simple in-memory caching solution for Node.js applications. It allows you to store and retrieve data quickly without the overhead of a database or external caching service. This can be particularly useful for caching frequently accessed data, reducing the load on your primary data sources, and improving application performance.
What are memory-cache's main functionalities?
Set a cache value
This feature allows you to store a value in the cache with a specified key. The value can be any data type, and it will be stored in memory for quick retrieval.
const cache = require('memory-cache');
cache.put('key', 'value');
Get a cache value
This feature allows you to retrieve a value from the cache using its key. If the key exists in the cache, the corresponding value will be returned; otherwise, it will return null.
const cache = require('memory-cache');
const value = cache.get('key');
Delete a cache value
This feature allows you to delete a value from the cache using its key. This can be useful for invalidating cache entries when the underlying data changes.
const cache = require('memory-cache');
cache.del('key');
Set a cache value with expiration
This feature allows you to store a value in the cache with a specified key and an expiration time in milliseconds. After the expiration time, the value will be automatically removed from the cache.
const cache = require('memory-cache');
cache.put('key', 'value', 10000);
Clear the cache
This feature allows you to clear all entries from the cache. This can be useful for resetting the cache or freeing up memory.
const cache = require('memory-cache');
cache.clear();
Other packages similar to memory-cache
node-cache
node-cache is another in-memory caching solution for Node.js. It offers similar functionality to memory-cache, including setting, getting, and deleting cache values, as well as setting expiration times. One key difference is that node-cache provides additional features such as event listeners for cache hits and misses, and the ability to store multiple values at once.
lru-cache
lru-cache is a caching solution that implements a Least Recently Used (LRU) algorithm. This means that it automatically removes the least recently accessed items when the cache reaches its maximum size. This can be useful for managing memory usage more effectively compared to memory-cache, which does not have built-in eviction policies.
cache-manager
cache-manager is a more advanced caching solution that supports multiple storage backends, including in-memory, Redis, and others. It provides a consistent API for interacting with different types of caches and offers features such as hierarchical caching and custom cache stores. This makes it more versatile than memory-cache, which is limited to in-memory storage.
memory-cache
A simple in-memory cache for node.js
Installation
npm install memory-cache --save
Usage
var cache = require('memory-cache');
cache.put('foo', 'bar');
console.log(cache.get('foo'));
cache.put('houdini', 'disappear', 100, function(key, value) {
console.log(key + ' did ' + value);
});
console.log('Houdini will now ' + cache.get('houdini'));
setTimeout(function() {
console.log('Houdini is ' + cache.get('houdini'));
}, 200);
var newCache = new cache.Cache();
newCache.put('foo', 'newbaz');
setTimeout(function() {
console.log('foo in old cache is ' + cache.get('foo'));
console.log('foo in new cache is ' + newCache.get('foo'));
}, 200);
which should print
bar
Houdini will now disappear
houdini did disappear
Houdini is null
foo in old cache is baz
foo in new cache is newbaz
API
put = function(key, value, time, timeoutCallback)
- Simply stores a value
- If time isn't passed in, it is stored forever
- Will actually remove the value in the specified time in ms (via
setTimeout
) - timeoutCallback is optional function fired after entry has expired with key and value passed (
function(key, value) {}
) - Returns the cached value
get = function(key)
- Retrieves a value for a given key
- If value isn't cached, returns
null
del = function(key)
- Deletes a key, returns a boolean specifying whether or not the key was deleted
clear = function()
size = function()
- Returns the current number of entries in the cache
memsize = function()
- Returns the number of entries taking up space in the cache
- Will usually
== size()
unless a setTimeout
removal went wrong
debug = function(bool)
- Turns on or off debugging
hits = function()
- Returns the number of cache hits (only monitored in debug mode)
misses = function()
- Returns the number of cache misses (only monitored in debug mode)
keys = function()
- Returns all the cache keys
exportJson = function()
- Returns a JSON string representing all the cache data
- Any timeoutCallbacks will be ignored
importJson = function(json: string, options: { skipDuplicates: boolean })
- Merges all the data from a previous call to
export
into the cache - Any existing entries before an
import
will remain in the cache - Any duplicate keys will be overwritten, unless
skipDuplicates
is true
- Any entries that would have expired since being exported will expire upon being imported (but their callbacks will not be invoked)
- Available
options
:
skipDuplicates
: If true
, any duplicate keys will be ignored when importing them. Defaults to false
.
- Returns the new size of the cache
Cache = function()
- Cache constructor
- note that
require('cache')
would return the default instance of Cache - while
require('cache').Cache
is the actual class
Note on Patches/Pull Requests
- Fork the project.
- Make your feature addition or bug fix.
- Send me a pull request.