Play with Android (mmkv storage device)

Time:2022-4-20

Reason for use: the multi-threaded value of shared preferences is invalid. A brother Xiong recommended using Xiangpiao mmkv storage sharp weapon, which won’t lose money. However, I played with the mentality of losing money. It’s really fragrant!!

Play with Android (mmkv storage device)

Mmkv advantages

  • Using one of the zero copy technologiesmmapThe key value component of memory mapping can share the data of kernel space in user space and reduce the number of copies from kernel space to user space
  • Use the best performanceprotobufagreement
  • Stable, first used in IOS, and later Android can also use this really sharp weapon
  • The performance is much better than SharedPreferences

    Play with Android (mmkv storage device)

    True aroma performance

Then start playing~~~

Add dependency

build.gradleIn addition to dependency, the specific version can be accessed from the GitHub project website:https://github.com/Tencent/MMKV

dependencies {
    implementation 'com.tencent:mmkv-static:1.2.8'
}

Initialize mmkv

Mmkv can be initialized in oncreate

@Override
public void onCreate() {
    super.onCreate();
    MMKV.initialize(this);
}

Really fragrant use

import com.tencent.mmkv.MMKV;

//Create default instance
MMKV kv = MMKV.defaultMMKV();

//Create your own instance
//Param1: Library Key
//Param2: library mode (multi process or single process)
MMKV kv2 = MMKV.mmkvWithID("test", MMKV.MULTI_PROCESS_MODE);

//Set value Boolean value
kv.encode("bool", true);
//Get value Boolean value
boolean bValue = kv.decodeBool("bool");

//Set value shaping
kv.encode("int", Integer.MIN_VALUE);
//Get value shaping
int iValue = kv.decodeInt("int");

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

//Set value float
kv.encode("float",30f);
//Get value float
float floatValue = kv.decodeFloat("float");

//Set value bytes
kv.encode("bytes", bytes);
//Get value bytes
byte[] b = kv.decodeBytes("bytes");

//Set value double
kv.encode("double", Double.MIN_VALUE);
//Get value double
Double d = kv.decodeDouble("double");

//Set value long
kv.encode("long", Long.MAX_VALUE);
//Get value long
Long l = kv.decodeLong("long");

//Set value set
kv.encode("set", new HashSet<String>(2) {{
    add("1");
    add("2");
}});
//Get value set set
Set<String> set =  kv.decodeStringSet("1");

//Get all keys
String[] strArray = kv.allKeys();

//Get total number of KV
long lo = kv.totalSize();

//Whether there is a key
boolean hasBool = kv.containsKey("bool");

//Delete single
kv.removeValueForKey("test");

//Delete multiple
kv.removeValuesForKeys(new String[]{"test1", "test2"});

//Delete the cached value, and subsequent calls will load all key values from disk
kv.clearMemoryCache();

//Delete all data
//The underlying layer calls clearal()
kv.clearAll();

//Delete all data
//Delete all key values loaded on disk
kv.clearAll();

Please jump to this address in IOS:https://www.jianshu.com/p/57734e33da8d

Recommended Today

Flutter synthetic games

Flutter synthetic gamesGitHub source address:https://github.com/CZXBigBrother/animals-merge-demo init.gif To realize the function, we need to understand the two shutter controls draggable and dragtarget It can be seen from the literal meaning that draggable can be dragged, and dragtarget is the drag target, which is to receive the control of draggable ddd.png We can only receive controls that […]