npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@luudjanssen/localforage-cache

v0.0.4

Published

A wrapper around localforage adding experation times and cache invalidation methods

Downloads

383

Readme

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:

| 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.