OpenHarmony/MMKV/README.md
MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application. It's now available on HarmonyOS NEXT.
Efficient. MMKV uses mmap to keep memory synced with file, and protobuf to encode/decode values, making the most of native platform to achieve best performance.
Easy-to-use. You can use MMKV as you go. All changes are saved immediately, no sync, no flush calls needed.
Small.
This is the fastest and recommended way to add MMKV to your project.
ohpm install @tencent/mmkv
Or, you can add it to your project manually.
Add the following lines to oh-package.json5 on your app module.
"dependencies": {
"@tencent/mmkv": "~2.4.0",
}
Then run
ohpm install
For additional installation methods, checkout the wiki.
You can use MMKV as you go. All changes are saved immediately, no sync, no apply calls needed.
Setup MMKV on App startup, say your EntryAbility.onCreate() function, add these lines:
import { MMKV } from '@tencent/mmkv';
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
let appCtx = this.context.getApplicationContext();
let mmkvRootDir = MMKV.initialize(appCtx);
console.info('mmkv rootDir: ', mmkvRootDir);
……
}
MMKV has a global instance, that can be used directly:
import { MMKV } from '@tencent/mmkv';
let mmkv = MMKV.defaultMMKV();
mmkv.encodeBool('bool', true);
console.info('bool = ', mmkv.decodeBool('bool'));
mmkv.encodeInt32('int32', Math.pow(2, 31) - 1);
console.info('max int32 = ', mmkv.decodeInt32('int32'));
mmkv.encodeInt64('int', BigInt(2**63) - BigInt(1));
console.info('max int64 = ', mmkv.decodeInt64('int'));
let str: string = 'Hello OpenHarmony from MMKV';
mmkv.encodeString('string', str);
console.info('string = ', mmkv.decodeString('string'));
let arrayBuffer: ArrayBuffer = StringToArrayBuffer('Hello OpenHarmony from MMKV with bytes');
mmkv.encodeBytes('bytes', arrayBuffer);
let bytes = mmkv.decodeBytes('bytes');
console.info('bytes = ', ArrayBufferToString(bytes));
let arr = new Uint8Array([0, 255, 1, 255]);
mmkv.encodeTypedArray('uint8-array', arr);
let newUI8Arr = kv.decodeUint8Array('uint8-array');
console.info('uint8-array = ', newUI8Arr);
As you can see, MMKV is quite easy to use.
Deleting & Querying:
mmkv.removeValueForKey('bool');
console.info('contains "bool"', mmkv.containsKey('bool'));
mmkv.removeValuesForKeys(['int32', 'int']);
console.info('all keys: ', mmkv.allKeys().join());
If different modules/logic need isolated storage, you can also create your own MMKV instance separately:
var mmkv = MMKV.mmkvWithID('test');
mmkv.encodeBool('bool', true);
console.info('bool = ', mmkv.decodeBool('bool'));
If multi-process accessing is needed,you can set MMKV.MULTI_PROCESS_MODE on MMKV initialization:
var mmkv = MMKV.mmkvWithID('test-multi-process', MMKV.MULTI_PROCESS_MODE);
mmkv.encodeBool('bool', true);
console.info('bool = ', mmkv.decodeBool('bool'));
Primitive Types:
boolean, number, bigint, stringClasses & Collections:
boolean[], number[], string[], ArrayBuffer, TypedArrayBy default, MMKV prints log to hilog, which is not convenient for diagnosing online issues. You can setup MMKV log redirecting on App startup on the native interface of MMKV. Checkout how to do it on C++. Due to the current limitation of NAPI runtime, we can't efficiently redirect log to the JavaScript side.
You can turn off MMKV's logging once and for all on initialization (which we strongly disrecommend).
import { MMKV, MMKVLogLevel } from '@tencent/mmkv';
MMKV.initialize(appCtx, MMKVLogLevel.None);
By default MMKV stores all key-values in plain text on file, relying on Android's/iOS's sandbox to make sure the file is encrypted. Should you worry about information leaking, you can choose to encrypt MMKV.
let encryptKey = 'MyEncryptKey';
let mmkv = MMKV.mmkvWithID('test-encryption', MMKV.SINGLE_PROCESS_MODE, encryptKey);
You can change the encryption key later as you like. You can also change an existing MMKV instance from encrypted to unencrypted, or vice versa.
// an unencrypted MMKV instance
let mmkv = MMKV.mmkvWithID('test-encryption');
// change from unencrypted to encrypted with AES-128 key length
mmkv.reKey('Key_seq_1');
// change encryption key with AES-258 key length
mmkv.reKey('Key_Seq_Very_Looooooooong', true);
// change from encrypted to unencrypted
kmmkv.reKey();
By default, MMKV stores file inside $(FilesDir)/mmkv/. You can customize MMKV's root directory on App startup:
let appCtx = this.context.getApplicationContext();
let rootDir = appCtx.filesDir + '/mmkv_2';
let cacheDir = appCtx.cacheDir;
MMKV.initializeWithPath(rootDir, cacheDir);
You can even customize any MMKV instance's location:
let appCtx = this.context.getApplicationContext();
let rootDir = appCtx.filesDir + '/mmkv_3';
var mmkv = MMKV.mmkvWithID('testCustomDir', MMKV.SINGLE_PROCESS_MODE, null, rootDir);
Note: It's recommended to store MMKV files inside your App's sandbox path. DO NOT store them on external storage(aka SD card).
For additional documents, checkout the wiki.
MMKV is published under the BSD 3-Clause license. For details check out the LICENSE.TXT.
Check out the CHANGELOG.md for details of change history.
If you are interested in contributing, check out the CONTRIBUTING.md, also join our Tencent OpenSource Plan.
To give clarity of what is expected of our members, MMKV has adopted the code of conduct defined by the Contributor Covenant, which is widely used. And we think it articulates our values well. For more, check out the Code of Conduct.
Check out the FAQ first. Should there be any questions, don't hesitate to create issues.