@ureq/lib-cache-store 0.0.3-alpha.0 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +170 -0
- package/dist/index.js +78 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +75 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# @ureq/lib-cache-store
|
|
2
|
+
|
|
3
|
+
缓存存储实现,提供内存缓存和 Storage 缓存。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @ureq/lib-cache-store
|
|
9
|
+
# 或
|
|
10
|
+
pnpm add @ureq/lib-cache-store
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 使用
|
|
14
|
+
|
|
15
|
+
### MemoryCacheStore
|
|
16
|
+
|
|
17
|
+
内存缓存实现,适用于浏览器和 Node.js。
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { Request } from '@ureq/core';
|
|
21
|
+
import { FetchRequestor } from '@ureq/impl-fetch';
|
|
22
|
+
import { MemoryCacheStore } from '@ureq/lib-cache-store';
|
|
23
|
+
|
|
24
|
+
const cacheStore = new MemoryCacheStore();
|
|
25
|
+
|
|
26
|
+
const request = new Request(
|
|
27
|
+
new FetchRequestor(),
|
|
28
|
+
{
|
|
29
|
+
cache: {
|
|
30
|
+
store: cacheStore,
|
|
31
|
+
ttl: 60000 // 60 秒
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### StorageCacheStore
|
|
38
|
+
|
|
39
|
+
基于 Web Storage API 的缓存实现(localStorage/sessionStorage)。
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { StorageCacheStore } from '@ureq/lib-cache-store';
|
|
43
|
+
|
|
44
|
+
// 使用 localStorage
|
|
45
|
+
const localCache = new StorageCacheStore(localStorage);
|
|
46
|
+
|
|
47
|
+
// 使用 sessionStorage
|
|
48
|
+
const sessionCache = new StorageCacheStore(sessionStorage);
|
|
49
|
+
|
|
50
|
+
const request = new Request(
|
|
51
|
+
new FetchRequestor(),
|
|
52
|
+
{
|
|
53
|
+
cache: {
|
|
54
|
+
store: localCache,
|
|
55
|
+
ttl: 3600000 // 1 小时
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## API
|
|
62
|
+
|
|
63
|
+
### MemoryCacheStore
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
class MemoryCacheStore implements CacheStore {
|
|
67
|
+
get<T>(key: string): Promise<T | null>;
|
|
68
|
+
set<T>(key: string, value: T, ttl: number): Promise<void>;
|
|
69
|
+
delete(key: string): Promise<void>;
|
|
70
|
+
clear(): Promise<void>;
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### StorageCacheStore
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
class StorageCacheStore implements CacheStore {
|
|
78
|
+
constructor(storage: Storage);
|
|
79
|
+
|
|
80
|
+
get<T>(key: string): Promise<T | null>;
|
|
81
|
+
set<T>(key: string, value: T, ttl: number): Promise<void>;
|
|
82
|
+
delete(key: string): Promise<void>;
|
|
83
|
+
clear(): Promise<void>;
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## 特性
|
|
88
|
+
|
|
89
|
+
### MemoryCacheStore
|
|
90
|
+
|
|
91
|
+
- ✅ 快速访问
|
|
92
|
+
- ✅ 支持浏览器和 Node.js
|
|
93
|
+
- ✅ 自动过期清理
|
|
94
|
+
- ⚠️ 数据不持久化
|
|
95
|
+
|
|
96
|
+
### StorageCacheStore
|
|
97
|
+
|
|
98
|
+
- ✅ 数据持久化
|
|
99
|
+
- ✅ 跨页面共享(localStorage)
|
|
100
|
+
- ✅ 自动过期清理
|
|
101
|
+
- ⚠️ 仅支持浏览器
|
|
102
|
+
- ⚠️ 存储空间有限(通常 5-10MB)
|
|
103
|
+
|
|
104
|
+
## 示例
|
|
105
|
+
|
|
106
|
+
### 基础用法
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { MemoryCacheStore } from '@ureq/lib-cache-store';
|
|
110
|
+
|
|
111
|
+
const cache = new MemoryCacheStore();
|
|
112
|
+
|
|
113
|
+
// 存储数据
|
|
114
|
+
await cache.set('key', { data: 'value' }, 60000);
|
|
115
|
+
|
|
116
|
+
// 获取数据
|
|
117
|
+
const data = await cache.get('key');
|
|
118
|
+
|
|
119
|
+
// 删除数据
|
|
120
|
+
await cache.delete('key');
|
|
121
|
+
|
|
122
|
+
// 清空缓存
|
|
123
|
+
await cache.clear();
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### 持久化缓存
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
import { StorageCacheStore } from '@ureq/lib-cache-store';
|
|
130
|
+
|
|
131
|
+
const cache = new StorageCacheStore(localStorage);
|
|
132
|
+
|
|
133
|
+
// 数据会保存到 localStorage
|
|
134
|
+
await cache.set('user', { id: 1, name: 'John' }, 3600000);
|
|
135
|
+
|
|
136
|
+
// 刷新页面后仍然可以获取
|
|
137
|
+
const user = await cache.get('user');
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### 自定义缓存实现
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import { CacheStore } from '@ureq/core';
|
|
144
|
+
|
|
145
|
+
class RedisCacheStore implements CacheStore {
|
|
146
|
+
async get<T>(key: string): Promise<T | null> {
|
|
147
|
+
// 从 Redis 获取
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async set<T>(key: string, value: T, ttl: number): Promise<void> {
|
|
151
|
+
// 存储到 Redis
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async delete(key: string): Promise<void> {
|
|
155
|
+
// 从 Redis 删除
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async clear(): Promise<void> {
|
|
159
|
+
// 清空 Redis
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## 文档
|
|
165
|
+
|
|
166
|
+
查看完整文档:[https://sunny-117.github.io/ureq](https://sunny-117.github.io/ureq)
|
|
167
|
+
|
|
168
|
+
## License
|
|
169
|
+
|
|
170
|
+
MIT
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,79 @@
|
|
|
1
|
-
'use strict';
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/memory.ts
|
|
4
|
+
var MemoryStore = class {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.store = /* @__PURE__ */ new Map();
|
|
7
|
+
}
|
|
8
|
+
set(key, value, ttl = 0) {
|
|
9
|
+
this.store.set(key, {
|
|
10
|
+
value,
|
|
11
|
+
timestamp: Date.now(),
|
|
12
|
+
ttl
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
get(key) {
|
|
16
|
+
const item = this.store.get(key);
|
|
17
|
+
if (!item) return void 0;
|
|
18
|
+
if (item.ttl && Date.now() - item.timestamp > item.ttl) {
|
|
19
|
+
this.store.delete(key);
|
|
20
|
+
return void 0;
|
|
21
|
+
}
|
|
22
|
+
return item.value;
|
|
23
|
+
}
|
|
24
|
+
has(key) {
|
|
25
|
+
return this.store.has(key);
|
|
26
|
+
}
|
|
27
|
+
delete(key) {
|
|
28
|
+
this.store.delete(key);
|
|
29
|
+
}
|
|
30
|
+
clear() {
|
|
31
|
+
this.store.clear();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// src/storage.ts
|
|
36
|
+
var StorageStore = class {
|
|
37
|
+
constructor(storage = localStorage) {
|
|
38
|
+
this.storage = storage;
|
|
39
|
+
}
|
|
40
|
+
getKey(key) {
|
|
41
|
+
return `cache:${key}`;
|
|
42
|
+
}
|
|
43
|
+
set(key, value, ttl = 0) {
|
|
44
|
+
const item = {
|
|
45
|
+
value,
|
|
46
|
+
timestamp: Date.now(),
|
|
47
|
+
ttl
|
|
48
|
+
};
|
|
49
|
+
this.storage.setItem(this.getKey(key), JSON.stringify(item));
|
|
50
|
+
}
|
|
51
|
+
get(key) {
|
|
52
|
+
const data = this.storage.getItem(this.getKey(key));
|
|
53
|
+
if (!data) return void 0;
|
|
54
|
+
const item = JSON.parse(data);
|
|
55
|
+
if (item.ttl && Date.now() - item.timestamp > item.ttl) {
|
|
56
|
+
this.delete(key);
|
|
57
|
+
return void 0;
|
|
58
|
+
}
|
|
59
|
+
return item.value;
|
|
60
|
+
}
|
|
61
|
+
has(key) {
|
|
62
|
+
return this.storage.getItem(this.getKey(key)) !== null;
|
|
63
|
+
}
|
|
64
|
+
delete(key) {
|
|
65
|
+
this.storage.removeItem(this.getKey(key));
|
|
66
|
+
}
|
|
67
|
+
clear() {
|
|
68
|
+
Object.keys(this.storage).forEach((key) => {
|
|
69
|
+
if (key.startsWith("cache:")) {
|
|
70
|
+
this.storage.removeItem(key);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
exports.MemoryStore = MemoryStore;
|
|
77
|
+
exports.StorageStore = StorageStore;
|
|
78
|
+
//# sourceMappingURL=index.js.map
|
|
2
79
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/memory.ts","../src/storage.ts"],"names":[
|
|
1
|
+
{"version":3,"sources":["../src/memory.ts","../src/storage.ts"],"names":[],"mappings":";;;AAMO,IAAM,cAAN,MAAkB;AAAA,EAAlB,WAAA,GAAA;AACL,IAAQ,IAAA,CAAA,KAAA,uBAAY,GAA4B,EAAA;AAAA;AAAA,EAEhD,GAAO,CAAA,GAAA,EAAa,KAAU,EAAA,GAAA,GAAc,CAAS,EAAA;AACnD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K,CAAA,MAAA,CAAO,GAAG,CAAG,EAAA,IAAA,CAAK,SAAU,CAAA,IAAI,CAAC,CAAA;AAAA;AAC7D,EAEA,IAAO,GAA4B,EAAA;AACjC,IAAA,MAAM,OAAO,IAAK,CAAA,OAAA,CAAQ,QAAQ,IAAK,CAAA,MAAA,CAAO,GAAG,CAAC,CAAA;AAClD,IAAI,IAAA,CAAC,MAAa,OAAA,MAAA;AAElB,IAAM,MAAA,IAAA,GAAO,IAAK,CAAA,KAAA,CAAM,IAAI,CAAA;AAC5B,IAAI,IAAA,IAAA,CAAK,OAAO,IAAK,CAAA,GAAA,KAAQ,IAAK,CAAA,SAAA,GAAY,KAAK,GAAK,EAAA;AACtD,MAAA,IAAA,CAAK,OAAO,GAAG,CAAA;AACf,MAAO,OAAA,MAAA;AAAA;AAGT,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACd,EAEA,IAAI,GAAsB,EAAA;AACxB,IAAA,OAAO,KAAK,OAAQ,CAAA,OAAA,CAAQ,KAAK,MAAO,CAAA,GAAG,CAAC,CAAM,KAAA,IAAA;AAAA;AACpD,EAEA,OAAO,GAAmB,EAAA;AACxB,IAAA,IAAA,CAAK,OAAQ,CAAA,UAAA,CAAW,IAAK,CAAA,MAAA,CAAO,GAAG,CAAC,CAAA;AAAA;AAC1C,EAEA,KAAc,GAAA;AACZ,IAAA,MAAA,CAAO,IAAK,CAAA,IAAA,CAAK,OAAO,CAAA,CAAE,QAAQ,CAAO,GAAA,KAAA;AACvC,MAAI,IAAA,GAAA,CAAI,UAAW,CAAA,QAAQ,CAAG,EAAA;AAC5B,QAAK,IAAA,CAAA,OAAA,CAAQ,WAAW,GAAG,CAAA;AAAA;AAC7B,KACD,CAAA;AAAA;AAEL","file":"index.js","sourcesContent":["interface CacheItem<T> {\n value: T;\n timestamp: number;\n ttl: number;\n}\n\nexport class MemoryStore {\n private store = new Map<string, CacheItem<any>>();\n\n set<T>(key: string, value: T, ttl: number = 0): void {\n this.store.set(key, {\n value,\n timestamp: Date.now(),\n ttl,\n });\n }\n\n get<T>(key: string): T | undefined {\n const item = this.store.get(key);\n if (!item) return undefined;\n\n if (item.ttl && Date.now() - item.timestamp > item.ttl) {\n this.store.delete(key);\n return undefined;\n }\n\n return item.value as T;\n }\n\n has(key: string): boolean {\n return this.store.has(key);\n }\n\n delete(key: string): void {\n this.store.delete(key);\n }\n\n clear(): void {\n this.store.clear();\n }\n} ","export class StorageStore {\n constructor(private storage: Storage = localStorage) {}\n\n private getKey(key: string): string {\n return `cache:${key}`;\n }\n\n set<T>(key: string, value: T, ttl: number = 0): void {\n const item = {\n value,\n timestamp: Date.now(),\n ttl,\n };\n this.storage.setItem(this.getKey(key), JSON.stringify(item));\n }\n\n get<T>(key: string): T | undefined {\n const data = this.storage.getItem(this.getKey(key));\n if (!data) return undefined;\n\n const item = JSON.parse(data);\n if (item.ttl && Date.now() - item.timestamp > item.ttl) {\n this.delete(key);\n return undefined;\n }\n\n return item.value as T;\n }\n\n has(key: string): boolean {\n return this.storage.getItem(this.getKey(key)) !== null;\n }\n\n delete(key: string): void {\n this.storage.removeItem(this.getKey(key));\n }\n\n clear(): void {\n Object.keys(this.storage).forEach(key => {\n if (key.startsWith('cache:')) {\n this.storage.removeItem(key);\n }\n });\n }\n} "]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,76 @@
|
|
|
1
|
-
|
|
1
|
+
// src/memory.ts
|
|
2
|
+
var MemoryStore = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.store = /* @__PURE__ */ new Map();
|
|
5
|
+
}
|
|
6
|
+
set(key, value, ttl = 0) {
|
|
7
|
+
this.store.set(key, {
|
|
8
|
+
value,
|
|
9
|
+
timestamp: Date.now(),
|
|
10
|
+
ttl
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
get(key) {
|
|
14
|
+
const item = this.store.get(key);
|
|
15
|
+
if (!item) return void 0;
|
|
16
|
+
if (item.ttl && Date.now() - item.timestamp > item.ttl) {
|
|
17
|
+
this.store.delete(key);
|
|
18
|
+
return void 0;
|
|
19
|
+
}
|
|
20
|
+
return item.value;
|
|
21
|
+
}
|
|
22
|
+
has(key) {
|
|
23
|
+
return this.store.has(key);
|
|
24
|
+
}
|
|
25
|
+
delete(key) {
|
|
26
|
+
this.store.delete(key);
|
|
27
|
+
}
|
|
28
|
+
clear() {
|
|
29
|
+
this.store.clear();
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// src/storage.ts
|
|
34
|
+
var StorageStore = class {
|
|
35
|
+
constructor(storage = localStorage) {
|
|
36
|
+
this.storage = storage;
|
|
37
|
+
}
|
|
38
|
+
getKey(key) {
|
|
39
|
+
return `cache:${key}`;
|
|
40
|
+
}
|
|
41
|
+
set(key, value, ttl = 0) {
|
|
42
|
+
const item = {
|
|
43
|
+
value,
|
|
44
|
+
timestamp: Date.now(),
|
|
45
|
+
ttl
|
|
46
|
+
};
|
|
47
|
+
this.storage.setItem(this.getKey(key), JSON.stringify(item));
|
|
48
|
+
}
|
|
49
|
+
get(key) {
|
|
50
|
+
const data = this.storage.getItem(this.getKey(key));
|
|
51
|
+
if (!data) return void 0;
|
|
52
|
+
const item = JSON.parse(data);
|
|
53
|
+
if (item.ttl && Date.now() - item.timestamp > item.ttl) {
|
|
54
|
+
this.delete(key);
|
|
55
|
+
return void 0;
|
|
56
|
+
}
|
|
57
|
+
return item.value;
|
|
58
|
+
}
|
|
59
|
+
has(key) {
|
|
60
|
+
return this.storage.getItem(this.getKey(key)) !== null;
|
|
61
|
+
}
|
|
62
|
+
delete(key) {
|
|
63
|
+
this.storage.removeItem(this.getKey(key));
|
|
64
|
+
}
|
|
65
|
+
clear() {
|
|
66
|
+
Object.keys(this.storage).forEach((key) => {
|
|
67
|
+
if (key.startsWith("cache:")) {
|
|
68
|
+
this.storage.removeItem(key);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export { MemoryStore, StorageStore };
|
|
75
|
+
//# sourceMappingURL=index.mjs.map
|
|
2
76
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/memory.ts","../src/storage.ts"],"names":[
|
|
1
|
+
{"version":3,"sources":["../src/memory.ts","../src/storage.ts"],"names":[],"mappings":";AAMO,IAAM,cAAN,MAAkB;AAAA,EAAlB,WAAA,GAAA;AACL,IAAQ,IAAA,CAAA,KAAA,uBAAY,GAA4B,EAAA;AAAA;AAAA,EAEhD,GAAO,CAAA,GAAA,EAAa,KAAU,EAAA,GAAA,GAAc,CAAS,EAAA;AACnD,IAAK,IAAA,CAAA,KAAA,CAAM,IAAI,GAAK,EAAA;AAAA,MAClB,KAAA;AAAA,MACA,SAAA,EAAW,KAAK,GAAI,EAAA;AAAA,MACpB;AAAA,KACD,CAAA;AAAA;AACH,EAEA,IAAO,GAA4B,EAAA;AACjC,IAAA,MAAM,IAAO,GAAA,IAAA,CAAK,KAAM,CAAA,GAAA,CAAI,GAAG,CAAA;AAC/B,IAAI,IAAA,CAAC,MAAa,OAAA,MAAA;AAElB,IAAI,IAAA,IAAA,CAAK,OAAO,IAAK,CAAA,GAAA,KAAQ,IAAK,CAAA,SAAA,GAAY,KAAK,GAAK,EAAA;AACtD,MAAK,IAAA,CAAA,KAAA,CAAM,OAAO,GAAG,CAAA;AACrB,MAAO,OAAA,MAAA;AAAA;AAGT,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACd,EAEA,IAAI,GAAsB,EAAA;AACxB,IAAO,OAAA,IAAA,CAAK,KAAM,CAAA,GAAA,CAAI,GAAG,CAAA;AAAA;AAC3B,EAEA,OAAO,GAAmB,EAAA;AACxB,IAAK,IAAA,CAAA,KAAA,CAAM,OAAO,GAAG,CAAA;AAAA;AACvB,EAEA,KAAc,GAAA;AACZ,IAAA,IAAA,CAAK,MAAM,KAAM,EAAA;AAAA;AAErB;;;ACx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AA;AAC1C,EAEA,KAAc,GAAA;AACZ,IAAA,MAAA,CAAO,IAAK,CAAA,IAAA,CAAK,OAAO,CAAA,CAAE,QAAQ,CAAO,GAAA,KAAA;AACvC,MAAI,IAAA,GAAA,CAAI,UAAW,CAAA,QAAQ,CAAG,EAAA;AAC5B,QAAK,IAAA,CAAA,OAAA,CAAQ,WAAW,GAAG,CAAA;AAAA;AAC7B,KACD,CAAA;AAAA;AAEL","file":"index.mjs","sourcesContent":["interface CacheItem<T> {\n value: T;\n timestamp: number;\n ttl: number;\n}\n\nexport class MemoryStore {\n private store = new Map<string, CacheItem<any>>();\n\n set<T>(key: string, value: T, ttl: number = 0): void {\n this.store.set(key, {\n value,\n timestamp: Date.now(),\n ttl,\n });\n }\n\n get<T>(key: string): T | undefined {\n const item = this.store.get(key);\n if (!item) return undefined;\n\n if (item.ttl && Date.now() - item.timestamp > item.ttl) {\n this.store.delete(key);\n return undefined;\n }\n\n return item.value as T;\n }\n\n has(key: string): boolean {\n return this.store.has(key);\n }\n\n delete(key: string): void {\n this.store.delete(key);\n }\n\n clear(): void {\n this.store.clear();\n }\n} ","export class StorageStore {\n constructor(private storage: Storage = localStorage) {}\n\n private getKey(key: string): string {\n return `cache:${key}`;\n }\n\n set<T>(key: string, value: T, ttl: number = 0): void {\n const item = {\n value,\n timestamp: Date.now(),\n ttl,\n };\n this.storage.setItem(this.getKey(key), JSON.stringify(item));\n }\n\n get<T>(key: string): T | undefined {\n const data = this.storage.getItem(this.getKey(key));\n if (!data) return undefined;\n\n const item = JSON.parse(data);\n if (item.ttl && Date.now() - item.timestamp > item.ttl) {\n this.delete(key);\n return undefined;\n }\n\n return item.value as T;\n }\n\n has(key: string): boolean {\n return this.storage.getItem(this.getKey(key)) !== null;\n }\n\n delete(key: string): void {\n this.storage.removeItem(this.getKey(key));\n }\n\n clear(): void {\n Object.keys(this.storage).forEach(key => {\n if (key.startsWith('cache:')) {\n this.storage.removeItem(key);\n }\n });\n }\n} "]}
|