提交 c620f827 编写于 作者: V vben

perf: set cache default time

上级 b350098f
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
### ⚡ Performance Improvements ### ⚡ Performance Improvements
- `setTitle`逻辑调整 - `setTitle`逻辑调整
- 将系统用到的 sessionStorage 及 LocalStorage 缓存设置默认 `7` 天过期
### ✨ Refactor ### ✨ Refactor
......
// System default cache time, in seconds
export const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7;
/** /**
* @description: * @description:
*/ */
......
...@@ -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
const ls = createStorage(localStorage); const ls = createStorage(localStorage);
const ss = createStorage(); const ss = createStorage();
interface CacheStore {
local?: any;
session?: any;
}
/** /**
* @description: Persistent cache * @description: Persistent cache
*/ */
const cacheStore: any = { const cacheStore: CacheStore = {
// localstorage cache // localstorage cache
local: {}, local: {},
// sessionstorage cache // sessionstorage cache
......
import { EncryptionParams } from '/@/utils/cipher/aesEncryption'; import { DEFAULT_CACHE_TIME } from '/@/settings/cipherSetting';
export interface CreateStorageParams extends EncryptionParams {
// import { EncryptionParams } from '/@/utils/cipher/aesEncryption';
export interface CreateStorageParams {
storage: Storage; storage: Storage;
hasEncrypt: boolean; hasEncrypt: boolean;
} }
const defaultTime = 60 * 60 * 24 * 7;
export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {}) => { export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {}) => {
/** /**
*缓存类 *缓存类
...@@ -36,7 +37,7 @@ export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {}) ...@@ -36,7 +37,7 @@ export const createStorage = ({ prefixKey = '', storage = sessionStorage } = {})
* @expire 过期时间 单位秒 * @expire 过期时间 单位秒
* @memberof Cache * @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({ const stringData = JSON.stringify({
value, value,
expire: expire !== null ? new Date().getTime() + expire * 1000 : null, 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 } = {})
* 例子: * 例子:
* cookieData.set('name','value',) * 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; document.cookie = this.getKey(name) + '=' + value + '; Max-Age=' + expire;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册