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

storage-cache

v2.0.0

Published

A plugin to store Js item in cache, to allow reduction of ajax calls in runtime

Downloads

7

Readme

Storage-cache

Easily use the storage or session cache from at client browser, to use in you javascript app

  • Choose bettwen localStorage and sessionStorage
  • Define when value expires
  • Stores info & data in separate items
  • Define Custom unique keys for the storage variables

Install

npm install --save storage-cache

How to use

import CACHE from 'storage-cache
Defining Cache to use

The plugin uses the localStorage object as default, if you wish to change to sessionStorage execute the following code

CACHE.Config('storage', sessionStorage)

Insert in cache

To insert a item into cache, call the set method

  • token | required | string - unique string to identify your cache contents
  • data | required | string - the contents to save
  • expire | optional | integer - miliseconds to expire (default as 600000ms (10 minutes))
token   = 'My Unique string' ||  HashModule('My Unique string')
data    = 'My String' || JSON.stringify({name: 'myObject', 'type': object})
expires = (1200 * 1000) || 1200000  // 20 minutes
CACHE.set( token, JSON.stringify({name: 'itemName'}), (120 * 1000) )

Get from cache

To recover a item from the cache, ask for the token, if the item did expire, will return false and delete it

CACHE.get(token)

Force a cache content to reset

You may want to forget the cache of a item at the moment, or in the next time that item will be requested, for that call the following code

token    = The unique token
contents = The content to override, if null on next request will return false and delete it
CACHE.forceReset(token, contents)

Delete from cache

To remove a item from cache, call the clear method

CACHE.clear(token)

Example of integration

Let's integrate this package with a javascript api connection

not ready for use, just an example

function postToApi (endpoint, params, options) {

  return new Promise((resolve, reject) => {
    // Get the token
    let token = MD5('endpoint' + 'params' + ...)
    
    // Option is to set to you behavior default?
    if (options.cache.level === 'DEFAULT') {
      let content = CACHE.get(token)
      if (content) resolve(content)
    }
    
    // Without cache content or this call will not use cache
    [YourHttpClient].post()
    .then((response) => {
    
      // Do you want to record the response on the cache
      if (options.cache.level === 'DEFAULT' || options.cache.level === 'GET_AND_REPLACE') {
        CACHE.set(token, response, options.cache.expires)
      }
      
      // resolve the response to the code
      resolve(response)
      
    }).catch((error) => { reject(error) })
  })
}

And to call this method

 MY_API_MODULE.postToApi('my/unique/endpoint', {name: 'Jonh', surname: 'Doe'}, {
   cache: {
     level = 'DEFAULT', // get from cache, otherwise, do request and save it on cache
     expires = 1200000  // 20 minutes
   }
 })