MMKV README

The project's README from github.com/Tencent/MMKV. As one markdown file: README.md.

license PRs Welcome Release Version Platform

中文版本请参看这里

MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application. It's currently available on Android, iOS/macOS, Windows, POSIX and HarmonyOS NEXT, with experimental Kotlin Multiplatform support.

MMKV for Kotlin Multiplatform

The v2.4.2 Kotlin Multiplatform package is experimental and currently targets Android and iOS. Its API and artifact layout may change in a future release. Add it to the shared module:

kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation("com.tencent:mmkv-kmp:2.4.2")
        }
    }
}

Supported targets are Android, iosArm64, iosSimulatorArm64, and iosX64. See the Kotlin Multiplatform guide for initialization and packaging details.

MMKV for Android

Features

Getting Started

Installation Via Maven

Add the following lines to build.gradle on your app module:

dependencies {
    implementation 'com.tencent:mmkv:2.4.2'
    // replace "2.4.2" with any available version
}

Starting from v2.0.0, MMKV no longer supports 32-bit arch and API level 22 or 21, if you want 32-bit or API level 21~22, use v1.3.x LTS series.

For other installation options, see Android Setup.

Quick Tutorial

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 Application class, add these lines:

public void onCreate() {
    super.onCreate();

    String rootDir = MMKV.initialize(this);
    System.out.println("mmkv root: " + rootDir);
    //……
}

MMKV has a global instance, that can be used directly:

import com.tencent.mmkv.MMKV;
    
MMKV kv = MMKV.defaultMMKV();

kv.encode("bool", true);
boolean bValue = kv.decodeBool("bool");

kv.encode("int", Integer.MIN_VALUE);
int iValue = kv.decodeInt("int");

kv.encode("string", "Hello from mmkv");
String str = kv.decodeString("string");

MMKV also supports Multi-Process Access. Full tutorials can be found here Android Tutorial.

Performance

Writing random int for 1000 times, we get this chart:

For more benchmark data, please refer to our benchmark.

MMKV for iOS/macOS

Features

Getting Started

Installation Via CocoaPods:

  1. Install CocoaPods;
  2. Open the terminal, cd to your project directory, run pod repo update to make CocoaPods aware of the latest available MMKV versions;
  3. Edit your Podfile, add pod 'MMKV' to your app target;
  4. Run pod install;
  5. Open the .xcworkspace file generated by CocoaPods;
  6. Add #import <MMKV/MMKV.h> to your source file and we are done.

For other installation options, see iOS/macOS Setup.

Quick Tutorial

You can use MMKV as you go, no configurations are needed. All changes are saved immediately, no synchronize calls are needed. Setup MMKV on App startup, in your -[MyApp application: didFinishLaunchingWithOptions:], add these lines:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // init MMKV in the main thread
    [MMKV initializeMMKV:nil];

    //...
    return YES;
}

MMKV has a global instance, that can be used directly:

MMKV *mmkv = [MMKV defaultMMKV];
    
[mmkv setBool:YES forKey:@"bool"];
BOOL bValue = [mmkv getBoolForKey:@"bool"];
    
[mmkv setInt32:-1024 forKey:@"int32"];
int32_t iValue = [mmkv getInt32ForKey:@"int32"];
    
[mmkv setString:@"hello, mmkv" forKey:@"string"];
NSString *str = [mmkv getStringForKey:@"string"];

MMKV also supports Multi-Process Access. Full tutorials can be found here.

Performance

Writing random int for 10000 times, we get this chart:

For more benchmark data, please refer to our benchmark.

MMKV for Windows

Features

Getting Started

Installation Via Source

  1. Getting source code from git repository:

    git clone https://github.com/Tencent/MMKV.git
    
  2. Add Core/core.vcxproj to your solution;

  3. Add MMKV project to your project's dependencies;

  4. Add $(OutDir)include to your project's C/C++ -> General -> Additional Include Directories;

  5. Add $(OutDir) to your project's Linker -> General -> Additional Library Directories;

  6. Add mmkv.lib to your project's Linker -> Input -> Additional Dependencies;

  7. Add #include <MMKV/MMKV.h> to your source file and we are done.

note:

  1. MMKV is compiled with MT/MTd runtime by default. If your project uses MD/MDd, you should change MMKV's setting to match your project's (C/C++ -> Code Generation -> Runtime Library), or vice versa.
  2. MMKV is developed with Visual Studio 2017, change the Platform Toolset if you use a different version of Visual Studio.

For other installation options, see Windows Setup.

Quick Tutorial

You can use MMKV as you go. All changes are saved immediately, no sync, no save calls needed.
Setup MMKV on App startup, say in your main(), add these lines:

#include <MMKV/MMKV.h>

int main() {
    std::wstring rootDir = getYourAppDocumentDir();
    MMKV::initializeMMKV(rootDir);
    //...
}

MMKV has a global instance, that can be used directly:

auto mmkv = MMKV::defaultMMKV();

mmkv->set(true, "bool");
std::cout << "bool = " << mmkv->getBool("bool") << std::endl;

mmkv->set(1024, "int32");
std::cout << "int32 = " << mmkv->getInt32("int32") << std::endl;

