Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
lru-cache-for-clusters-as-promised
Advanced tools
LRU Cache for Clusters as Promised provides a cluster-safe lru-cache
via Promises. For environments not using cluster
, the class will provide a Promisified interface to a standard lru-cache
.
Each time you call cluster.fork()
, a new thread is spawned to run your application. When using a load balancer even if a user is assigned a particular IP and port these values are shared between the workers
in your cluster, which means there is no guarantee that the user will use the same workers
between requests. Caching the same objects in multiple threads is not an efficient use of memory.
LRU Cache for Clusters as Promised stores a single lru-cache
on the master
thread which is accessed by the workers
via IPC messages. The same lru-cache
is shared between workers
having a common master
, so no memory is wasted.
To differentiate caches on the master as instances on the workers
, specify a namespace
value in the options argument of the new LRUCache(options)
constructor.
npm install --save lru-cache-for-clusters-as-promised
const LRUCache = require('lru-cache-for-clusters-as-promised');
const cache = new LRUCache({
max: 50,
stale: false,
namespace: 'users',
});
const user = { name: 'user name' };
const key = 'userKey';
// set a user for a the key
cache.set(key, user)
.then(() => {
console.log('set the user to the cache');
// get the same user back out of the cache
return cache.get(key);
})
.then((cachedUser) => {
console.log('got the user from cache', cachedUser);
// check the number of users in the cache
return cache.length();
})
.then((size) => {
console.log('user cache size/length', size);
// remove all the items from the cache
return cache.reset();
})
.then(() => {
console.log('the user cache is empty');
// return user count, this will return the same value as calling length()
return cache.itemCount();
})
.then((size) => {
console.log('user cache size/itemCount', size);
});
namespace: string
max: number
maxAge: milliseconds
stale: true|false
! note that
length
anddispose
are missing as it is not possible to passfunctions
via IPC messages.
set(key, value)
get(key)
peek(key)
del(key)
has(key)
reset()
keys()
values()
dump()
prune()
length()
itemCount()
length()
.Clustered cache on master thread for clustered environments*
+-----+
+--------+ +---------------+ +---------+ +---------------+ # M T #
| +--> LRU Cache for +--> +--> Worker Sends +--># A H #
| Worker | | Clusters as | | Promise | | IPC Message | # S R #
| <--+ Promised <--+ <--+ to Master <---# T E #
+--------+ +---------------+ +---------+ +---------------+ # E A #
# R D #
v---------------------------------------------------------------+-----+
+-----+
* W T * +--------------+ +--------+ +-----------+
* O H *---> Master +--> +--> LRU Cache |
* R R * | IPC Message | | Master | | by |
* K E *<--+ Listener <--+ <--+ namespace |
* E A * +--------------+ +--------+ +-----------+
* R D *
+-----+
Promisified for non-clustered environments*
+---------------+ +---------------+ +---------+ +-----------+
| +--> LRU Cache for +--> +--> |
| Non-clustered | | Clusters as | | Promise | | LRU Cache |
| <--+ Promised <--+ <--+ |
+---------------+ +---------------+ +---------+ +-----------+
FAQs
LRU Cache that is safe for clusters, based on `lru-cache`. Save memory by only caching items on the main thread via a promisified interface.
We found that lru-cache-for-clusters-as-promised 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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.