
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Memored implements an in-memory shared cache to use in nodejs applications which uses cluster module.
Let's say you want your application to take advantage of multi-core CPUs using nodejs cluster module; you will be able to run several isolated processes which shared nothing but a communication channel with parent process. If you need a fast volatile cache, common solutions would create an in-memory map for every process you run, so you end up with the same data stored several times in your machine RAM.
Memored uses communication channel between master process and its workers to use a unique in-memory storage, reducing the amount of memory your application would use.
Install this module with npm:
npm install memored
Store and read values is straightforward:
var cluster = require('cluster'),
memored = require('memored');
if (cluster.isMaster) {
cluster.fork();
} else {
var han = {
firstname: 'Han',
lastname: 'Solo'
},
luke = {
firstname: 'Luke',
lastname: 'Skywalker'
};
// Store and read
memored.store('character1', han, function() {
console.log('Value stored!');
memored.read('character1', function(err, value) {
console.log('Read value:', value);
});
});
// You can also set a ttl (milliseconds)
memored.store('character2', luke, 1000, function(err, expirationTime) {
console.log('Value stored until:', new Date(expirationTime));
setTimeout(function() {
memored.read('character2', function(err, value) {
console.log('Value is gone?', value === undefined);
process.exit();
});
}, 1050);
});
}
By default, memored will evict cache entries (stored with ttl) passively. This is, when you read an expired entry, you will get no value on return and memored will delete the value from its internal cache.
You can also configure memored to actively evict expired entries every N milliseconds. For this to work, you need to pass the attribute purgeInterval to the setup function. This will trigger an internal function which looks for expired entries and deletes them from its internal cache.
Example:
var cluster = require('cluster'),
async = require('async'),
memored = require('memored');
if (cluster.isMaster) {
cluster.fork();
memored.setup({ purgeInterval: 500});
} else {
async.series({
storeValue: function(next) {
memored.store('key1', 'My simple string value', 100, next);
},
readCacheSize: function(next) {
memored.size(function(err, size) {
console.log('Current size is 1?', size === 1);
next();
});
},
wait: function(next) {
setTimeout(next, 600);
},
readCacheSizeAgain: function(next) {
memored.size(function(err, size) {
console.log('Current size is 0?', size === 0);
next();
});
}
}, process.exit);
}
Documentation for every module function:
This function is used to configure memored.
Arguments:
Example:
memored.setup({
purgeInterval: 15000,
logger: console
});
This function stores a value in the cache. It is intended to be called from a worker process.
Arguments:
Examples:
memored.store('key1', {firstname: 'Han', lastname: 'Solo'}, function() {
console.log('Value stored!');
});
memored.store('key2', ['a', 'b', 'c'], 15000, function(err, expirationTime) {
console.log('This value will expire on:', new Date(expirationTime));
});
This function stores several values in the cache. It is intended to be called from a worker process.
Arguments:
Examples:
var users = {
'user1': { name: 'Han Solo' },
'user2': { name: 'Princess Leia' },
'user3': { name: 'Luke Skywalker' }
};
memored.multiStore(users, function() {
console.log('Users saved');
});
memored.multiStore(users, 15000, function(err, expirationTime) {
console.log('First value will expire on:', new Date(expirationTime));
});
This function reads a value from the cache. It is intended to be called from a worker process.
Arguments:
Example:
memored.read('key1', function(err, value) {
console.log('Key1 value:', value);
});
memored.read('key1', function(err, value, expirationTime) {
console.log('Key1 value:', value);
console.log('Key1 expiration time:', new Date(expirationTime));
});
memored.read('unknownKey', function(err, value) {
console.log('No data read?', value === undefined);
});
This function reads several values from the cache. It is intended to be called from a worker process.
Arguments:
Example;
memored.multiRead(['key1', 'key2', 'unknownKey'], function(err, values) {
console.log('Key1 value:', values.key1.value);
console.log('Key1 expiration time:', values.key1.expirationTime);
console.log(Object.keys(values)); // ['key1', 'key2']
console.log('unknownKey:', values.unknownKey); // undefined
});
This function removes an entry from the cache. It is intended to be called from a worker process.
Arguments:
Example:
memored.remove('key1', function() {
console.log('Key removed from the cache.');
});
This function removes several entries from the cache. It is intended to be called from a worker process.
Arguments:
Example:
memored.multiRemove(['key1', 'key2', 'unknownKey'], function() {
console.log('Entries foundn in the cache has been removed.')
});
This function removes all the entries from the cache. It is intended to be called from a worker process.
Arguments:
Example:
memored.clean(function() {
console.log('All cache entries have been deleted.');
});
This function returns the number of entries in the cache.
Arguments:
Example:
memored.size(function(err, size) {
console.log('Cache size:', size);
});
This function returns an array of the keys for objects in the cache.
Arguments:
Example:
memored.keys(function(err, keys) {
console.log('Cache keys:', keys);
});
### version
This is an attribute which provides module's version number
All the callbacks first parameter is an optional error object. Actually, this param will never be an error because there is no expected error in the internal code. There's no function call that can possible throw an expected error that this module would deal with. The existence of this param is to follow the convention about libraries callbacks in nodejs. As everybody expects this first callback parameter to be an optional one, I decided to include it.
Copyright (c) 2014 PaquitoSoft
Licensed under the MIT license.
FAQs
Shared in-memory module for cluster applications
The npm package memored receives a total of 2,686 weekly downloads. As such, memored popularity was classified as popular.
We found that memored demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.