cache.ts
1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { Preference } from '@vben-core/typings';
class PreferenceCache {
cachePrefix: string;
constructor(cachePrefix: string) {
this.cachePrefix = cachePrefix;
}
/**
* 从 localStorage 中获取偏好设置
* @returns 返回从 localStorage 中获取的偏好设置,如果没有获取到,则返回默认偏好设置。
*/
get(defaultValue: Preference): Preference {
let cache = defaultValue;
try {
cache = JSON.parse(localStorage.getItem(this.getCacheKey()) || '');
} catch {
return defaultValue;
}
return cache;
}
/**
* 获取偏好设置的缓存键
*/
getCacheKey(name: string = 'preference') {
return `__${this.cachePrefix}-${name}__`;
}
/**
* 从 localStorage 中移除偏好设置
*/
remove() {
localStorage.removeItem(this.getCacheKey());
localStorage.removeItem(this.getCacheKey('locale'));
localStorage.removeItem(this.getCacheKey('theme'));
}
/**
* 将当前偏好设置持久化到 localStorage
*/
set(preference: Preference) {
localStorage.setItem(this.getCacheKey(), JSON.stringify(preference));
// 额外存储一份主题、语言
localStorage.setItem(this.getCacheKey('locale'), preference.locale);
localStorage.setItem(this.getCacheKey('theme'), preference.theme);
}
/**
* 设置偏好设置的缓存前缀
* @param prefix - 前缀
*/
setCachePrefix(prefix: string) {
this.cachePrefix = prefix;
}
}
export type PreferenceCacheType = PreferenceCache;
export { PreferenceCache };