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

local-nut

v1.0.7-0

Published

## 基本用法

Downloads

2

Readme

缓存

基本用法

MemoryStore

  • 基于 js 内存
  • 支持任意 js 支持的类型
  • 同步读写、删除、清空
  • 不建议大数据量存储

示例

import { createMemoryStore } from "@done-coding/nut";
const testStore = createMemoryStore("test", {
  debug: false,
  maxSize: 10,
  maxCount: 100,
  useClone: true,
  clearPlan: "LRU",
  onLimitClearUp(key, value, expire) {
    console.log(
      `${key}因为缓存达到上限被清理,还有${expire}ms过期,值为:`,
      value,
    );
  },
});
testStore.set("k1", 1); // true
testStore.get("k1"); // 1
testStore.remove("k1"); // true
testStore.clear();

API

| 属性 | 说明 | 类型 | 必需 | 默认值 | | --------- | ------------------ | -------------------------------------------- | ---- | -------------------------------------------------------------------------------- | | namespace | 该类型仓库命名空间 | string | 是 | / | | options | 实例化配置项 | 见附录 Options | 否 | { debug: false, maxSize: 10, maxCount: 100, useClone: true, clearPlan: "LRU" } |


SessionStore

  • 基于 SessionStorage
  • 不支持不能 JSON 序列化的数据

示例

import { createSessionStore } from "@done-coding/nut";

类似 MemoryStore 示例,但函数名及选项默认值不一样

API

| 属性 | 说明 | 类型 | 必需 | 默认值 | | --------- | ------------------ | ---------------------------------- | ---- | --------------------------------------------------------------- | | namespace | 该类型仓库命名空间 | string | 是 | / | | options | 实例化配置项 | 见附录 BaseOptions | 否 | { debug: false, maxSize: 1, maxCount: 100, clearPlan: "LRU" } |


LocalEasyStore

  • 基于 LocalStorage
  • 不支持不能 JSON 序列化的数据

示例

import { createLocalEasyStore } from "@done-coding/nut";

类似 MemoryStore 示例,但函数名及选项默认值不一样

API

| 属性 | 说明 | 类型 | 必需 | 默认值 | | --------- | ------------------ | ---------------------------------- | ---- | --------------------------------------------------------------- | | namespace | 该类型仓库命名空间 | string | 是 | / | | options | 实例化配置项 | 见附录 BaseOptions | 否 | { debug: false, maxSize: 1, maxCount: 100, clearPlan: "LRU" } |


LocalBigStore

  • 基于 IndexDB
  • 支持的存储类型见IndexDB

示例

import { createLocalBigStore } from "@done-coding/nut";

const testStore = createLocalBigStore("test", {
  debug: false,
  maxSize: 200,
  maxCount: 100,
  useClone: true,
  clearPlan: "LRU",
});
testStore.set("k1", 1).then((res) => {
  console.log(res); // 1
});
testStore.get("k1").then((res) => {
  console.log(res); // 1
});
testStore.remove("k1").then(() => {
  console.log("移除成功");
});
testStore.clear().then(() => {
  console.log("清空成功");
});

API

| 属性 | 说明 | 类型 | 必需 | 默认值 | | --------- | ------------------ | ---------------------------------- | ---- | ----------------------------------------------------------------- | | namespace | 该类型仓库命名空间 | string | 是 | / | | options | 实例化配置项 | 见附录 BaseOptions | 否 | { debug: false, maxSize: 200, maxCount: 100, clearPlan: "LRU" } |


高级用法

UseCacheDispatch

示例

import {
  createUseCacheDispatch,
  createMemoryStore,
  createLocalBigStore,
} from from "@done-coding/nut";

const localBigStore = createLocalBigStore("test");
const memoryStore = createMemoryStore("test", {
  // 非必传 当内存仓库因达到上限被清理时可以转存到另一个仓库中
  onLimitClearUp: localBigStore.set
});

