1.0.7-0 • Published 6 months ago

local-nut v1.0.7-0

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

缓存

基本用法

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是否开启调试模式booleanfalse
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;
}