@ureq/lib-cache-store 0.0.3 → 0.0.4
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 +75 -71
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -12,92 +12,93 @@ pnpm add @ureq/lib-cache-store
|
|
|
12
12
|
|
|
13
13
|
## 使用
|
|
14
14
|
|
|
15
|
-
###
|
|
15
|
+
### MemoryStore
|
|
16
16
|
|
|
17
17
|
内存缓存实现,适用于浏览器和 Node.js。
|
|
18
18
|
|
|
19
19
|
```typescript
|
|
20
|
-
import {
|
|
21
|
-
import { FetchRequestor } from '@ureq/impl-fetch';
|
|
22
|
-
import { MemoryCacheStore } from '@ureq/lib-cache-store';
|
|
20
|
+
import { MemoryStore } from '@ureq/lib-cache-store';
|
|
23
21
|
|
|
24
|
-
const
|
|
22
|
+
const cache = new MemoryStore();
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
24
|
+
// 存储数据(TTL 单位:毫秒)
|
|
25
|
+
cache.set('key', { data: 'value' }, 60000);
|
|
26
|
+
|
|
27
|
+
// 获取数据
|
|
28
|
+
const data = cache.get('key');
|
|
29
|
+
|
|
30
|
+
// 检查是否存在
|
|
31
|
+
const exists = cache.has('key');
|
|
32
|
+
|
|
33
|
+
// 删除数据
|
|
34
|
+
cache.delete('key');
|
|
35
|
+
|
|
36
|
+
// 清空缓存
|
|
37
|
+
cache.clear();
|
|
35
38
|
```
|
|
36
39
|
|
|
37
|
-
###
|
|
40
|
+
### StorageStore
|
|
38
41
|
|
|
39
42
|
基于 Web Storage API 的缓存实现(localStorage/sessionStorage)。
|
|
40
43
|
|
|
41
44
|
```typescript
|
|
42
|
-
import {
|
|
45
|
+
import { StorageStore } from '@ureq/lib-cache-store';
|
|
43
46
|
|
|
44
47
|
// 使用 localStorage
|
|
45
|
-
const localCache = new
|
|
48
|
+
const localCache = new StorageStore(localStorage);
|
|
46
49
|
|
|
47
50
|
// 使用 sessionStorage
|
|
48
|
-
const sessionCache = new
|
|
51
|
+
const sessionCache = new StorageStore(sessionStorage);
|
|
49
52
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
cache: {
|
|
54
|
-
store: localCache,
|
|
55
|
-
ttl: 3600000 // 1 小时
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
);
|
|
53
|
+
// 使用方式与 MemoryStore 相同
|
|
54
|
+
localCache.set('user', { id: 1, name: 'John' }, 3600000);
|
|
55
|
+
const user = localCache.get('user');
|
|
59
56
|
```
|
|
60
57
|
|
|
61
58
|
## API
|
|
62
59
|
|
|
63
|
-
###
|
|
60
|
+
### MemoryStore
|
|
64
61
|
|
|
65
62
|
```typescript
|
|
66
|
-
class
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
63
|
+
class MemoryStore {
|
|
64
|
+
set<T>(key: string, value: T, ttl?: number): void;
|
|
65
|
+
get<T>(key: string): T | undefined;
|
|
66
|
+
has(key: string): boolean;
|
|
67
|
+
delete(key: string): void;
|
|
68
|
+
clear(): void;
|
|
71
69
|
}
|
|
72
70
|
```
|
|
73
71
|
|
|
74
|
-
###
|
|
72
|
+
### StorageStore
|
|
75
73
|
|
|
76
74
|
```typescript
|
|
77
|
-
class
|
|
78
|
-
constructor(storage
|
|
75
|
+
class StorageStore {
|
|
76
|
+
constructor(storage?: Storage);
|
|
79
77
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
78
|
+
set<T>(key: string, value: T, ttl?: number): void;
|
|
79
|
+
get<T>(key: string): T | undefined;
|
|
80
|
+
has(key: string): boolean;
|
|
81
|
+
delete(key: string): void;
|
|
82
|
+
clear(): void;
|
|
84
83
|
}
|
|
85
84
|
```
|
|
86
85
|
|
|
87
86
|
## 特性
|
|
88
87
|
|
|
89
|
-
###
|
|
88
|
+
### MemoryStore
|
|
90
89
|
|
|
91
90
|
- ✅ 快速访问
|
|
92
91
|
- ✅ 支持浏览器和 Node.js
|
|
93
92
|
- ✅ 自动过期清理
|
|
93
|
+
- ✅ 同步 API
|
|
94
94
|
- ⚠️ 数据不持久化
|
|
95
95
|
|
|
96
|
-
###
|
|
96
|
+
### StorageStore
|
|
97
97
|
|
|
98
98
|
- ✅ 数据持久化
|
|
99
99
|
- ✅ 跨页面共享(localStorage)
|
|
100
100
|
- ✅ 自动过期清理
|
|
101
|
+
- ✅ 同步 API
|
|
101
102
|
- ⚠️ 仅支持浏览器
|
|
102
103
|
- ⚠️ 存储空间有限(通常 5-10MB)
|
|
103
104
|
|
|
@@ -106,59 +107,62 @@ class StorageCacheStore implements CacheStore {
|
|
|
106
107
|
### 基础用法
|
|
107
108
|
|
|
108
109
|
```typescript
|
|
109
|
-
import {
|
|
110
|
+
import { MemoryStore } from '@ureq/lib-cache-store';
|
|
110
111
|
|
|
111
|
-
const cache = new
|
|
112
|
+
const cache = new MemoryStore();
|
|
112
113
|
|
|
113
|
-
//
|
|
114
|
-
|
|
114
|
+
// 存储数据(TTL: 60 秒)
|
|
115
|
+
cache.set('key', { data: 'value' }, 60000);
|
|
115
116
|
|
|
116
117
|
// 获取数据
|
|
117
|
-
const data =
|
|
118
|
+
const data = cache.get('key');
|
|
119
|
+
|
|
120
|
+
// 检查是否存在
|
|
121
|
+
if (cache.has('key')) {
|
|
122
|
+
console.log('Key exists');
|
|
123
|
+
}
|
|
118
124
|
|
|
119
125
|
// 删除数据
|
|
120
|
-
|
|
126
|
+
cache.delete('key');
|
|
121
127
|
|
|
122
128
|
// 清空缓存
|
|
123
|
-
|
|
129
|
+
cache.clear();
|
|
124
130
|
```
|
|
125
131
|
|
|
126
132
|
### 持久化缓存
|
|
127
133
|
|
|
128
134
|
```typescript
|
|
129
|
-
import {
|
|
135
|
+
import { StorageStore } from '@ureq/lib-cache-store';
|
|
130
136
|
|
|
131
|
-
const cache = new
|
|
137
|
+
const cache = new StorageStore(localStorage);
|
|
132
138
|
|
|
133
139
|
// 数据会保存到 localStorage
|
|
134
|
-
|
|
140
|
+
cache.set('user', { id: 1, name: 'John' }, 3600000);
|
|
135
141
|
|
|
136
142
|
// 刷新页面后仍然可以获取
|
|
137
|
-
const user =
|
|
143
|
+
const user = cache.get('user');
|
|
138
144
|
```
|
|
139
145
|
|
|
140
|
-
###
|
|
146
|
+
### 与 @ureq/core 集成
|
|
141
147
|
|
|
142
148
|
```typescript
|
|
143
|
-
import {
|
|
149
|
+
import { Request } from '@ureq/core';
|
|
150
|
+
import { FetchRequestor } from '@ureq/impl-fetch';
|
|
151
|
+
import { LibCacheStore } from '@ureq/business';
|
|
152
|
+
import { MemoryStore } from '@ureq/lib-cache-store';
|
|
144
153
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
// 从 Redis 删除
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
async clear(): Promise<void> {
|
|
159
|
-
// 清空 Redis
|
|
154
|
+
const memoryStore = new MemoryStore();
|
|
155
|
+
const cacheStore = new LibCacheStore(memoryStore);
|
|
156
|
+
|
|
157
|
+
const request = new Request(
|
|
158
|
+
new FetchRequestor(),
|
|
159
|
+
{
|
|
160
|
+
cache: {
|
|
161
|
+
store: cacheStore,
|
|
162
|
+
ttl: 60000
|
|
163
|
+
}
|
|
160
164
|
}
|
|
161
|
-
|
|
165
|
+
);
|
|
162
166
|
```
|
|
163
167
|
|
|
164
168
|
## 文档
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ureq/lib-cache-store",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Cache store implementations for universal request library",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -18,6 +18,6 @@
|
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "tsup --config tsup.config.ts",
|
|
20
20
|
"dev": "tsup --config tsup.config.ts --watch",
|
|
21
|
-
"test": "vitest"
|
|
21
|
+
"test": "vitest --run"
|
|
22
22
|
}
|
|
23
23
|
}
|