Installations
npm install redis-lru
Releases
Unable to fetch releases
Developer
facundoolano
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
9.9.0
NPM Version
5.6.0
Statistics
96 Stars
43 Commits
13 Forks
5 Watching
5 Branches
5 Contributors
Updated on 26 Jun 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
7,973,233
Last day
56.3%
98,082
Compared to previous day
Last week
14.8%
410,673
Compared to previous week
Last month
6.2%
1,432,830
Compared to previous month
Last year
323.5%
4,538,524
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
redis lru cache
A least recently used (LRU) cache backed by Redis, allowing data to be shared by multiple Node.JS processes. API inspired by node-lru-cache.
1var redis = require('redis').createClient(port, host, opts); 2var lru = require('redis-lru'); 3 4var personCache = lru(redis, 5); // up to 5 items 5 6personCache.set('john', {name: 'John Doe', age: 27}) 7 .then(() => personCache.set('jane', {name: 'Jane Doe', age: 30})) 8 .then(() => personCache.get('john')) 9 .then(console.log) // prints {name: 'John Doe', age: 27} 10 .then(() => personCache.reset()) //clear the cache 11 12var bandCache = lru(redis, {max: 2, namespace: 'bands', maxAge: 15000}); // use a different namespace and set expiration 13 14bandCache.set('beatles', 'john, paul, george, ringo') 15 .then(() => bandCache.set('zeppelin', 'jimmy, robert, john, bonzo')) 16 .then(() => bandCache.get('beatles')) // now beatles are the most recently accessed 17 .then(console.log) // 'john, paul, george, ringo' 18 .then(() => bandCache.set('floyd', 'david, roger, syd, richard, nick')) // cache full, remove least recently accessed 19 .then(() => bandCache.get('zeppelin')) 20 .then(console.log) // null, was evicted from cache
Works both with node_redis and ioredis clients.
Installation
npm install redis-lru
Options
max
: Maximum amount of items the cache can hold. This option is required; if no other option is needed, it can be passed directly as the second parameter when creating the cache.namespace
: Prefix appended to all keys saved in Redis, to avoid clashes with other applications and to allow multiple instances of the cache.maxAge
: Maximum amount of milliseconds the key will be kept in the cache; after that getting/peeking will resolve tonull
. Note that the value will be removed from Redis aftermaxAge
, but the key will be kept in the cache until next time it's accessed (i.e. it will be included incount
,keys
, etc., although not inhas
.).score
: function to customize the score used to order the elements in the cache. Defaults to() => new Date().getTime()
increment
: iftrue
, on each access the result of thescore
function is added to the previous one, rather than replacing it.
API
All methods return a Promise.
set(key, value, maxAge)
: set value for the given key, marking it as the most recently accessed one. Keys should be strings, values will be JSON.stringified. The optionalmaxAge
overrides for this specific key the global expiration of the cache.get(key)
: resolve to the value stored in the cache for the given key ornull
if not present. If present, the key will be marked as the most recently accessed one.getOrSet(key, fn, maxAge)
: resolve to the value stored in the cache for the given key. If not present, executefn
, save the result in the cache and return it.fn
should be a no args function that returns a value or a promise. IfmaxAge
is passed, it will be used only if the key is not already in the cache.peek(key)
: resolve to the value stored in the cache for the given key, without changing its last accessed time.del(key)
: removes the item from the cache.reset()
: empties the cache.has(key)
: resolves to true if the given key is present in the cache.keys()
: resolves to an array of keys in the cache, sorted from most to least recently accessed.values()
: resolves to an array of values in the cache, sorted from most to least recently accessed.count()
: resolves to the number of items currently in the cache.
Using as a LFU cache
By using a custom score
function and the increment
option, one can turn the cache
into a least frequently used (LFU), where the items that have been accessed more times
(rather than most recently) are preserved:
1var redis = require('redis').createClient(port, host, opts); 2var lru = require('redis-lru'); 3 4var bandLfu = lru(redis, {max: 2, score: () => 1, increment: true}); 5 6bandLfu.set('beatles', 'john, paul, george, ringo') 7 .then(() => bandLfu.get('beatles')) // accessed twice 8 .then(() => bandLfu.set('zeppelin', 'jimmy, robert, john, bonzo')) 9 .then(() => bandLfu.set('floyd', 'david, roger, syd, richard, nick')) // cache full, remove least frequently accessed 10 .then(() => bandLfu.get('zeppelin')) 11 .then(console.log) // null, was evicted from cache
Implementation
Each item in the cache is stored as a regular key/value in Redis. Additionally, a ZSET is used to keep an index of the keys sorted by last-accessed timestamp.
Requires Redis 3.0.2 or greater, since it uses the XX option of ZADD.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 4/18 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 16 are checked with a SAST tool
Reason
17 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-9vvw-cc9w-f27h
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-h6ch-v84p-w6p9
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-qh2h-chj9-jffq
- Warn: Project is vulnerable to: GHSA-282f-qqgm-c34q
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-hxm2-r34f-qmc5
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-w9mr-4mfr-499f
- Warn: Project is vulnerable to: GHSA-35q2-47q7-3pc3
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
Score
1.6
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More