@shencom/utils-storage
Version:
320 lines (293 loc) • 7.3 kB
text/typescript
interface StorageOption {
scid: string;
get: (key: string) => string | null;
set: (key: string, value: string) => void;
remove: (key: string) => void;
clear: () => void;
keys: () => string[];
}
type PrefixData = 'data_';
type PrefixUser = 'user_';
type PrefixLasting = 'lasting_';
class StorageBase {
/** 数据 前缀 */
protected readonly prefixData: PrefixData = 'data_';
/** 用户 前缀 */
protected readonly prefixUser: PrefixUser = 'user_';
/** 永久 前缀 */
protected readonly prefixLasting: PrefixLasting = 'lasting_';
private readonly _prefix: string;
constructor(private readonly _local: StorageOption) {
this._prefix = `sc_${_local.scid}_`;
}
private _handleData<T>(data: string | null, key: string): T | null {
const value = data ? JSON.parse(data) : null;
if (!value) {
return null;
}
if (value.time) {
const now = new Date().getTime(); // 当前时间
if (Number(value.time) < now) {
this._local.remove(this._prefix + key);
return null;
}
return value.value;
}
return value.value;
}
/**
* 获取本地存储数据
*
* @template T
* @example
* ```ts
* const data = storage.get(key);
* ```
* @param {string} key 存储的key
* @return {T | null}
* @memberof StorageBase
*/
public get<T = any>(key: string): T | null {
try {
const data = this._local.get(this._prefix + key);
return this._handleData<T>(data, key);
} catch (error) {
return null;
}
}
/**
* 设置本地存储数据
*
* @example
* ```ts
* storage.set(key, data, 10); // 10分钟
* storage.set(key, data, 0); // 永久
* ```
* @param {string} key 存储的key
* @param {any} data 存储的值
* @param {number} [time] 存储时间(单位为分钟),默认: `永久`
* @memberof StorageBase
*/
public set(key: string, data: any, time = 0) {
const H = time * 1000 * 60;
const now = new Date().getTime() + H;
const val = JSON.stringify({ time: time ? String(now) : null, value: data });
this._local.set(this._prefix + key, val);
}
/**
* 移除指定本地存储数据
*
* @example
* ```ts
* storage.remove(key);
* ```
* @param {string} key 存储的key
* @memberof StorageBase
*/
public remove(key: string) {
this._local.remove(this._prefix + key);
}
/**
* 清空本地存储数据,默认保留 lasting 前缀字段数据
*
* @param {boolean} [isAll] 是否全部清除,默认: `false`
* @example
* ```ts
* storage.clear(); // 保留 lasting 前缀字段数据
* storage.clear(true); // 全部清除
* ```
* @return {void}
* @memberof StorageBase
*/
public clear(isAll = false): void {
if (isAll) {
this._local.clear();
return;
}
const reg = new RegExp(`^${this.prefixLasting}`);
this.keys().forEach((key) => {
if (!reg.test(key)) {
this.remove(key);
}
});
}
/**
* 获取本地存储所有的 key
* @example
* ```ts
* const keys = storage.keys();
* ```
* @return {string[]}
* @memberof StorageBase
*/
public keys(): string[] {
return this._local
.keys()
.map((key) => key.replace(new RegExp(`^${this._prefix}`), ''))
.filter(Boolean);
}
}
export default class ScStorageBase extends StorageBase {
constructor(_local: StorageOption) {
super(_local);
}
/**
* 清除自定义固定前缀的数据
*
* @param {string} prefix 前缀
* @memberof ScStorageBase
*/
public clearPrefix(prefix: string) {
const reg = new RegExp(`^${prefix}`);
const data_keys = this.keys().filter((key) => reg.test(key));
data_keys.forEach((key) => {
this.remove(key);
});
}
/**
* 获取 `user_` 开头的数据
*
* @template T
* @example
* ```ts
* const data = storage.getUser(key);
* ```
* @param {string} key 存储的key
* @return {T | null}
* @memberof ScStorageBase
*/
public getUser<T = any>(key: string): T | null {
return this.get<T>(this.prefixUser + key);
}
/**
* 设置 `user_` 开头的数据
* @example
* ```ts
* storage.setUser(key, data, 10); // 10分钟
* storage.setUser(key, data); // 永久
* ```
* @param {string} key 存储的key
* @param {*} data 存储的值
* @param {number} [time] 存储时间(单位为分钟),默认: `永久`
* @memberof ScStorageBase
*/
public setUser(key: string, data: any, time = 0) {
this.set(this.prefixUser + key, data, time);
}
/**
* 移除以 `user_` 开头的数据
* @example
* ```ts
* storage.removeUser(key);
* ```
* @memberof ScStorageBase
*/
public removeUser(key: string) {
this.remove(this.prefixUser + key);
}
/**
* 清除所有以 `user_` 开头的数据
*
* @memberof ScStorage
*/
public clearUser() {
this.clearPrefix(this.prefixUser);
}
/**
* 获取 `data_` 开头的数据
*
* @template T
* @example
* ```ts
* const data = storage.getData(key);
* ```
* @param {string} key 存储的key
* @return {T | null}
* @memberof ScStorageBase
*/
public getData<T = any>(key: string): T | null {
return this.get<T>(this.prefixData + key);
}
/**
* 设置 `data_` 开头的数据
* @example
* ```ts
* storage.setData(key, data, 10); // 10分钟
* storage.setData(key, data); // 永久
* ```
* @param {string} key 存储的key
* @param {*} data 存储的值
* @param {number} [time] 存储时间(单位为分钟),默认: `永久`
* @memberof ScStorageBase
*/
public setData(key: string, data: any, time = 0) {
this.set(this.prefixData + key, data, time);
}
/**
* 移除以 `data_` 开头的数据
* @example
* ```ts
* storage.removeData(key);
* ```
* @memberof ScStorageBase
*/
public removeData(key: string) {
this.remove(this.prefixData + key);
}
/**
* 清除所有以 `data_` 开头的数据
*
* @memberof ScStorage
*/
public clearData() {
this.clearPrefix(this.prefixData);
}
/**
* 获取 `lasting_` 开头的数据
*
* @template T
* @example
* ```ts
* const data = storage.getLasting(key);
* ```
* @param {string} key 存储的key
* @return {T | null}
* @memberof ScStorageBase
*/
public getLasting<T = any>(key: string): T | null {
return this.get<T>(this.prefixLasting + key);
}
/**
* 设置 `lasting_` 开头的数据,永久存储
* 使用 `clear` 方法默认是不清空这个数据
* @example
* ```ts
* storage.setLasting(key, data);
* ```
* @param {string} key 存储的key
* @param {*} data 存储的值
* @memberof ScStorageBase
*/
public setLasting(key: string, data: any) {
this.set(this.prefixLasting + key, data);
}
/**
* 移除以 `lasting_` 开头的数据
* @example
* ```ts
* storage.removeLasting(key);
* ```
* @memberof ScStorageBase
*/
public removeLasting(key: string) {
this.remove(this.prefixLasting + key);
}
/**
* 清除所有以 `lasting_` 开头的数据
*
* @memberof ScStorage
*/
public clearLasting() {
this.clearPrefix(this.prefixLasting);
}
}