# 持久化

定义

export declare const getCookies: () => Record<string, string>;
export declare const getCookie: (key: string) => string;
export interface Cookie {
    value?: string;
    expires?: DateTypes;
    path?: string;
    domain?: string;
}
export declare type Cookies = Record<string, string | Cookie>;
export declare const setCookie: (key: string, value: string | Cookie) => void;
export declare const setCookies: (cookies: Cookies) => void;
export declare const delCookies: (keys: string[]) => void;
export declare const delCookie: (key: string) => void;

# SlugStorage @Deprecated

@Deprecated 如有必要,使用 ProxyStorage

统一 Storage 定义

interface SimpleWebStorage {
  readonly length: number;
  key(index: number): string | null;

  getItem(key: string): string | null;
  removeItem(key: string): void;
  setItem(key: string, value: string): void;
  clear(): void;
}

export interface SlugStorageInfo {
    keys: string[];
}
export interface SlugStorage<K extends SlugStorageInfo = SlugStorageInfo> {
    batchGet<T>(keys: string[]): Promise<{
        [key: string]: T | undefined;
    }>;
    batchSet<T>(data: {
        [key: string]: T | undefined;
    }): Promise<void>;
    getItem<T>(key: string, defaultValue?: T): Promise<T | undefined>;
    setItem<T>(key: string, value?: T): Promise<void>;
    remove<T>(key: string): Promise<T | undefined>;
    clear(): Promise<void>;
    info(): Promise<K>;
    getItemSync<T>(key: string, defaultValue?: T): T | undefined;
    setItemSync<T>(key: string, value?: T): void;
    removeSync<T>(key: string): T | undefined;
    clearSync(): void;
    infoSync(): K;
}

Storage 适配器工厂,以符合 SimpleWebStorage 定义的对象(e.g. window.localStorage)创建具有命名空间的 Storage(本质是在 key 前加命名空间作为前缀)。

export declare class WebStorage implements SlugStorage {
    // ...
    constructor(storage: SimpleWebStorage, ns?: string);
    // ...
}

内置 Storage (不带命名空间)

出于 tree-shacking 考虑,已删除实例化导出

// export const localStorage = new WebStorage(window.localStorage);
// export const sessionStorage = new WebStorage(window.sessionStorage);

# ProxyStorage

v1.5.0+ 新增的 Storage 使用方式,与 SlugStorage 相比:

  1. 有着更好的跨平台能力,原 SlugStorage 是定义了接口,各平台实现接口,过于冗余,新的方案只需实现部分方法的适配器即可快速接入新平台(微信小程序、hippy)
  2. ProxyStorage 有着更好的 ts 类型支持
  3. ProxyStorage 将命名空间的概念完全抽离到公共部分,平台适配器不再需要考虑
interface Model {
  o: { a: number }
  b: boolean
  c: number
  d: string
}

const proxy = createProxyStorage<Model>({
  adapter: webStorageAdapter,
  storage: localStorage,
})
await proxy.setItem('o', { a: 1 })

定义:

export interface BaseProxyStorage<Model extends Record<Keys, any>, Keys extends string> {
    keys: () => Promise<Keys[]>;
    setItem: <Key extends Keys>(key: Key, value?: Model[Key]) => Promise<void>;
    getItem: <Key extends Keys>(key: Key) => Promise<Model[Key] | undefined>;
}
export interface IProxyStorage<Model extends Record<Keys, any>, Keys extends string> extends BaseProxyStorage<Model, Keys> {
    removeItem?: (key: Keys) => Promise<void>;
    batchSet?: (data: Partial<Model>) => Promise<void>;
    batchGet?: <Key extends Keys>(keys: Key[]) => Promise<Pick<Model, Extract<keyof Model, Key>>>;
    batchRemove?: (keys: Keys[]) => Promise<void>;
    clear?: () => Promise<void>;
}
export declare type ProxyStorage<Model extends Record<Keys, any>, Keys extends string> = Required<IProxyStorage<Model, Keys>>;
export interface CreateProxyStorageOptions<OriginStorage = any> {
    ns?: string;
    adapter?: ProxyStorageAdapter<OriginStorage>;
    storage: OriginStorage;
    plugins?: ProxyStoragePluginOptions;
}
export declare function createProxyStorage<Model extends Record<Keys, any> = any, Keys extends string = keyof Model & string>(opts: CreateProxyStorageOptions | Record<string, any>): ProxyStorage<Model, Keys>;

# ProxyStorageAdapter

定义:

export declare type ProxyStorageAdapter<OriginStorage> = (storage: OriginStorage) => IProxyStorage<Record<string, any>, string>;

ProxyStorageAdapter 只需实现 BaseProxyStorage 即可拥有 IProxyStorage 附加的能力,当然如果原平台支持,也可主动实现更多接口,达到更好的性能

目前内置的 Adapter 有:

  1. simpleStorageAdapter: 基于 Object
  2. webStorageAdapter: 基于 WebStorage
  3. wxStorageAdapter: QQ/微信小程序(单测暂缺)
  4. hippyStorageAdapter: Hippy(单测暂缺)

# ProxyStoragePlugin

在抹平平台差异的 ProxyStorage 基础上附加更多的能力

  1. LRU 缓存机制(LRU Cache)

用法:

const storage: Model = {} as any
const proxy = createProxyStorage<Model>({
    storage,
    plugins: {
        lru: { size: 2 },
    },
})
更新于: 6/25/2021, 5:28:40 PM