| | |
| | | }, |
| | | }; |
| | | |
| | | //#region ====================== 缓存带时间 ====================== |
| | | type CacheValue<T> = { |
| | | // 过期时间,时间戳 |
| | | expiredTime: number; |
| | | value: T; |
| | | }; |
| | | export const LocalPlus = { |
| | | // 设置 |
| | | set<T>(key: string, val: T, expireDay: number) { |
| | | const expireMs = expireDay * 24 * 60 * 60 * 1000; |
| | | const cacheValue: CacheValue<T> = { |
| | | expiredTime: new Date().getTime() + expireMs, |
| | | value: val, |
| | | }; |
| | | Local.set(key, cacheValue); |
| | | }, |
| | | // 获取,过期则清除 |
| | | get(key: string) { |
| | | const cacheValue: CacheValue<any> = Local.get(key); |
| | | if (new Date().getTime() > cacheValue.expiredTime) { |
| | | Local.remove(key); |
| | | return null; |
| | | } |
| | | return cacheValue.value; |
| | | }, |
| | | }; |
| | | |
| | | |
| | | //#endregion |
| | | |
| | | /** |
| | | * window.sessionStorage 浏览器临时缓存 |
| | | * @method set 设置临时缓存 |