Skip to content

Commit 383fdc8

Browse files
committed
fix(LiteStorage): 修复删除缓存项无效的问题
1 parent 185b716 commit 383fdc8

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

src/node/LiteStorage.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ export interface LSOptions<T extends object = Record<string, unknown>> {
2727
* @example
2828
* ```ts
2929
* const stor = await new LiteStorage({ uuid: 'test1' }).ready();
30+
*
31+
* // 示例1: 读取缓存
3032
* const config = stor.get();
3133
* console.log(config);
34+
* // 缓存操作
35+
* // 存入
36+
* stor.save(config);
37+
*
3238
* stor.getItem('key1');
3339
* stor.setItem('key2', { a: 1 });
3440
* stor.removeItem('key2');
@@ -66,13 +72,11 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
6672
}
6773
private init() {
6874
let { filepath = 'ls.json', uuid, version } = this.options;
69-
if (!/\.(json5?|toml)$/i.test(filepath)) {
70-
filepath = resolve(this.baseDir, filepath, 'ls.json');
71-
}
75+
if (!/\.(json5?|toml)$/i.test(filepath)) filepath = resolve(this.baseDir, filepath, 'ls.json');
76+
7277
this.isJson5 = /\.json5$/i.test(filepath);
7378
this.isToml = /\.toml$/i.test(filepath);
7479
this.options.filepath = resolve(this.baseDir, filepath);
75-
7680
this.cache = {
7781
version,
7882
data: {
@@ -93,10 +97,13 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
9397
/** 主动保存 */
9498
public async save(value?: T, mode: 'merge' | 'cover' = 'merge') {
9599
if (value) return this.set(value, mode);
96-
100+
await this.reload();
101+
return this.toCache();
102+
}
103+
private async toCache() {
97104
const cacheDir = dirname(this.cachePath);
98105
if (!fs.existsSync(cacheDir)) fs.mkdirSync(cacheDir, { recursive: true });
99-
await this.reload();
106+
100107
let content = '';
101108
if (this.isToml) {
102109
const TOML = await import('@iarna/toml');
@@ -126,6 +133,7 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
126133
}
127134
return this;
128135
}
136+
/** 以 options.uuid 为 key 设置数据 */
129137
public set(value: T, mode: 'merge' | 'cover' = 'merge') {
130138
const uuid = this.options.uuid;
131139

@@ -141,22 +149,29 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
141149
}
142150
return this;
143151
}
152+
/**
153+
* 以 options.uuid 为 key 获取数据
154+
* @param raw 是否返回原始数据(不进行深拷贝)。默认为 false
155+
*/
144156
public get(raw = false) {
145157
const info = this.cache.data[this.options.uuid] || {};
146158
return raw ? info : { ...info };
147159
}
160+
/** 获取全量缓存的原始数据 */
148161
public getAll() {
149162
return this.cache;
150163
}
151164
/** 移除一项数据 */
152-
public del(key: keyof T) {
165+
public async del(key: keyof T) {
153166
const info = this.cache.data[this.options.uuid];
154167
if (key in info) {
168+
await this.reload();
155169
delete info[key];
156-
this.save();
170+
return this.toCache();
157171
}
158172
return this;
159173
}
174+
/** 设置并保存一个数据项。提示:setItem、removeItem 都会触发文件读写,应避免密集高频调用 */
160175
public setItem<K extends keyof T>(key: K, value: Partial<T[K]>, mode: 'merge' | 'cover' = 'merge') {
161176
const data = this.get(true);
162177
if (mode === 'cover') data[key] = value as T[K];
@@ -177,14 +192,18 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
177192
* 清理缓存
178193
* @param isAll 是否清空全部缓存(即移除缓存文件重新初始化)。默认为 false,只清空当前 uuid 约束下的缓存数据
179194
*/
180-
public clear(isAll = false) {
195+
public async clear(isAll = false) {
181196
if (isAll) {
182197
if (fs.existsSync(this.cachePath)) fs.rmSync(this.cachePath, { force: true });
183198
this.init();
184199
} else {
185200
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+
}
188206
}
207+
return this;
189208
}
190209
}

0 commit comments

Comments
 (0)