Skip to content

Commit ec7bef7

Browse files
committed
fix: ensure that storage is deleted correctly close #292
1 parent efb27af commit ec7bef7

File tree

3 files changed

+37
-30
lines changed

3 files changed

+37
-30
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@
103103
"vite-plugin-imagemin": "^0.2.8",
104104
"vite-plugin-mock": "^2.1.5",
105105
"vite-plugin-purge-icons": "^0.7.0",
106-
"vite-plugin-pwa": "^0.5.4",
106+
"vite-plugin-pwa": "^0.5.5",
107107
"vite-plugin-style-import": "^0.7.5",
108108
"vite-plugin-theme": "^0.4.8",
109-
"vite-plugin-windicss": "0.5.0",
109+
"vite-plugin-windicss": "0.5.3",
110110
"vue-eslint-parser": "^7.5.0",
111111
"yargs": "^16.2.0"
112112
},

src/utils/cache/storageCache.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type { EncryptionParams } from '/@/utils/cipher';
44

55
import { AesEncryption } from '/@/utils/cipher';
66

7+
import { isNullOrUnDef } from '/@/utils/is';
8+
79
export interface CreateStorageParams extends EncryptionParams {
810
prefixKey: string;
911
storage: Storage;
@@ -61,7 +63,8 @@ export const createStorage = ({
6163
set(key: string, value: any, expire: number | null = timeout) {
6264
const stringData = JSON.stringify({
6365
value,
64-
expire: expire !== null ? new Date().getTime() + expire * 1000 : null,
66+
time: Date.now(),
67+
expire: !isNullOrUnDef(expire) ? new Date().getTime() + expire * 1000 : null,
6568
});
6669
const stringifyValue = this.hasEncrypt
6770
? this.encryption.encryptByAES(stringData)
@@ -75,21 +78,20 @@ export const createStorage = ({
7578
* @memberof Cache
7679
*/
7780
get(key: string, def: any = null): any {
78-
const item = this.storage.getItem(this.getKey(key));
79-
if (item) {
80-
try {
81-
const decItem = this.hasEncrypt ? this.encryption.decryptByAES(item) : item;
82-
const data = JSON.parse(decItem);
83-
const { value, expire } = data;
84-
if (expire === null || expire >= new Date().getTime()) {
85-
return value;
86-
}
87-
this.remove(this.getKey(key));
88-
} catch (e) {
89-
return def;
81+
const val = this.storage.getItem(this.getKey(key));
82+
if (!val) return def;
83+
84+
try {
85+
const decVal = this.hasEncrypt ? this.encryption.decryptByAES(val) : val;
86+
const data = JSON.parse(decVal);
87+
const { value, expire } = data;
88+
if (isNullOrUnDef(expire) || expire >= new Date().getTime()) {
89+
return value;
9090
}
91+
this.remove(key);
92+
} catch (e) {
93+
return def;
9194
}
92-
return def;
9395
}
9496

9597
/**

yarn.lock

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,12 +1718,12 @@
17181718
dependencies:
17191719
vue-demi latest
17201720

1721-
"@windicss/plugin-utils@0.5.0":
1722-
version "0.5.0"
1723-
resolved "https://registry.npmjs.org/@windicss/plugin-utils/-/plugin-utils-0.5.0.tgz#dab121dd33074eccb4b7c85e7757eb5704f1c76e"
1724-
integrity sha512-d2oi4cY+PV0mcQ2wij1nI+Ku0s6Zw2/+zllxX8TtMyEknYqe5zHE69DbUyCjIyVg2DYCje2vIc8XnTWYDFEkjQ==
1721+
"@windicss/plugin-utils@0.5.3":
1722+
version "0.5.3"
1723+
resolved "https://registry.npmjs.org/@windicss/plugin-utils/-/plugin-utils-0.5.3.tgz#5302864cb2f94dd3d71bb5af958dab6ce3f74dab"
1724+
integrity sha512-BXNvMSRmi1TyM8j3EnF/li9HViJPN4EPeGKTiIDWemxrfkRNlgN+Gmv31CFtZwnjiAlXRO58B6LIqVTSVYaTfQ==
17251725
dependencies:
1726-
esbuild "^0.8.50"
1726+
esbuild "^0.8.52"
17271727
esbuild-register "^2.0.0"
17281728
fast-glob "^3.2.5"
17291729
micromatch "^4.0.2"
@@ -3616,6 +3616,11 @@ esbuild@^0.8.50:
36163616
resolved "https://registry.npmjs.org/esbuild/-/esbuild-0.8.50.tgz#ebf24fde0cdad1a369789dd6fd7a820b0a01e46c"
36173617
integrity sha512-oidFLXssA7IccYzkqLVZSqNJDwDq8Mh/vqvrW+3fPWM7iUiC5O2bCllhnO8+K9LlyL/2Z6n+WwRJAz9fqSIVRg==
36183618

3619+
esbuild@^0.8.52:
3620+
version "0.8.52"
3621+
resolved "https://registry.npmjs.org/esbuild/-/esbuild-0.8.52.tgz#6dabf11c517af449a96d66da20dfc204ee7b5294"
3622+
integrity sha512-b5KzFweLLXoXQwdC/e2+Z80c8uo2M5MgP7yQEEebkFw6In4T9CvYcNoM2ElvJt8ByO04zAZUV0fZkXmXoi2s9A==
3623+
36193624
escalade@^3.1.1:
36203625
version "3.1.1"
36213626
resolved "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
@@ -8935,10 +8940,10 @@ vite-plugin-purge-icons@^0.7.0:
89358940
"@purge-icons/generated" "^0.7.0"
89368941
rollup-plugin-purge-icons "^0.7.0"
89378942

8938-
vite-plugin-pwa@^0.5.4:
8939-
version "0.5.4"
8940-
resolved "https://registry.npmjs.org/vite-plugin-pwa/-/vite-plugin-pwa-0.5.4.tgz#ce6fb85da140359057290e5eba3c22548392bea5"
8941-
integrity sha512-Zcr190GixdvvHBS1poTevtuw0irRvRi9rLFdXUbkPyY5hozQ+JhR8i/ORRvl6a9wV6Gl/mVwJ3IaY5IjTf3zFw==
8943+
vite-plugin-pwa@^0.5.5:
8944+
version "0.5.5"
8945+
resolved "https://registry.npmjs.org/vite-plugin-pwa/-/vite-plugin-pwa-0.5.5.tgz#f6bcaf6f6f1af0882fff7a9334aec685b798cceb"
8946+
integrity sha512-gwPg+pDm87iMOLORz/fOZiNNWNXhHFrMPW34XpX3F9JLl6ytcNZ6cJMYJ1FRKQPtVADqkbZjk3g3AOi1oI6HKQ==
89428947
dependencies:
89438948
debug "^4.3.2"
89448949
fast-glob "^3.2.5"
@@ -8968,12 +8973,12 @@ vite-plugin-theme@^0.4.8:
89688973
es-module-lexer "^0.3.26"
89698974
tinycolor2 "^1.4.2"
89708975

8971-
vite-plugin-windicss@0.5.0:
8972-
version "0.5.0"
8973-
resolved "https://registry.npmjs.org/vite-plugin-windicss/-/vite-plugin-windicss-0.5.0.tgz#63189c772feab50b3860ddec4c8a0ac69555f9d2"
8974-
integrity sha512-8mT03wppL3/ehyNEIJHVpMlWsS8jlSBaW/zSUuGjFiUC0zhB0GSzTbQkvtpJLqlFvvU4oHF4uhvN9tb7Uoq1ww==
8976+
vite-plugin-windicss@0.5.3:
8977+
version "0.5.3"
8978+
resolved "https://registry.npmjs.org/vite-plugin-windicss/-/vite-plugin-windicss-0.5.3.tgz#7929b5fbaed7f0530cbe79947e7bc9e4f471b683"
8979+
integrity sha512-hVfj0QjgxBch4j5M6BfMj9Dsm7iKioP9uQUvs00RGkuUdG+Gc0PQcT9SuLrcZJjUE/2Tp9+J44BEbOYxjesqlA==
89758980
dependencies:
8976-
"@windicss/plugin-utils" "0.5.0"
8981+
"@windicss/plugin-utils" "0.5.3"
89778982
windicss "^2.2.0"
89788983

89798984
vite@2.0.3:

0 commit comments

Comments
 (0)