Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
lock_and_cache_manager
Advanced tools
Most caching libraries don't do locking, meaning that >1 process can be calculating a cached value at the same time. Since you presumably cache things because they cost CPU, database reads, or money, doesn't it make sense to lock while caching?
Lock and cache using redis!
Most caching libraries don't do locking, meaning that >1 process can be calculating a cached value at the same time. Since you presumably cache things because they cost CPU, database reads, or money, doesn't it make sense to lock while caching?
const lnc = require('lock_and_cache')
let cache = lock_and_cache.LockAndCache()
// standalone mode
function getStockQuote (symbol) {
return cache.get(['stock', symbol], 60, async function () {
// fetch stock price from remote source, cache for one minute
// calling this multiple times in parallel will only run it once
})
}
// wrap mode
const getStockQuote = cache.wrap(60, async function stock (symbol) {
// fetch stock price from remote source, cache for one minute
// calling this multiple times in parallel will only run it once
// the cache key is based on the function name and arguments
})
// custom options w immediate closing
// options are passed thru to cache manager which passes thru to backends
const customCache = new lnc.LockAndCache({
caches: cache.tieredCache({...memOpts}, {...redisOpts})
})
closing(customCache, async function (cache) {
const value = cache.get(...)
const wrapped = cache.wrap(...)
cache.del(key)
...
})
npm install --save lock_and_cache
lock_and_cache...
As you can see, most caching libraries only take care of (1) and (4) (well, and (5) of course).
Just setting REDIS_URL in your environment is enough.
REDIS_URL=redis://redis:6379/2
If you want to put the cache and the locks in different places (which is useful if you want to invalidate the cache without messing with any locks currently held), you can do:
CACHE_URL=redis://redis:6379/2 LOCK_URL=redis://redis:6379/3
Distributed locking is supported and uses redlock.
const lockAndCache = require('lock_and_cache')
const cache = lockAndCache.configure([
'redis://client-1',
'redis://client-2',
'redis://client-3'
])
All clients you specify will be used for locking, but only the first will be used for caching.
lockAndCache (mixed key, number ttl, function work)
lockAndCache.configure (mixed client(s)[, object options])
The LOCK_DRIFT_FACTOR
, LOCK_RETRY_COUNT
, and LOCK_RETRY_DELAY
environment
variables can be set to configure the global lockAndCache function's locking
behavior. You probably don't need this unless the jobs you are caching are
either very fast or very slow. By default, the global lockAndCache function
will retry every 100ms for one hour using the standard drift factor.
Please send me your pull requests!
FAQs
Most caching libraries don't do locking, meaning that >1 process can be calculating a cached value at the same time. Since you presumably cache things because they cost CPU, database reads, or money, doesn't it make sense to lock while caching?
The npm package lock_and_cache_manager receives a total of 1 weekly downloads. As such, lock_and_cache_manager popularity was classified as not popular.
We found that lock_and_cache_manager 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.