Crates.io PRs Welcome Crates.io Crates.io

Library uses file-based mmap to store key-values

This is a Rust version of MMKV.

By default, this lib uses CRC8 to check data integrity.

If include feature encryption, this lib will encrypt the data with AES-EAX.

MMKV is thread-safe but cannot guarantee cross-process data consistency (still under development). If you want to use it in a cross-process scenario, please ensure that there is no competing write.

How to use

Add dependency:

cargo add mmkv

And use MMKV directly: ```rust use mmkv::MMKV;

fn main() { // initialize it with a directory, // the library will crate a file, // named "minimmkv" under this dir MMKV::initialize("."); MMKV::puti32("key1", 1); // Ok(1) println!("{:?}", MMKV::geti32("key1")); MMKV::putstr("key1", "value"); // Err(Error::KeyNotFound), cause "key1" was override by putstr println!("{:?}", MMKV::geti32("key1")); // Ok("value") println!("{:?}", MMKV::getstr("key1")); MMKV::putbool("key1", true); // Ok(true) println!("{:?}", MMKV::getbool("key1")); // close the instance if you need to re-initialize with different config. // MMKV::close(); // clear all related data to free disk space, this call will also close the instance. // MMKV::cleardata(); } ```

Use with encryption feature

Add dependency:

cargo add mmkv --features encryption

Then init MMKV with an encryption credential:

MMKV::initialize(".", "88C51C536176AD8A8EE4A06F62EE897E")

Encryption will greatly reduce the efficiency of reading and writing, and will also increase the file size, use at your own risk!

Use in Android projects

Add lib dependency to gradle: kotlin dependencies { implementation("net.yangkx:mmkv:0.1.9.1") // Or another one with encryption feature // implementation("net.yangkx:mmkv-encrypt:0.1.9.1") } Use the kotlin API: ```kotlin class MyApplication : Application() { override fun onCreate() { super.onCreate() val dir = this.getDir("mmkv", Context.MODE_PRIVATE) MMKV.initialize(dir.absolutePath) // If you are using mmkv with encryption // MMKV.initialize(dir.absolutePath, "88C51C536176AD8A8EE4A06F62EE897E") } }

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) MMKV.putString("firstkey", "first value") MMKV.putInt("secondkey", 1024) MMKV.putBool("thirdkey", true) binding.string.text = MMKV.getString("firstkey", "default") binding.integer.text = MMKV.getInt("secondkey", 0).toString() binding.bool.text = MMKV.getBool("thirdkey", false).toString() } } ``` Check the android demo for more detail.