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 🙏

© 2026 – Pkg Stats / Ryan Hefner

cache-in-storage

v3.1.8

Published

前端缓存工具

Readme

前端Cache工具

coveralls travis

前端经常碰到需要缓存一些方法的情况(比如接口,wx.login等),有些情况下,数据还需要落地(localStorage,wx的本地缓存等)。

安装

npm i cache-in-storage

快速使用

import { cacheDec } from "cache-in-storage";

const getTimeSpan = async (isError = false) => {
  return new Promise((resolve, _reject) => {
    setTimeout(() => {
      if (isError) {
        return _reject(new Error("test"));
      }
      resolve(Date.now());
    }, 200);
  });
};

// 对getTimeSpan进行缓存,并把缓存结果放入localStorage
const getTimeSpanWithCache = cacheDec(getTimeSpan, "keyInCache", { cache:true }, localStorage);

// 此时执行方法返回的值都是相同的
getTimeSpanWithCache().then(console.log);
getTimeSpanWithCache().then(console.log);
getTimeSpanWithCache(true).catch(console.error);

API

cacheDec: (func: (...args: any[]) => Promise<any>, key: string, settings: CacheOptionModel, storage?: CacheNStorage, promiseStorage?: CacheNStorage): (...args: any[]) => Promise<any>

一个高阶函数,包装一个Promise方法,返回一个方法((...args)=>Promise);

参数说明

| 参数名 | 类型 | 说明 | | -------- | ------------------ | ------------------ | | func | (...args)=>Promise | 需要包装缓存的方法 | | key | String | 缓存的key值 | | storage | CacheNStorage | 缓存的类 | | settings | CacheOptionModel | 缓存设置 |

getCacheInWithKey: getCacheInWithKey = (key: string, storage?: Storage): number

返回key值在进入缓存的时间戳,如果不存在键值则返回0。

| 参数名 | 类型 | 说明 | | -------- | ------------------ | ------------------ | | key | String | 缓存的key值 | | storage | CacheNStorage | 缓存的类 |

getDataFromStorage: (key: string, storage: Storage): CacheDataModel | null

从缓存类中获取数据,如果不存在缓存,则返回null。

| 参数名 | 类型 | 说明 | | -------- | ------------------ | ------------------ | | key | String | 缓存的key值 | | storage | CacheNStorage | 缓存的类 |

removeCacheFromKey: (key: string, storage: CacheNStorage): void

从缓存类中删除一个键值。

| 参数名 | 类型 | 说明 | | -------- | ------------------ | ------------------ | | key | String | 缓存的key值 | | storage | CacheNStorage | 缓存的类 |

removeCacheFromRegexp: (regexp: RegExp, storage: CacheNStorage): void

根据正则匹配,从缓存类中删除匹配的键值。

| 参数名 | 类型 | 说明 | | -------- | ------------------ | ------------------ | | key | String | 缓存的key值 | | storage | CacheNStorage | 缓存的类 |

BaseFactory

继承自CacheNStorage接口,提供了操作缓存的方法。此类的数据缓存在内存中。需要扩展一个forEach方法。


export interface CacheNStorage extends Storage {
  forEach(fn: (key: string, val: any) => void): void;
}

/** An interface of the Web Storage API provides access to a particular domain's session or local storage. It allows, for example, the addition, modification, or deletion of stored data items. */
interface Storage {
    /**
     * Returns the number of key/value pairs currently present in the list associated with the
     * object.
     */
    readonly length: number;
    /**
     * Empties the list associated with the object of all key/value pairs, if there are any.
     */
    clear(): void;
    /**
     * value = storage[key]
     */
    getItem(key: string): string | null;
    /**
     * Returns the name of the nth key in the list, or null if n is greater
     * than or equal to the number of key/value pairs in the object.
     */
    key(index: number): string | null;
    /**
     * delete storage[key]
     */
    removeItem(key: string): void;
    /**
     * storage[key] = value
     */
    setItem(key: string, value: string): void;
    [name: string]: any;
}

LruFactory

继承自BaseStorage接口,提供了操作缓存的方法并支持lru缓存算法。此类的数据缓存在内存中。

类型说明

CacheOptionModel

| 参数名 | 类型 | 说明 | | ------ | ------- | ----------- | | cache | boolean | 缓存开关 | | reload | boolean | 重刷缓存开关 | | expire | boolean | 过期时间(单位毫秒) |

changelog

V2.0.0

  1. 修改了promise缓存的机制,添加了缓存lru算法

V1.0.1

  1. 默认导出了内存factory

LICENSE

MIT