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

z-multi-cache

v0.0.20

Published

this is a caching tool that uses localStorage, sessionStorage, memory storage in combination to provide more convenient get data from the cache

Downloads

17

Readme

this is a caching tool that uses localStorage, sessionStorage, url in combination to provide more convenient get data from the cache

Install

npm i z-multi-cache --save

Use

method 1: use default store

    import store from 'z-multi-cache';

    const optObj = {};

    // setItem
    store.setItem('city', 'beijing');
    // getItem
    const city = store.getItem('city');

    // you can personalize optObj. store will use default value without optObj.

method 2: use factory to create a store with config

Api

store.setItem(key, val, optObj);

factory(

opts = {
    ns: 'project namespace',
    strict: true, // whether to open strict mode.
    getterStrict: true, // whether to open strict mode when use getItem.
    template: simpleObj, // verification template in strict mode.
    scopeSeparator: '/' // the split symbol used by the scope value. introduced later.
})
    import { factory } from 'z-multi-cache';

    const template = {
        globalKeys: [
            'cityName',
            'arrDate'
        ],
        pages: [
            'home',
            'hotelList',
            'hotelDetail',
            'orderList',
            'orderDetail'
        ]
    };

    const store = factory({
        ns: "projectA",
        getterStrict: true,
        strict: true,
        template
    });

    //-- use
    
    // in mobx, we create a decorator. when the value change, the changed value will be stored automatically.
    export function storeSet(key, opts) {
        return function(clazz) {
            const proxy = new Proxy(clazz, {
                construct: function(target, args) {
                    const obj = Reflect.construct(target, args);
                    autorun(() => {
                        store.setItem(key, toJS(obj[key]), opts);
                    });
                    return obj;
                }
            });
            return proxy;
        };
    }

    // used in mobx
    @storeSet('cityName', { scope: 'global' })
    class DataStore {
        @observable
        cityName = store.getItem('cityName', { scope: 'global', default: 'beijing' } //default can be basic type, or a function. default will be return value of the funciton.

        @aciton.bound
        changeCityName(val) {
            this.cityName = val;
        }
    }

    // getItem, in localStorage, get value from key: 'defaultKey', in sessionStorage get value from key: 'ssKey'
    store.getItem('defaultKey', {scope: 'global', type: ['localStorage', {type: 'sessionStorage', key: 'ssKey' }] })

    // getItem according to the priorities of "localStorage", "sessionStorage", and "urlSearch". the default value is "2018-12-12" while the result of getItem is undefined. if getItem from urlSearch, the key is arrDate
    store.getItem('theDate', {
        scope: 'global',
        type: ['localStorage', 'sessionStorage', { type: 'urlSearch', key: 'arrDate'}],
        default: function() {  return '2018-12-12' } // default can also be a string: '2018-12-12'
    });

    // update search part of the url
    // if 'theCityName' in the search part of url, then the value from storage of key: cityName will update 'theCityName'
    store.getItem('cityName', {scope: 'global', updateSearchUrlKey: 'theCityName' })
    store.getItem('cityName', {scope: 'global', updateSearchUrlKey: 'theCityName' })

    // updateUrlSearch
    store.updateUrlSearch({
        cityNameKeyInSearchOfUrl: ['cityName', {  scope: 'global', type: 'localStorage' }],
        arrDateKeyOfSearch: ['arrDate', {scope: 'global', type: ['sessionStorage', 'localStorage'] }] // first get value from sessionStorage
    });

    // updateUrlSearch asynchronously, the search part of the url will be updated after 100ms.
    store.updateUrlSearch({
        data: "2018-12-12",
        arrDateKeyOfSearch: ['arrDate', {scope: 'global', type: ['sessionStorage', 'localStorage'] }] // first get value from sessionStorage
    }, 100);

    // updateUrlSearch when setItem or getItem
    store.setItem('cityName', { updateSearchUrlKey: 'city', updateSearchUrlKeyTime: 400 }); // update url after 400ms, the default scope is "global" and default type is "sessionStorage"

    store.getItem('arrDate', { updateSearchUrlKey: 'arrDate', updateSearchUrlKeyTime: 500 }); // update url after 500ms

    // url utils

    const { getUrlParam, updateUrlSearch } = store;
    const urlParamObj = getUrlParam();
    const cityName = getUrlParam('cityName');
    
    const _setTimeout = 500;
    updateUrlSearch({
        cityName: 'beijing',
        arrDate: '2018-12-12'
    }, _setTimeout); // if _setTimeout is undefined, the updateUrlSearch will be invoked synchronously.