1
1
class Storage {
2
+ // `prefix`: 缓存前缀, 实现namespace的功能, 防止重名
2
3
static readonly prefix : string = '__' ;
3
- private readonly splitCode = '|_|' ;
4
- private readonly storage = localStorage || window . localStorage ;
5
- static readonly defauleExpiredTime = 30 * 24 * 60 * 60 * 1000 ;
6
4
7
- constructor ( ) {
5
+ // `splitCode`: 过期时间 和 缓存值之间的切割符, 如: '60000|_|abc'
6
+ static readonly splitCode : string = '|_|' ;
7
+
8
+ // storage support
9
+ static readonly storage = localStorage || window . localStorage ;
10
+
11
+ // `defaultExpiredTime`: 默认30天过期
12
+ static readonly defaultExpiredTime = 30 * 24 * 60 * 60 * 1000 ;
13
+
14
+ private prefix : string ;
15
+
16
+ private splitCode : string ;
17
+
18
+ constructor ( prefix ?: string , splitCode ?: string ) {
19
+ this . prefix = prefix || Storage . prefix ;
20
+ this . splitCode = splitCode || Storage . splitCode ;
8
21
}
9
22
10
- private getStorageKey ( key :string ) {
11
- return `${ Storage . prefix } ${ key } ` ;
23
+ /**
24
+ * 获取缓存key
25
+ * @param key
26
+ */
27
+ private getStorageKey ( key :string ) : string {
28
+ return `${ this . prefix } ${ key } ` ;
12
29
}
13
30
14
- public setItem ( key : string , value : any , expired : number = Storage . defauleExpiredTime ) {
31
+ /**
32
+ * 设置缓存值
33
+ * @param key, storage 的 key
34
+ * @param value, storage 的 value
35
+ * @param expired, storage 的过期时间
36
+ */
37
+ public setItem ( key : string , value : any , expired : number = Storage . defaultExpiredTime ) : void {
15
38
const itemKey = this . getStorageKey ( key ) ;
16
- const expiredTimestamp = + ( new Date ( ) ) + expired ;
17
- this . storage . setItem ( itemKey , `${ expiredTimestamp } ${ this . splitCode } ${ value } ` ) ;
39
+ const date = new Date ( ) ;
40
+ const expiredTimestamp = date . getTime ( ) + expired ;
41
+ Storage . storage . setItem ( itemKey , `${ expiredTimestamp } ${ this . splitCode } ${ value } ` ) ;
18
42
}
19
43
20
- public getItem ( key : string ) {
44
+ /**
45
+ * 获取缓存值
46
+ * @param key, 缓存的key
47
+ */
48
+ public getItem ( key : string ) : string {
21
49
const itemKey = this . getStorageKey ( key ) ;
22
- const localValue = this . storage . getItem ( itemKey ) ;
50
+ const value = Storage . storage . getItem ( itemKey ) ;
23
51
24
- if ( localValue ) {
25
- const localValueSplits = localValue . split ( this . splitCode ) ;
52
+ if ( value ) {
53
+ const localValueSplits = value . split ( this . splitCode ) ;
54
+ const currentTimestamp = ( new Date ( ) ) . getTime ( ) ;
26
55
if ( + localValueSplits [ 0 ] > + ( new Date ( ) ) ) {
27
56
return localValueSplits [ 1 ] ;
28
57
}
@@ -33,10 +62,14 @@ class Storage {
33
62
return null ;
34
63
}
35
64
36
- public removeItem ( key : string ) {
65
+ /**
66
+ * 删除缓存
67
+ * @param key, 缓存的key
68
+ */
69
+ public removeItem ( key : string ) : void {
37
70
const itemKey = this . getStorageKey ( key ) ;
38
- this . storage . removeItem ( itemKey ) ;
71
+ Storage . storage . removeItem ( itemKey ) ;
39
72
}
40
73
}
41
74
42
- export default new Storage ( ) ;
75
+ export default Storage ;
0 commit comments