Skip to content
guoling edited this page Apr 28, 2024 · 27 revisions

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 both Android, iOS/macOS, Windows, POSIX and HarmonyOS NEXT.

MMKV for Android

Features

  • Efficient. MMKV uses mmap to keep memory synced with files, and protobuf to encode/decode values, making the most of Android to achieve the best performance.

    • Multi-Process concurrency: MMKV supports concurrent read-read and read-write access between processes.
  • Easy-to-use. You can use MMKV as you go. All changes are saved immediately, no sync, no apply calls needed.

  • Small.

    • A handful files: MMKV contains process locks, encode/decode helpers and mmap logics, and nothing more. It's really tidy.
    • About 60K in binary size: MMKV adds about 60K per architecture on App size, and much less when zipped (APK).

Getting Started

Installation Via Maven

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

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

Starting from v1.2.8, MMKV has been migrated to Maven Central. Older versions (<= v1.2.7) are still available on JCenter.
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 code:

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

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

MMKV has a global instance, you can use it 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 is 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

  • Efficient. MMKV uses mmap to keep memory synced with files, and protobuf to encode/decode values, making the most of iOS to achieve the best performance.

  • Easy-to-use. You can use MMKV as you go, no configurations are needed. All changes are saved immediately, no synchronize calls are needed.

  • Small

    • A handful files: MMKV contains encode/decode helpers and mmap logics and nothing more. It's really tidy.
    • Less than 30K in binary size: MMKV adds less than 30K per architecture on App size, and much less when zipped (IPA).

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

  • Efficient. MMKV uses mmap to keep memory synced with file, and protobuf to encode/decode values, making the most of Windows to achieve the best performance.

    • Multi-Process concurrency: MMKV supports concurrent read-read and read-write access between processes.
  • Easy-to-use. You can use MMKV as you go. All changes are saved immediately, no save, no sync calls are needed.

  • Small.

    • A handful of files: MMKV contains process locks, encode/decode helpers and mmap logics, and nothing more. It's really tidy.
    • About 10K in binary size: MMKV adds about 10K on application size, and much less when zipped.

Getting Started

Installation Via Source

  1. Getting source code from the git repository:

    git clone https://github.com/Tencent/MMKV.git
  2. Add Windows/MMKV/MMKV.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 wise 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

  • Efficient. MMKV uses mmap to keep memory synced with files, and protobuf to encode/decode values, making the most of POSIX to achieve the best performance.

    • Multi-Process concurrency: MMKV supports concurrent read-read and read-write access between processes.
  • Easy-to-use. You can use MMKV as you go. All changes are saved immediately, no save, no sync calls are needed.

  • Small.

    • A handful of files: MMKV contains process locks, encode/decode helpers and mmap logics, and nothing more. It's really tidy.
    • About 7K in binary size: MMKV adds about 7K on application size, and much less when zipped.

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

  • 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.

    • Multi-Process concurrency: MMKV supports concurrent read-read and read-write access between processes.
  • Easy-to-use. You can use MMKV as you go. All changes are saved immediately, no sync, no flush calls needed.

  • Small.

    • A handful of files: MMKV contains process locks, encode/decode helpers and mmap logics and nothing more. It's really tidy.
    • About 600K in binary size: MMKV adds about 600K per architecture on App size, and much less when zipped (HAR/HAP).

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.

Clone this wiki locally