@@ -27,8 +27,14 @@ export interface LSOptions<T extends object = Record<string, unknown>> {
27
27
* @example
28
28
* ```ts
29
29
* const stor = await new LiteStorage({ uuid: 'test1' }).ready();
30
+ *
31
+ * // 示例1: 读取缓存
30
32
* const config = stor.get();
31
33
* console.log(config);
34
+ * // 缓存操作
35
+ * // 存入
36
+ * stor.save(config);
37
+ *
32
38
* stor.getItem('key1');
33
39
* stor.setItem('key2', { a: 1 });
34
40
* stor.removeItem('key2');
@@ -66,13 +72,11 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
66
72
}
67
73
private init ( ) {
68
74
let { filepath = 'ls.json' , uuid, version } = this . options ;
69
- if ( ! / \. ( j s o n 5 ? | t o m l ) $ / i. test ( filepath ) ) {
70
- filepath = resolve ( this . baseDir , filepath , 'ls.json' ) ;
71
- }
75
+ if ( ! / \. ( j s o n 5 ? | t o m l ) $ / i. test ( filepath ) ) filepath = resolve ( this . baseDir , filepath , 'ls.json' ) ;
76
+
72
77
this . isJson5 = / \. j s o n 5 $ / i. test ( filepath ) ;
73
78
this . isToml = / \. t o m l $ / i. test ( filepath ) ;
74
79
this . options . filepath = resolve ( this . baseDir , filepath ) ;
75
-
76
80
this . cache = {
77
81
version,
78
82
data : {
@@ -93,10 +97,13 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
93
97
/** 主动保存 */
94
98
public async save ( value ?: T , mode : 'merge' | 'cover' = 'merge' ) {
95
99
if ( value ) return this . set ( value , mode ) ;
96
-
100
+ await this . reload ( ) ;
101
+ return this . toCache ( ) ;
102
+ }
103
+ private async toCache ( ) {
97
104
const cacheDir = dirname ( this . cachePath ) ;
98
105
if ( ! fs . existsSync ( cacheDir ) ) fs . mkdirSync ( cacheDir , { recursive : true } ) ;
99
- await this . reload ( ) ;
106
+
100
107
let content = '' ;
101
108
if ( this . isToml ) {
102
109
const TOML = await import ( '@iarna/toml' ) ;
@@ -126,6 +133,7 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
126
133
}
127
134
return this ;
128
135
}
136
+ /** 以 options.uuid 为 key 设置数据 */
129
137
public set ( value : T , mode : 'merge' | 'cover' = 'merge' ) {
130
138
const uuid = this . options . uuid ;
131
139
@@ -141,22 +149,29 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
141
149
}
142
150
return this ;
143
151
}
152
+ /**
153
+ * 以 options.uuid 为 key 获取数据
154
+ * @param raw 是否返回原始数据(不进行深拷贝)。默认为 false
155
+ */
144
156
public get ( raw = false ) {
145
157
const info = this . cache . data [ this . options . uuid ] || { } ;
146
158
return raw ? info : { ...info } ;
147
159
}
160
+ /** 获取全量缓存的原始数据 */
148
161
public getAll ( ) {
149
162
return this . cache ;
150
163
}
151
164
/** 移除一项数据 */
152
- public del ( key : keyof T ) {
165
+ public async del ( key : keyof T ) {
153
166
const info = this . cache . data [ this . options . uuid ] ;
154
167
if ( key in info ) {
168
+ await this . reload ( ) ;
155
169
delete info [ key ] ;
156
- this . save ( ) ;
170
+ return this . toCache ( ) ;
157
171
}
158
172
return this ;
159
173
}
174
+ /** 设置并保存一个数据项。提示:setItem、removeItem 都会触发文件读写,应避免密集高频调用 */
160
175
public setItem < K extends keyof T > ( key : K , value : Partial < T [ K ] > , mode : 'merge' | 'cover' = 'merge' ) {
161
176
const data = this . get ( true ) ;
162
177
if ( mode === 'cover' ) data [ key ] = value as T [ K ] ;
@@ -177,14 +192,18 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
177
192
* 清理缓存
178
193
* @param isAll 是否清空全部缓存(即移除缓存文件重新初始化)。默认为 false,只清空当前 uuid 约束下的缓存数据
179
194
*/
180
- public clear ( isAll = false ) {
195
+ public async clear ( isAll = false ) {
181
196
if ( isAll ) {
182
197
if ( fs . existsSync ( this . cachePath ) ) fs . rmSync ( this . cachePath , { force : true } ) ;
183
198
this . init ( ) ;
184
199
} else {
185
200
const uuid = this . options . uuid ;
186
- if ( this . cache . data [ uuid ] ) delete this . cache . data [ uuid ] ;
187
- this . save ( ) ;
201
+ if ( this . cache . data [ uuid ] ) {
202
+ await this . reload ( ) ;
203
+ delete this . cache . data [ uuid ] ;
204
+ return this . toCache ( ) ;
205
+ }
188
206
}
207
+ return this ;
189
208
}
190
209
}
0 commit comments