Skip to content
本章目录

【浏览器缓存 —— newStorage】

功能: 生成一个可以操作的浏览器缓存实例:localStoragesessionStorageCookie

参数:

  • type: 'local' | 'session' | 'cookie'
  • options:{prefix:'pre',...}

实例方法: 每个实例都有5个功能相同的方法: setFungetFundelFunexistFunallKeyclearFun

1-函数引入

js
  import { newStorage } from 'tj-jstools'
1

2-函数声明

ts
type StorageType = 'cookie' | 'local' | 'session';
function newStorage(type: StorageType, options?: IComCookieBasicProp): ComStorage;
1
2
详细的类型声明
ts
declare type UnitTimeType = 'ms' | 's' | 'min' | 'h' | 'd' | 'w' | 'm' | 'y';
declare type StorageType = 'cookie' | 'local' | 'session';
interface IComStorageBasicProp {
    prefix?: string;
    suffix?: string;
    linkSign?: string;
}
interface IComStorageSetProp extends IComStorageBasicProp {
    expireTime?: number;
    unitTime?: UnitTimeType;
}
interface IComCookieBasicProp extends IComStorageSetProp {
    expires?: number | Date;
    path?: string;
    domain?: string;
    secure?: boolean;
    sameSite?: 'strict' | 'Strict' | 'lax' | 'Lax' | 'none' | 'None' | undefined;
    [property: string]: any;
}
interface IComStorageFun {
    setFun(key: string, value: any, options?: IComStorageSetProp | IComCookieBasicProp): void;
    getFun(key: string, options?: IComStorageBasicProp | IComCookieBasicProp): any;
    delFun(key: string, options?: IComStorageBasicProp | IComCookieBasicProp): void;
    existFun(key: string, options?: IComStorageBasicProp | IComCookieBasicProp): boolean;
    allKey(options?: IComStorageBasicProp | IComCookieBasicProp): string[];
    clearFun(options?: IComStorageBasicProp | IComCookieBasicProp): void;
}
declare class ComStorage implements IComStorageFun {
    private instanceType;
    private prefix;
    private suffix;
    private linkSign;
    private expireTime;
    private unitTime;
    private expires;
    private path;
    private domain;
    private secure;
    private sameSite;
    constructor(type: StorageType, options?: IComStorageSetProp | IComCookieBasicProp | undefined);
    setFun(key: string, value: any, options?: IComStorageSetProp | IComCookieBasicProp | undefined): void;
    getFun(key: string, options?: IComStorageBasicProp | IComCookieBasicProp | undefined): any;
    delFun(key: string, options?: IComStorageBasicProp | IComCookieBasicProp | undefined): void;
    existFun(key: string, options?: IComStorageBasicProp | IComCookieBasicProp | undefined): boolean;
    allKey(options?: IComStorageBasicProp | undefined): string[];
    clearFun(options?: IComStorageBasicProp | IComCookieBasicProp | undefined): void;
}
declare function newStorage(type: StorageType, options?: IComCookieBasicProp): ComStorage;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

3-使用示例

ts
import {newStorage} from 'tj-jstools'
const Coptions = {
  prefix:'tj',
  linkSign: '@',
  suffix:'jstools',
  expireTime: 2,
  unitTime: 'd'
}
const CInstance = newStorage('cookie',Coptions)

CInstance.setFun('test','testValue')
// tj@test@jstools: testValue ; 过期时间:2天
1
2
3
4
5
6
7
8
9
10
11
12

以上案例,设置了一个key为tj@test@jstools,值为'testValue',过期时间2天的cookie值。 如果你有个别缓存有其他的需求,你可以在setFun方法里面单独配置属性,它会覆盖初始化的配置

ts
CInstance.setFun('newTest','testValue',
{suffix:'com',expireTime:10,unitTime:'h'})
// tj@newTest@com: testValue ; 过期时间:10小时
1
2
3

以上案例,在setFun方法里面配置了options参数,它会覆盖初始化的配置,所以这里是设置了一个 key为tj@newTest@com,值为'testValue',过期时间为10个小时的cookie值。

4-配置参数options详解

公共配置:

这里指localStorage、sessionStorage、cookie实例共有的配置属性

  • prefix:默认值:'', key值的前缀
  • linkSign:默认值:.,前缀后缀与key值的连接符号
  • suffix:默认值:'',key值的后缀
  • expireTime:默认值:-1, 如果需要一个有效的过期时间,请设置一个大于0的int型数值
  • UnitTime:默认值:ms,可选:'ms(毫秒)'、's(秒)'、'min(分)'、'h(时)'、'd(天)'、'w(周=7天)'、'm(月=30天)'、'y(年=365天)'

Cookie配置:

这里指cookie独有的配置,对localStorage、sessionStorage无效

Released under the MIT License.