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.
@luudjanssen/localforage-cache
Advanced tools
A wrapper around localforage adding experation times and cache invalidation methods
A wrapper around localforage adding experation times and cache invalidation methods
First install the module:
npm install @luudjanssen/localforage-cache
Then, import the module and create a new cache instance:
import cache from "@luudjanssen/localforage-cache"
const productCache = cache.createInstance({
name: "products-cache",
defaultExpiration: 1000
})
The defaultExpiration
is the default expiration time of the items saved to the cache. In the example we have 10 minutes, this means that all items saved to the cache will expire in 1 second (1000 milliseconds). The default expiration time is "Infinity", which would give you the same behaviour as using the original localforage module.
Now, you can save items to the cache and retreive them:
// Save an item to the cache
await productCache.setItem("product-1", { stock: 4, name: "Product 1" })
// Retreive it from the cache
const product1 = await productCache.getItem("product-1")
console.log('Product 1 returned from cache:', product1)
// Wait two seconds and retreive it again
setTimeout(() => {
const product1Expired = await productCache.getItem("product-1")
console.log('The cache has expired, so this should be null', product1Expired)
}, 2000)
The module works by saving an additional entry in the local storage for each item you save which states its expiration date. For example, the output in the storage driver of the first line of the example code in the database would be:
Key | Value |
---|---|
product-1 | { stock: 4 , name: "Product 1" } |
product-1_expires_a05fa06b | 1584277478393 |
The timestamp in the product-1_expires_a05fa06b
entry is 1000 (the defaultExpiration
) + the timestamp of the moment it was saved. If an item is retreived from the cache which is expired, both rows will be deleted and null
will be returned.
If you want more in depth knowledge of how the module works, take a look at the source.
FAQs
A wrapper around localforage adding experation times and cache invalidation methods
The npm package @luudjanssen/localforage-cache receives a total of 69 weekly downloads. As such, @luudjanssen/localforage-cache popularity was classified as not popular.
We found that @luudjanssen/localforage-cache 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.