New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@luudjanssen/localforage-cache

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@luudjanssen/localforage-cache

A wrapper around localforage adding experation times and cache invalidation methods

  • 0.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
94
decreased by-21.01%
Maintainers
1
Weekly downloads
 
Created
Source

localforage-cache

A wrapper around localforage adding experation times and cache invalidation methods

Usage

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)

How does it work?

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:

KeyValue
product-1{ stock: 4 , name: "Product 1" }
product-1_expires_a05fa06b1584277478393

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.

Keywords

FAQs

Package last updated on 15 Mar 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc