Skip to content

Commit c620f82

Browse files
committed
perf: set cache default time
1 parent b350098 commit c620f82

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

CHANGELOG.zh_CN.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
### ⚡ Performance Improvements
1616

1717
- `setTitle`逻辑调整
18+
- 将系统用到的 sessionStorage 及 LocalStorage 缓存设置默认 `7` 天过期
1819

1920
### ✨ Refactor
2021

src/settings/cipherSetting.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// System default cache time, in seconds
2+
export const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7;
3+
14
/**
25
* @description:
36
*/

src/utils/helper/persistent.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import { BASE_LOCAL_CACHE_KEY, BASE_SESSION_CACHE_KEY } from '/@/enums/cacheEnum
66
const ls = createStorage(localStorage);
77
const ss = createStorage();
88

9+
interface CacheStore {
10+
local?: any;
11+
session?: any;
12+
}
13+
914
/**
1015
* @description: Persistent cache
1116
*/
12-
const cacheStore: any = {
17+
const cacheStore: CacheStore = {
1318
// localstorage cache
1419
local: {},
1520
// sessionstorage cache

src/utils/storage/Storage.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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 {
35
storage: Storage;
46
hasEncrypt: boolean;
57
}
6-
const defaultTime = 60 * 60 * 24 * 7;
78
export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {}) => {
89
/**
910
*缓存类
@@ -36,7 +37,7 @@ export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {})
3637
* @expire 过期时间 单位秒
3738
* @memberof Cache
3839
*/
39-
set(key: string, value: any, expire: number | null = defaultTime) {
40+
set(key: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
4041
const stringData = JSON.stringify({
4142
value,
4243
expire: expire !== null ? new Date().getTime() + expire * 1000 : null,
@@ -96,7 +97,7 @@ export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {})
9697
* 例子:
9798
* cookieData.set('name','value',)
9899
*/
99-
setCookie(name: string, value: any, expire: number | null = defaultTime) {
100+
setCookie(name: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
100101
document.cookie = this.getKey(name) + '=' + value + '; Max-Age=' + expire;
101102
}
102103

0 commit comments

Comments
 (0)