const getSomeData = (...args: any[]) => {
    // 经过复杂的处理得出结果 res
    ...
    ...
    ...
    return res;
}

const getSomeData2 = (...args: any[]) => {
  // 经过复杂的处理得出结果 res
  ...
  ...
  ...
  return res
}

// getSomeData2参数唯一标识计算如果耗时过长,请手动提供一个子方法getSole返回一个基于参数得到的唯一标识

getSomeData2.getSolo = (...args: any[]) => {
  return new Promise((resolve, reject) => {
    // 经过某种处理得到该参数的唯一标识 key
    ...
    ...
    ...
    if(...){
      resolve(key)
    }else{
      reject(...)
    }
  })
}

const useCacheDispatch = createUseCacheDispatch({
  generator: {
    getSomeData,
    getSomeData2
  },
  storeList: [memoryStore, localBigStore]
});


await useCacheDispatch.getData[getSomeData](...)

API

Options

| 属性 | 说明 | 类型 | 必需 | 默认值 | | --------- | ---------------- | ---------------------------------------- | ---- | ------- | | debug | 是否开启调试模式 | boolean | 否 | false | | generator | 数据生成配置项 | Generator | 是 | | storeList | 缓存仓库列表 | BaseStore[] | 是 |


附录

CacheUpperLimitClearPlan

/**
 * 缓存上限清除策略
 *
 * LRU 优先保留最近使用的数据;
 *
 * LFU 优先保留高频使用的数据;
 *
 * FIFO 优先保留后面的数据;
 */
type CacheUpperLimitClearPlan = "LRU" | "LFU" | "FIFO";

CacheUpperLimit

interface CacheUpperLimit {
  /**
   * 最大缓存大小,单位MB
   */
  maxSize?: number;
  /**
   * 最大缓存数量
   */
  maxCount?: number;
  /**
   * 缓存上限清除策略
   */
  clearPlan?: CacheUpperLimitClearPlan;
}

BaseOptions

interface BaseOptions extends CacheUpperLimit {
  /**
   * 是否开启调试模式
   */
  debug?: boolean;
}

Options

interface Options extends BaseOptions {
  /**
   * 是否使用克隆数据[默认是]
   */
  useClone?: boolean;
  /**
   * [因上限]清理缓存时触发
   * @param info
   * @returns
   */
  onLimitClearUp?: (...args: Parameters<BaseStore["set"]>) => void;
}

Generator

interface GeneratorFun<P extends any[] = any[], R extends any = any> {
  (...args: P): R;
  /**
   * 获取唯一值
   */
  getSole?: {
    (...args: P): Promise<string>;
  };
}

/**
 * 数据生成器选项
 */
interface Generator {
  [generatorName: string]: GeneratorFun;
}

BaseStore

interface BaseStore {
  /**
   * 设置缓存
   * @param key
   * @param value
   * @param expire 过期时间,单位ms(如100为100ms后过期)
   * @param type 内部使用标识,外部不用传 也不可能传对 symbol类型
   * @returns
   */
  set: <V = any>(
    key: string,
    value: V,
    expire?: number,
    type?: SetActionType,
  ) => Promise<V> | boolean;
  /**
   * 获取缓存
   * @param key
   * @param type 内部使用标识,外部不用传 也不可能传对 symbol类型
   * @returns
   */
  get: (key: string, type?: GetActionType) => Promise<any> | any;
  /**
   * 移除缓存
   * @param key
   * @param type 内部使用标识,外部不用传 也不可能传对 symbol类型
   * @returns
   */
  remove: (key: string, type?: RemoveActionType) => Promise<void> | boolean;
  /**
   * 清空缓存
   * @returns
   */
  clear: () => Promise<void> | void;
  /**
   * 所有缓存的key
   * @returns
   */
  getKeys: () => Promise<string[]> | string[];

  /**
   * 注销实例
   * @param saveCache 是否保留缓存记录 【!MemoryStore注销时内存直接释放 不需要该选项】
   * @returns
   */
  destroy: (saveCache: boolean) => void;
}