1.0.2 • Published 1 year ago

front-end-cache v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

FrontEndCache

统一前端浏览器缓存管理,支持 cookielocalStoragesessionStorage

文档

语法:new FrontEndCache<ValueType>(key[, options]);

key

当前缓存对象的 key,可以通过 options.encryKey 对其加密。

options 属性

参数类型必填默认值说明
typelocalStorage | sessionStorage | cookielocalStorage类型
domainstringlocation.hostname仅仅 type=cookie注意和 原生 cookie 的区分,默认不是当前 path
domainstring/cookiepath 配置
expiresnumber-过期时间,默认单位 seconds 针对 sessionStoragecookie 默认值为当前会话 针对 localStorage 默认值为 永久
expiresUnityears | months | days | hours | minutes | secondsseconds过期时间单位
securebooleanfalse仅仅 type=cookie
serializebooleantrue是否序通过 JSON.stringify 列化 value如果为 cookie 还会通过 encodeURIComponent 一次 JSON.stringify 后的数据仅在 value 类型为 string 的时候可以禁用序列化,但同样会调用 encodeURIComponent
encryKey(key: string) => string-加密 key 的函数需要注意如果 type = 'cookie', 最终的 key 也会 encodeURIComponent

使用

/** cache.ts */

import FrontEndCache from 'front-end-cache';

export const accessToken = new FrontEndCache<string>('ACCESS_TOKEN', { type: 'cookie', serialize: false });

export const userinfoCache = new FrontEndCache<UserInfoModel>('PROJECTNAME_USERINFO_CACHE');

/** more cache */
import { userinfoCache } from 'utils/cache.ts';

userinfoCache.setItem({ id: 1, name: 'xxx' });

userinfoCache.getItem();