Skip to main content

store

基础数据缓存,提供数据页面隔离、数据过期、缓存级别(localStorage、sessionStorage 和 内存缓存)相关能力

安装

npm install @jelper/store

Example

import stroe from '@jelper/store';

store.setValue<string>('key', 'pagekey');
store.setValue<string>('value', 'home');

console.log(store.getValue<string>('key')) // pagekey
console.log(store.getValue<string>('value')) // home

store.remove('value');
console.log(store.getValue<string>('key')) // pagekey
console.log(store.getValue<string>('value')) // undefined

store.clear();
console.log(store.getValue<string>('key')) // undefined
console.log(store.getValue<string>('value')) // undefined

Api

浏览器缓存工具包,支持 localStorage、sessionStorage 和 内存缓存,解决跨页面冲突、过期等问题;

方法参数返回值
setValue(key: string; value: any; opts?: Option )void
getValue(key: string; opts?: Omit<Option, 'expire'> }any
remove(key: string; opts?: Omit<Option, 'expire'> }void
clear(opts?: Omit<Option, 'expire'>)void

注:opts 默认值 { type: StoreType.page, cache: StorageType.local, expires: undefined }

Type

Option

interface Options {
  cache?: StorageType,
  type?: StoreType,
  expires?: number,
}

StoreType

export enum StoreType {
  global = 'global', // 全局缓存
  page = 'page', // 页面级缓存
}

StorageType

export enum StorageType {
  memory = 'memory', // 内存
  session = 'session', // sessionStorage
  local = 'local', // localStorage
}