Library uses file-based mmap to store key-values

Related tags

Command-line rust mmkv
Overview

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:

use mmkv::MMKV;

fn main() {
    // initialize it with a directory, 
    // the library will crate a file,
    // named "mini_mmkv" under this dir
    MMKV::initialize(".");
    MMKV::put_i32("key1", 1);
    // Ok(1)
    println!("{:?}", MMKV::get_i32("key1"));
    MMKV::put_str("key1", "value");
    // Err(Error::KeyNotFound), cause "key1" was override by put_str
    println!("{:?}", MMKV::get_i32("key1"));
    // Ok("value")
    println!("{:?}", MMKV::get_str("key1"));
    MMKV::put_bool("key1", true);
    // Ok(true)
    println!("{:?}", MMKV::get_bool("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::clear_data();
}

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:

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:

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("first_key", "first value")
        MMKV.putInt("second_key", 1024)
        MMKV.putBool("third_key", true)
        binding.string.text = MMKV.getString("first_key", "default")
        binding.integer.text = MMKV.getInt("second_key", 0).toString()
        binding.bool.text = MMKV.getBool("third_key", false).toString()
    }
}

Check the android demo for more detail.

You might also like...
🍅 A command-line tool to get and set values in toml files while preserving comments and formatting

tomato Get, set, and delete values in TOML files while preserving comments and formatting. That's it. That's the feature set. I wrote tomato to satisf

Encode and decode dynamically constructed values of arbitrary shapes to/from SCALE bytes

scale-value · This crate provides a Value type, which is a runtime representation that is compatible with scale_info::TypeDef. It somewhat analogous t

A command line tool that resembles a debugger as well as Cheat Engine, to search for values in memory
A command line tool that resembles a debugger as well as Cheat Engine, to search for values in memory

Summary This is a small command-line tool designed to peek around memory of a running Linux process. It also provides filtering mechanisms similar to

Macro to print variable(s) with values nicely (stripped from release builds)

log_macro Macro to print variable(s) with values nicely (stripped from release builds) Install cargo add log_macro Use Add this to top of file: #[mac

Use LLMs to generate strongly-typed values

Magic Instantiate Quickstart use openai_magic_instantiate::*; #[derive(MagicInstantiate)] struct Person { // Descriptions can help the LLM unders

Microsoft Excel (XLSX) to Unicode Separated Values (USV) Rust crate

xlsx-to-usv Convert Microsoft Excel (XLSX) to Unicode Separated Values (USV). Built with the USV Rust crate. Syntax: stdin | xlsx-to-usv [options] | s

A fast bump allocator that supports allocation scopes / checkpoints. Aka an arena for values of arbitrary types.

bump-scope A fast bump allocator that supports allocation scopes / checkpoints. Aka an arena for values of arbitrary types. What is bump allocation? A

Single File Assets is a file storage format for images

SFA (Rust) Single File Assets is a file storage format for images. The packed images are not guaranteed to be of same format because the format while

A command-line tool aiming to upload the local image used in your markdown file to the GitHub repo and replace the local file path with the returned URL.
A command-line tool aiming to upload the local image used in your markdown file to the GitHub repo and replace the local file path with the returned URL.

Pup A command line tool aiming to upload the local image used in your markdown file to the GitHub repo and replace the local file path with the return

Releases(v0.1.9)
  • v0.1.9(Aug 11, 2023)

    • Add log support
    • Add Android log support
    • Bug fix
    dependencies {
        implementation("net.yangkx:mmkv:0.1.9.1")
        // Or another one with encryption feature
        // implementation("net.yangkx:mmkv-encrypt:0.1.9.1")
    }
    

    Crate address: https://crates.io/crates/mmkv

    Source code(tar.gz)
    Source code(zip)
  • v0.1.8(Aug 9, 2023)

    • Allow initialize multiple times
    • Add API clear_data
    • Add API close
    dependencies {
        implementation("net.yangkx:mmkv:0.1.8")
        // Or another one with encryption feature
        // implementation("net.yangkx:mmkv-encrypt:0.1.8")
    }
    

    Crate address: https://crates.io/crates/mmkv

    Source code(tar.gz)
    Source code(zip)
  • v0.1.7(Aug 8, 2023)

    Add Android support.

    MMKV has been packaged as android aar lib and uploaded to the maven repository. Import it in gradle directly like this:

    dependencies {
        implementation("net.yangkx:mmkv:0.1.7")
        // Or another one with encryption feature
        // implementation("net.yangkx:mmkv-encrypt:0.1.7")
    }
    

    Crate address: https://crates.io/crates/mmkv

    Source code(tar.gz)
    Source code(zip)
  • v0.1.6(Aug 7, 2023)

  • v0.1.5(Aug 7, 2023)

  • v0.1.4(Aug 7, 2023)

  • v0.1.3(Aug 7, 2023)

  • v0.1.2(Aug 6, 2023)

  • v0.1.1(Aug 4, 2023)

  • v0.1.0(Aug 3, 2023)

Rust TUI library - Clipping region is a set of min/max x/y values applied to the existing region

TinyBit Clipping region is a set of min/max x/y values applied to the existing region A TUI lib This is not yet production ready T O D O TODO: bugs: T

Togglebit 13 May 3, 2022
FileSorterX is an automatic file sorting application that sorts your files into folders based on their file extension

FileSorterX is an automatic file sorting application that sorts your files into folders based on their file extension. With FileSorterX, you can easily keep your files organized and find what you need quickly.

Xanthus 22 Apr 4, 2023
A user-friendly TUI for secure file transfers, with arrow-key and VIM-style navigation

gsftp SFTP with an interactive text-based user interface (TUI). Transfer files through an encrypted connection with a visual interface, so you can see

Ben Jiron 3 Jul 7, 2022
List key patterns of a JSON file for jq.

jqk jqk lists key patterns of a JSON file for jq. Why? jq is a useful command line tool to filter values from a JSON file quickly on a terminal; howev

Kentaro Wada 8 Jun 25, 2023
Conference Monitoring Project based on Image Recognition that uses Rust Language and AWS Rekognition service to get the level of image similarity.

Conference Monitoring System based on Image Recognition in Rust This is a Conference Monitoring Project based on Image Recognition that uses Rust Lang

Pankaj Chaudhary 6 Dec 18, 2022
Padding/aligning values without heap allocation

zero-copy-pads Padding/aligning values without heap allocation. Cargo Features std (default feature): Disable #![no_std]. Enable features that require

Khải 5 Nov 29, 2021
Eventually consistent values for Rust

Eventuals give you the most up-to-date snapshots of some value. They are like Futures that update over time, continually resolving to an eventually co

Edge & Node 110 Dec 31, 2022
Generic extensions for tapping values in Rust.

tap Suffix-Position Pipeline Behavior This crate provides extension methods on all types that allow transparent, temporary, inspection/mutation (tappi

Alexander Payne 213 Dec 30, 2022
Quickly save and retrieve values for shell scripts.

Quickly save and retrieve values for shell scripts.

Alex Andrade 2 Dec 15, 2022
a crate to swap values between possibly-overlapping references

omniswap: a crate to swap values between possibly-overlapping references Motivating Example You cannot simply use std::mem::swap to replace values wit

Masaki Hara 21 Nov 30, 2022