Commit c620f8279f1056ddab84b3907fb50b3af4fe9247
1 parent
b350098f
perf: set cache default time
Showing
4 changed files
with
16 additions
and
6 deletions
CHANGELOG.zh_CN.md
src/settings/cipherSetting.ts
src/utils/helper/persistent.ts
@@ -6,10 +6,15 @@ import { BASE_LOCAL_CACHE_KEY, BASE_SESSION_CACHE_KEY } from '/@/enums/cacheEnum | @@ -6,10 +6,15 @@ import { BASE_LOCAL_CACHE_KEY, BASE_SESSION_CACHE_KEY } from '/@/enums/cacheEnum | ||
6 | const ls = createStorage(localStorage); | 6 | const ls = createStorage(localStorage); |
7 | const ss = createStorage(); | 7 | const ss = createStorage(); |
8 | 8 | ||
9 | +interface CacheStore { | ||
10 | + local?: any; | ||
11 | + session?: any; | ||
12 | +} | ||
13 | + | ||
9 | /** | 14 | /** |
10 | * @description: Persistent cache | 15 | * @description: Persistent cache |
11 | */ | 16 | */ |
12 | -const cacheStore: any = { | 17 | +const cacheStore: CacheStore = { |
13 | // localstorage cache | 18 | // localstorage cache |
14 | local: {}, | 19 | local: {}, |
15 | // sessionstorage cache | 20 | // sessionstorage cache |
src/utils/storage/Storage.ts
1 | -import { EncryptionParams } from '/@/utils/cipher/aesEncryption'; | ||
2 | -export interface CreateStorageParams extends EncryptionParams { | 1 | +import { DEFAULT_CACHE_TIME } from '/@/settings/cipherSetting'; |
2 | + | ||
3 | +// import { EncryptionParams } from '/@/utils/cipher/aesEncryption'; | ||
4 | +export interface CreateStorageParams { | ||
3 | storage: Storage; | 5 | storage: Storage; |
4 | hasEncrypt: boolean; | 6 | hasEncrypt: boolean; |
5 | } | 7 | } |
6 | -const defaultTime = 60 * 60 * 24 * 7; | ||
7 | export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {}) => { | 8 | export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {}) => { |
8 | /** | 9 | /** |
9 | *缓存类 | 10 | *缓存类 |
@@ -36,7 +37,7 @@ export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {}) | @@ -36,7 +37,7 @@ export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {}) | ||
36 | * @expire 过期时间 单位秒 | 37 | * @expire 过期时间 单位秒 |
37 | * @memberof Cache | 38 | * @memberof Cache |
38 | */ | 39 | */ |
39 | - set(key: string, value: any, expire: number | null = defaultTime) { | 40 | + set(key: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) { |
40 | const stringData = JSON.stringify({ | 41 | const stringData = JSON.stringify({ |
41 | value, | 42 | value, |
42 | expire: expire !== null ? new Date().getTime() + expire * 1000 : null, | 43 | expire: expire !== null ? new Date().getTime() + expire * 1000 : null, |
@@ -96,7 +97,7 @@ export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {}) | @@ -96,7 +97,7 @@ export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {}) | ||
96 | * 例子: | 97 | * 例子: |
97 | * cookieData.set('name','value',) | 98 | * cookieData.set('name','value',) |
98 | */ | 99 | */ |
99 | - setCookie(name: string, value: any, expire: number | null = defaultTime) { | 100 | + setCookie(name: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) { |
100 | document.cookie = this.getKey(name) + '=' + value + '; Max-Age=' + expire; | 101 | document.cookie = this.getKey(name) + '=' + value + '; Max-Age=' + expire; |
101 | } | 102 | } |
102 | 103 |