Browse Source

feat: 修改缓存存储机制

lanjianrong 4 years ago
parent
commit
1dd4571d8e

+ 2 - 1
src/pages/user/login/index.jsx

@@ -8,6 +8,7 @@ import styles from './index.less'
 import { apiLogin } from '@/services/login'
 import { setAuthCache } from '@/utils/auth'
 import { TOKEN_KEY } from '@/utils/cache/cacheEnum'
+import { DEFAULT_CACHE_TIME } from '@/settings/encryptionSetting'
 
 const LoginMessage = ({ content }) => (
   <Alert
@@ -56,7 +57,7 @@ const Login = () => {
       // 登录
       const res = await apiLogin({ ...values })
       if (res.code === 0) {
-        setAuthCache(TOKEN_KEY, res.data.token)
+        setAuthCache(TOKEN_KEY, res.data.token, DEFAULT_CACHE_TIME)
 
         const defaultLoginSuccessMessage = intl.formatMessage({
           id: 'pages.login.success',

+ 1 - 1
src/settings/encryptionSetting.ts

@@ -1,7 +1,7 @@
 import { isDevMode } from '@/utils/env'
 
 // System default cache time, in seconds
-export const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7 * 1000
+export const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7
 
 // aes encryption key
 export const cacheCipher = {

+ 2 - 2
src/utils/auth/index.ts

@@ -22,9 +22,9 @@ export function getAuthCache<T>(key: BasicKeys) {
   return fn(key) as T
 }
 
-export function setAuthCache(key: BasicKeys, value) {
+export function setAuthCache(key: BasicKeys, value: any, expire?: number) {
   const fn = isLocal ? Persistent.setLocal : Persistent.setSession
-  return fn(key, value, true)
+  return fn(key, value, true, expire)
 }
 
 export function clearAuthCache(immediate = true) {

+ 2 - 0
src/utils/cache/cacheEnum.ts

@@ -2,6 +2,8 @@
 // token key
 export const TOKEN_KEY = 'TOKEN__'
 
+export const LOCALE_KEY = 'LOCALE__'
+
 // base global local key
 export const APP_LOCAL_CACHE_KEY = 'COMMON__LOCAL__KEY__'
 

+ 2 - 2
src/utils/cache/index.ts

@@ -23,11 +23,11 @@ export const createStorage = (storage: Storage = sessionStorage, options: Option
 }
 
 export const createSessionStorage = (options: Options = {}) => {
-  return createStorage(sessionStorage, { ...options, timeout: DEFAULT_CACHE_TIME })
+  return createStorage(sessionStorage, { ...options, timeout: DEFAULT_CACHE_TIME * 5 })
 }
 
 export const createLocalStorage = (options: Options = {}) => {
-  return createStorage(localStorage, { ...options, timeout: DEFAULT_CACHE_TIME })
+  return createStorage(localStorage, { ...options, timeout: DEFAULT_CACHE_TIME * 5 })
 }
 
 export default WebStorage

+ 3 - 2
src/utils/cache/memory.ts

@@ -14,7 +14,8 @@ export class Memory<T = any, V = any> {
   private alive: number
 
   constructor(alive = NOT_ALIVE) {
-    this.alive = alive
+    // 30天有效
+    this.alive = alive * 1000 * 30
   }
 
   get getCache() {
@@ -74,7 +75,7 @@ export class Memory<T = any, V = any> {
 
   resetCache(cache: { [K in keyof T]: cache }) {
     Object.keys(cache).forEach(key => {
-      const k = (key as any) as keyof T
+      const k = key as any as keyof T
       const item = cache[k]
       if (item && item.time) {
         const now = new Date().getTime()

+ 7 - 2
src/utils/cache/persistent.ts

@@ -52,9 +52,14 @@ export class Persistent {
     return localMemory.get(key)?.value as Nullable<T>
   }
 
-  static setLocal(key: LocalKeys, value: LocalStore[LocalKeys], immediate = false): void {
+  static setLocal(
+    key: LocalKeys,
+    value: LocalStore[LocalKeys],
+    immediate = false,
+    expire?: number
+  ): void {
     localMemory.set(key, value)
-    immediate && ls.set(APP_LOCAL_CACHE_KEY, localMemory.getCache)
+    immediate && ls.set(APP_LOCAL_CACHE_KEY, localMemory.getCache, expire)
   }
 
   static removeLocal(key: LocalKeys, immediate = false): void {