Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
This project aims to be fast and smart. It currently sets itself apart from existing LRU caches in its ability to evict data from the cache once it fills a given amount of memory, rather than requiring a specific number of allowed cache entries. Cached data can vary in size and it is generally desirable to cache as much data as memory will allow.
npm install smart-lru
var SmartLRU = require('smart-lru');
var cache = new SmartLRU({
inactiveTTL: 86400, // Keep unused objects in the cache for 1 day
maxSize: '80%', // Don't use more than 80% of the system memory
maxKeys: 5000 // Don't allow more than 5000 keys in the cache
});
cache.set('test', {someKey: 5});
cache.get('test'); // Returns {someKey: 5}
Options and defaults
inactiveTTL
: integer, default 0 (no TTL). The default number of seconds unused cached objects should remain in the cache.maxSize
: string, default 50m
. The maximum amount of memory the cache should use. Size can be specified as a percentage with as suffix of '%', as a number in bytes, or as a string with a suffix of 'k', 'm' or 'g'. The suffix is not case sensitive.maxKeys
: integer, default 0 (unlimited). The maximum number of keys allows in the cache, regardless of whether it has reached maxSize
.Get the value of the given cache key. Returns undefined
if the key is not set. This is important since null
is a valid cacheable value.
If touch
is passed and set to false
it will return the value without resetting its TTL or its place in line for eviction from the cache.
Set a value in the cache. The value
can be anything, but if it is not a Buffer
then it will be JSON encoded before being stored. It will be decoded on get
so that part is transparent, but it will affect how much memory the value uses. In the future this may be able to measure object sizes properly and store them in their native form. Although, in some cases the JSON encoded value can be smaller.
The ttl
is optional and will be defaulted to the inactiveTTL
specified in the constructor options if not given. When you use this method it will completely replace any existing entry under the given key and reset its TTL. Passing a value of 0
to the optional ttl
parameter will cause the given value to have no TTL even if there is a default inactiveTTL
set.
Reset the ttl
and eviction queue position for the given key.
Returns true
if the given key exists in the cache, false
otherwise. This should be used before a call to update()
if you are not completely sure the key exists.
Update the value of a key without resetting its TTL or changing its place in line for eviction from the cache. Note that this will throw an exception of the given key does not exist in the cache.
Delete the given cache key.
Clear the entire LRU cache.
FAQs
Simple, fast, smart and flexible Least Recently Used (LRU) cache
The npm package smart-lru receives a total of 1 weekly downloads. As such, smart-lru popularity was classified as not popular.
We found that smart-lru 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
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.