-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
iOS_advance_cn
MMKV 是基于 mmap 内存映射的 key-value 组件,底层序列化/反序列化使用 protobuf 实现,性能高,稳定性强。从 2015 年中至今在微信上使用,其性能和稳定性经过了时间的验证。近期也已移植到 Android / macOS / Win32 / POSIX 平台,一并开源。
MMKV 有一些高级设置,可以使得更符合你的需求。
-
MMKV 默认将日志打印到 console,对线上问题的解决很不便。你可以在 App 启动时接收转发 MMKV 的日志。实现
MMKVHandler
协议,添加类似下面的代码:- (void)mmkvLogWithLevel:(MMKVLogLevel)level file:(const char *)file line:(int)line func:(const char *)funcname message:(NSString *)message { const char *levelDesc = nullptr; switch (level) { case MMKVLogDebug: levelDesc = "D"; break; case MMKVLogInfo: levelDesc = "I"; break; case MMKVLogWarning: levelDesc = "W"; break; case MMKVLogError: levelDesc = "E"; break; default: levelDesc = "N"; break; } // use your own logging tool //NSLog(@"[%s] <%s:%d::%s> %@", levelDesc, file, line, funcname, message); }
至于使用哪个客户端日志组件,我们推荐使用 xlog,同样也是开源自微信团队。
-
如果你不希望 MMKV 打印日志,你可以在 MMKV 初始化时一劳永逸地关掉它(虽然我们强烈不建议你这么做)。
[MMKV initializeMMKV:nil logLevel:MMKVLogNone];
-
在 crc 校验失败,或者文件长度不对的时候,MMKV 默认会丢弃所有数据。你可以让 MMKV 恢复数据。要注意的是修复率无法保证,而且可能修复出奇怪的 key-value。同样地也是实现
MMKVHandler
协议,添加以下代码:- (MMKVRecoverStrategic)onMMKVCRCCheckFail:(NSString *)mmapID { return MMKVOnErrorRecover; } - (MMKVRecoverStrategic)onMMKVFileLengthError:(NSString *)mmapID { return MMKVOnErrorRecover; }
-
MMKV 默认把文件存放在
$(Documents)/mmkv/
目录。你可以在 MMKV 初始化时自定义根目录:NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); auto libraryPath = (NSString *) [paths firstObject]; auto rootDir = [libraryPath stringByAppendingPathComponent:@"mmkv_2"]; [MMKV initializeMMKV:rootDir];
-
MMKV 甚至支持自定义某个文件的目录:
auto path = [MMKV mmkvBasePath]; path = [path stringByDeletingLastPathComponent]; path = [path stringByAppendingPathComponent:@"mmkv_3"]; auto mmkv = [MMKV mmkvWithID:@"test/case1" relativePath:path];
-
MMKV 默认明文存储所有 key-value,依赖 iOS/macOS 系统的沙盒机制保证文件加密。如果你担心信息泄露,你可以选择加密 MMKV。
NSData *cryptKey = [@"My-Encrypt-Key" dataUsingEncoding:NSUTF8StringEncoding]; auto mmkv = [MMKV mmkvWithID:@"MyID" cryptKey:cryptKey];
-
你可以更改密钥,也可以将一个加密 MMKV 改成明文,或者反过来。
NSString *mmapID = @"testAES_reKey"; // an unencrypted MMKV instance MMKV *kv = [MMKV mmkvWithID:mmapID cryptKey:nullptr]; NSData *key_1 = [@"Key_seq_1" dataUsingEncoding:NSUTF8StringEncoding]; // change from unencrypted to encrypted [kv reKey:key_1]; NSData *key_2 = [@"Key_seq_2" dataUsingEncoding:NSUTF8StringEncoding]; // change encryption key [kv reKey:key_2]; // change from encrypted to unencrypted [kv reKey:nullptr];
-
MMKV 默认缓存所有访问过的 MMKV 实例, 并在收到 memory warning 的时候清理他们的内部缓存。不过 MMKV 不会彻底销毁他们,除非手工调用
-[MMKV close]
。你可以在 MMKV 初始化后调用+[MMKV enableAutoCleanUp:]
开启 自动清理逻辑:// auto clean up instances that not been accessed in 10 minutes [MMKV enableAutoCleanUp:10];
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.
User privacy is taken very seriously: MMKV does not obtain, collect or upload any personal information. Please refer to the MMKV SDK Personal Information Protection Rules for details.
- In English
- 中文
- In English
- 中文
- In English
- 中文
-
In English
-
中文
-
Golang