Scripts2026年5月19日·1 分钟阅读

MMKV — Efficient Mobile Key-Value Storage Framework by WeChat

MMKV is a high-performance key-value storage framework developed by WeChat. It uses memory-mapped files and protobuf encoding to deliver fast, reliable persistence on Android, iOS, macOS, Windows, and POSIX systems.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
MMKV Overview
通用 CLI 安装命令
npx tokrepo install 4f654acd-5318-11f1-9bc6-00163e2b0d79

Introduction

MMKV is an efficient, small, and easy-to-use mobile key-value storage framework created by the WeChat team at Tencent. It relies on memory-mapped files (mmap) for persistence and Protocol Buffers for serialization, achieving read and write speeds that far exceed SharedPreferences and NSUserDefaults.

What MMKV Does

  • Provides persistent key-value storage using memory-mapped files for near-instant writes
  • Supports multiple data types including strings, integers, floats, byte arrays, and custom Parcelable/NSCoding objects
  • Offers multi-process concurrent read/write on Android via file locking
  • Delivers cross-platform support for Android, iOS, macOS, Windows, POSIX, and HarmonyOS
  • Enables data encryption with AES-128/CFB for sensitive values

Architecture Overview

MMKV maps a file into virtual memory using mmap, so writes go directly to the kernel page cache without explicit I/O calls. Data is encoded with Protocol Buffers in an append-only log. When the file grows beyond a threshold, MMKV performs a full rewrite to reclaim space. A CRC checksum guards against corruption from unexpected process termination.

Self-Hosting & Configuration

  • Add the dependency for your platform (CocoaPods, Gradle, pip, or CMake)
  • Initialize MMKV once at app startup with a root directory path
  • Optionally set a per-instance encryption key for AES-128 protection
  • Enable multi-process mode on Android by passing MMKV.MULTI_PROCESS_MODE
  • Tune the page-size or file-growth strategy for workloads with very large values

Key Features

  • Blazing speed: benchmarks show 1000x faster than SharedPreferences on Android
  • Tiny binary size of roughly 400 KB on mobile platforms
  • Thread-safe and multi-process-safe with granular file locks
  • Automatic data migration helpers from SharedPreferences and NSUserDefaults
  • Mature and battle-tested in WeChat, serving over a billion monthly active users

Comparison with Similar Tools

  • SharedPreferences — Android-only, synchronous XML I/O, much slower for frequent writes
  • NSUserDefaults — Apple-only, plist-backed, no encryption or multi-process safety
  • DataStore (Jetpack) — Google's modern replacement for SharedPreferences, but Android-only and coroutine-based
  • Realm — Full object database with queries, heavier footprint than a simple KV store
  • LMDB — Also mmap-based, but targets server workloads and lacks mobile-first APIs

FAQ

Q: Does MMKV handle process crashes safely? A: Yes. Because mmap writes go to the OS page cache, data survives app crashes. A CRC check detects partial writes on next load.

Q: Can I use MMKV as a general-purpose database? A: MMKV is designed for flat key-value data. For relational or document data, use a proper database like SQLite.

Q: How large can an MMKV file grow? A: There is no hard limit, but MMKV is optimized for many small values. Files over tens of MB may trigger more frequent full-rewrites.

Q: Is MMKV open source? A: Yes. MMKV is released under the BSD 3-Clause license on GitHub.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产