Skip to content

Commit

Permalink
perf: set cache default time
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Oct 27, 2020
1 parent 9099187 commit 5da09a3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
### ⚡ Performance Improvements

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

### ✨ Refactor

Expand Down
3 changes: 3 additions & 0 deletions src/settings/cipherSetting.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// System default cache time, in seconds
export const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7;

/**
* @description:
*/
Expand Down
7 changes: 6 additions & 1 deletion src/utils/helper/persistent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import { BASE_LOCAL_CACHE_KEY, BASE_SESSION_CACHE_KEY } from '/@/enums/cacheEnum
const ls = createStorage(localStorage);
const ss = createStorage();

interface CacheStore {
local?: any;
session?: any;
}

/**
* @description: Persistent cache
*/
const cacheStore: any = {
const cacheStore: CacheStore = {
// localstorage cache
local: {},
// sessionstorage cache
Expand Down
11 changes: 6 additions & 5 deletions src/utils/storage/Storage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { EncryptionParams } from '/@/utils/cipher/aesEncryption';
export interface CreateStorageParams extends EncryptionParams {
import { DEFAULT_CACHE_TIME } from '/@/settings/cipherSetting';

// import { EncryptionParams } from '/@/utils/cipher/aesEncryption';
export interface CreateStorageParams {
storage: Storage;
hasEncrypt: boolean;
}
const defaultTime = 60 * 60 * 24 * 7;
export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {}) => {
/**
*缓存类
Expand Down Expand Up @@ -36,7 +37,7 @@ export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {})
* @expire 过期时间 单位秒
* @memberof Cache
*/
set(key: string, value: any, expire: number | null = defaultTime) {
set(key: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
const stringData = JSON.stringify({
value,
expire: expire !== null ? new Date().getTime() + expire * 1000 : null,
Expand Down Expand Up @@ -96,7 +97,7 @@ export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {})
* 例子:
* cookieData.set('name','value',)
*/
setCookie(name: string, value: any, expire: number | null = defaultTime) {
setCookie(name: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
document.cookie = this.getKey(name) + '=' + value + '; Max-Age=' + expire;
}

Expand Down

0 comments on commit 5da09a3

Please sign in to comment.