mmkv->set("Hello, MMKV for Windows", "string");
std::string result;
mmkv->getString("string", result);
std::cout << "string = " << result << std::endl;

MMKV also supports Multi-Process Access. Full tutorials can be found here Windows Tutorial.

MMKV for POSIX

Features

Getting Started

Installation Via CMake

  1. Getting source code from the git repository:

    git clone https://github.com/Tencent/MMKV.git
    
  2. Edit your CMakeLists.txt, add those lines:

    add_subdirectory(mmkv/POSIX/src mmkv)
    target_link_libraries(MyApp
        mmkv)
    
  3. Add #include "MMKV.h" to your source file and we are done.

For other installation options, see POSIX Setup.

Quick Tutorial

You can use MMKV as you go. All changes are saved immediately, no sync, no save calls needed.
Setup MMKV on App startup, say in your main(), add these lines:

#include "MMKV.h"

int main() {
    std::string rootDir = getYourAppDocumentDir();
    MMKV::initializeMMKV(rootDir);
    //...
}

MMKV has a global instance, that can be used directly:

auto mmkv = MMKV::defaultMMKV();

mmkv->set(true, "bool");
std::cout << "bool = " << mmkv->getBool("bool") << std::endl;

mmkv->set(1024, "int32");
std::cout << "int32 = " << mmkv->getInt32("int32") << std::endl;

mmkv->set("Hello, MMKV for Windows", "string");
std::string result;
mmkv->getString("string", result);
std::cout << "string = " << result << std::endl;

MMKV also supports Multi-Process Access. Full tutorials can be found here POSIX Tutorial.

MMKV for HarmonyOS NEXT

Features

Getting Started

Installation via OHPM:

ohpm install @tencent/mmkv

Quick Tutorial

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));

As you can see, MMKV is quite easy to use. For the full documentation, see HarmonyOS NEXT Tutorial.

License

MMKV is published under the BSD 3-Clause license. For details check out the LICENSE.TXT.

Change Log

Check out the CHANGELOG.md for details of change history.

Contributing

If you are interested in contributing, check out the CONTRIBUTING.md, also join our Tencent OpenSource Plan. MMKV has officially joined the Tencent Device-oriented Service Product Alliance, working together with other alliance members to build an open and mutually beneficial frontend technology product ecosystem.

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.

FAQ & Feedback

Check out the FAQ first. Should there be any questions, don't hesitate to create issues.

Personal Information Protection Rules

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.

MMKV (Tencent/MMKV)

Searching Database…

18.8K ·GitHub
这是什么?MMKV 是微信开发的一个高效、小巧的键值存储框架,被移动端和桌面端应用广泛使用,用来持久化保存数据,可替代 SharedPreferences、NSUserDefaults 或普通文件。
它是数据库,还是更像简单的键值缓存?它是一个轻量级的嵌入式键值存储,而不是完整的关系型数据库:它使用 mmap 让内存与文件保持同步,并用 protobuf 对值进行编码和解码,没有查询语言。
我该如何把它添加到项目中?在 Android 上,在 build.gradle 中添加 Maven 依赖;在 iOS/macOS 上,在 Podfile 中添加 'pod MMKV',然后运行 pod install;在 Windows/POSIX 上,通过 Visual Studio 或 CMake 从源码构建;在 HarmonyOS 上,通过 OHPM 安装。
使用前还需要准备别的东西吗,比如 32 位的编译目标?从 v2.0.0 开始,MMKV 在 Android 上不再支持 32 位架构以及 API 21/22;如果你需要这些,请改用较早的 v1.3.x LTS 系列。
Kotlin Multiplatform 支持可以用于生产环境吗?不可以。KMP 包明确标注为实验性功能,目前只支持 Android 和 iOS,其 API 和产物结构仍可能变化。
它实际支持哪些平台和语言?它原生支持 Android、iOS、macOS、Windows、POSIX 和 HarmonyOS NEXT,提供 Java/Kotlin、Swift/Objective-C、C++ 的绑定,以及实验性的 Flutter、Python 和 Go 封装。
MMKV 会收集或发送我的任何数据吗?不会。MMKV 明确声明,它不会收集、获取或上传应用中的任何个人信息。
写入数据后,我需要调用保存或同步吗?不需要。MMKV 在每次操作时都会立即写入更改,因此无需像 SharedPreferences 或 NSUserDefaults 那样调用任何显式的 save、sync 或 apply。
多个进程能否安全地同时读写同一个 MMKV 文件?可以。在 Android、Windows 和 POSIX 上,MMKV 通过进程锁支持进程之间并发的读-读和读-写访问。
它会给我的应用增加多大的二进制体积?体积很小:Android 上每个架构约 50K,iOS/macOS 上不到 30K,Windows 上约 10K,POSIX 上约 7K,HarmonyOS NEXT 上约 600K。
微信为什么要自己开发 MMKV,而不用现有的存储 API?MMKV 的设计目标是超越 SharedPreferences、NSUserDefaults 等标准方案的性能,它结合了基于 mmap 的存储和 protobuf 编码,兼顾速度与极小的体积,这也是微信在生产环境中使用它的原因。
Creator
Tencent
Size
47.8 MB
Stars
18.8K
Age
8y
Commits · 90 Days