Showing
2 changed files
with
154 additions
and
1 deletions
| 1 | +import { distributedKVStore } from '@kit.ArkData'; | ||
| 2 | +import { BusinessError } from '@kit.BasicServicesKit'; | ||
| 3 | +import { AppUtils } from './AppUtils'; | ||
| 4 | +import { Logger } from './Logger'; | ||
| 5 | + | ||
| 6 | +const TAG = 'KVStoreHelper' | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * 键值型数据库管理类,类似sp,存储变大,单条数据,value<4M | ||
| 10 | + */ | ||
| 11 | +export class KVStoreHelper { | ||
| 12 | + private static _context: Context; | ||
| 13 | + // TODO 待优化,可以指定数据库名,创建多个数据库。当前没有需求,只创建一个,缓存接口数据用。 | ||
| 14 | + private static _default_store_id: string = 'default_kv_store'; | ||
| 15 | + private kvManager: distributedKVStore.KVManager | undefined = undefined; | ||
| 16 | + private kvStore: distributedKVStore.SingleKVStore | undefined = undefined; | ||
| 17 | + | ||
| 18 | + private constructor() { | ||
| 19 | + Logger.error(TAG, 'constructor') | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + static init(context: Context) { | ||
| 23 | + Logger.error(TAG, 'init') | ||
| 24 | + KVStoreHelper._context = context; | ||
| 25 | + KVStoreHelper.default.createKVManager() | ||
| 26 | + KVStoreHelper.default.createKVStore() | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + // 静态属性 | ||
| 30 | + static default: KVStoreHelper = new KVStoreHelper(); | ||
| 31 | + | ||
| 32 | + private createKVManager() { | ||
| 33 | + if (this.kvManager) { | ||
| 34 | + return | ||
| 35 | + } | ||
| 36 | + if (!KVStoreHelper._context) { | ||
| 37 | + Logger.fatal(TAG, 'context is null, must be initialized first') | ||
| 38 | + return | ||
| 39 | + } | ||
| 40 | + let context: Context = KVStoreHelper._context; | ||
| 41 | + const kvManagerConfig: distributedKVStore.KVManagerConfig = { | ||
| 42 | + context: context, | ||
| 43 | + bundleName: AppUtils.getPackageName(context) | ||
| 44 | + }; | ||
| 45 | + try { | ||
| 46 | + // 创建KVManager实例 | ||
| 47 | + this.kvManager = distributedKVStore.createKVManager(kvManagerConfig); | ||
| 48 | + Logger.info(TAG, 'Succeeded in creating KVManager.'); | ||
| 49 | + // 继续创建获取数据库 | ||
| 50 | + } catch (e) { | ||
| 51 | + let error = e as BusinessError; | ||
| 52 | + Logger.error(TAG, `Failed to create KVManager. Code:${error.code},message:${error.message}`); | ||
| 53 | + } | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + private createKVStore() { | ||
| 57 | + if (this.kvStore) { | ||
| 58 | + return | ||
| 59 | + } | ||
| 60 | + if (!this.kvManager) { | ||
| 61 | + this.createKVManager() | ||
| 62 | + // 直接拦截,避免陷入循环 | ||
| 63 | + Logger.error(TAG, 'kvManager is null, please re-create it and try again') | ||
| 64 | + return | ||
| 65 | + } | ||
| 66 | + try { | ||
| 67 | + const options: distributedKVStore.Options = { | ||
| 68 | + createIfMissing: true, | ||
| 69 | + encrypt: false, | ||
| 70 | + backup: false, | ||
| 71 | + autoSync: false, | ||
| 72 | + // kvStoreType不填时,默认创建多设备协同数据库 | ||
| 73 | + kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION, | ||
| 74 | + securityLevel: distributedKVStore.SecurityLevel.S1 | ||
| 75 | + }; | ||
| 76 | + this.kvManager?.getKVStore<distributedKVStore.SingleKVStore>(KVStoreHelper._default_store_id, options, | ||
| 77 | + (err, store: distributedKVStore.SingleKVStore) => { | ||
| 78 | + if (err) { | ||
| 79 | + Logger.error(TAG, `Failed to get KVStore: Code:${err.code},message:${err.message}`); | ||
| 80 | + return; | ||
| 81 | + } | ||
| 82 | + Logger.info(TAG, 'Succeeded in getting KVStore.'); | ||
| 83 | + this.kvStore = store; | ||
| 84 | + // 请确保获取到键值数据库实例后,再进行相关数据操作 | ||
| 85 | + }); | ||
| 86 | + } catch (e) { | ||
| 87 | + let error = e as BusinessError; | ||
| 88 | + Logger.error(TAG, `An unexpected error occurred. Code:${error.code},message:${error.message}`); | ||
| 89 | + } | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + private checkStoreCreated() { | ||
| 93 | + if (!this.kvManager) { | ||
| 94 | + this.createKVManager() | ||
| 95 | + } | ||
| 96 | + if (!this.kvStore) { | ||
| 97 | + this.createKVStore() | ||
| 98 | + } | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + get(key: string, def: boolean | string | number | Uint8Array): Promise<boolean | string | number | Uint8Array> { | ||
| 102 | + // this.checkStoreCreated() | ||
| 103 | + return new Promise<boolean | string | number | Uint8Array>((success, failed) => { | ||
| 104 | + try { | ||
| 105 | + this.kvStore?.get(key, (err, data) => { | ||
| 106 | + if (err != undefined) { | ||
| 107 | + Logger.debug(TAG, `Failed to get data. Code:${err.code},message:${err.message}`); | ||
| 108 | + success(def) | ||
| 109 | + return; | ||
| 110 | + } | ||
| 111 | + success(data) | ||
| 112 | + Logger.debug(TAG, `Succeeded in getting data. Data:${data}`); | ||
| 113 | + }); | ||
| 114 | + } catch (e) { | ||
| 115 | + success(def) | ||
| 116 | + let error = e as BusinessError; | ||
| 117 | + Logger.error(TAG, `Failed to get data. Code:${error.code},message:${error.message}`); | ||
| 118 | + } | ||
| 119 | + }); | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + put(key: string, value: boolean | string | number | Uint8Array) { | ||
| 123 | + // this.checkStoreCreated() | ||
| 124 | + try { | ||
| 125 | + this.kvStore?.put(key, value, (err) => { | ||
| 126 | + if (err !== undefined) { | ||
| 127 | + Logger.debug(TAG, `Failed to put data. Code:${err.code},message:${err.message}`); | ||
| 128 | + return; | ||
| 129 | + } | ||
| 130 | + Logger.debug(TAG, 'Succeeded in putting data.'); | ||
| 131 | + }); | ||
| 132 | + } catch (e) { | ||
| 133 | + let error = e as BusinessError; | ||
| 134 | + Logger.error(TAG, `An unexpected error occurred. Code:${error.code},message:${error.message}`); | ||
| 135 | + } | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + del(key: string) { | ||
| 139 | + // this.checkStoreCreated() | ||
| 140 | + try { | ||
| 141 | + this.kvStore?.delete(key, (err) => { | ||
| 142 | + if (err !== undefined) { | ||
| 143 | + Logger.debug(TAG, `Failed to delete data. Code:${err.code},message:${err.message}`); | ||
| 144 | + return; | ||
| 145 | + } | ||
| 146 | + Logger.debug(TAG, 'Succeeded in deleting data.'); | ||
| 147 | + }); | ||
| 148 | + } catch (e) { | ||
| 149 | + let error = e as BusinessError; | ||
| 150 | + Logger.error(TAG, `An unexpected error occurred. Code:${error.code},message:${error.message}`); | ||
| 151 | + } | ||
| 152 | + } | ||
| 153 | +} |
| @@ -4,7 +4,7 @@ import { Logger } from './Logger'; | @@ -4,7 +4,7 @@ import { Logger } from './Logger'; | ||
| 4 | const TAG = 'SPHelper' | 4 | const TAG = 'SPHelper' |
| 5 | 5 | ||
| 6 | /** | 6 | /** |
| 7 | - * sp存储 | 7 | + * sp存储,单条数据,value<1k;业务数据超过1k的,建议使用KVStoreHelper |
| 8 | */ | 8 | */ |
| 9 | export class SPHelper { | 9 | export class SPHelper { |
| 10 | private static context: Context; | 10 | private static context: Context; |
-
Please register or login to post a comment