MMKV-storage framework in Android

1. mmkv usage operation code

The main point is that you only need to know how to operate 1, 2, 3, and 4 in the first chapter, but you don’t need to understand the following ones.

1. First, you need to introduce the mmkv package and add the following content to build.gradle:

implementation 'com.tencent:mmkv-static:1.2.7'

2. Then you need to add initialization content in the custom Application: (put it in the onCreate method)

MMKV.initialize(this);

3. Custom mmkv object

MMKV mmkv2 = MMKV.mmkvWithID("id");

MMKV supports single process by default. If the business requires multi-process access, you need to add the multi-process mode parameter during initialization: MMKV.MULTI_PROCESS_MODE

MMKV mmkv3 = MMKV.mmkvWithID("myId",MMKV.MULTI_PROCESS_MODE);//多进程同步支持

4.Storage method

//1.添加或者更新数据
//可以通过 encode() 方式存储数据也可以使用和 SharedPreferences 相同的 put() 方式存储数据;
kv.encode("name", "阿策小和尚");
kv.putInt("sex", 0);
 
//2.获取数据,同样可以采用 decodeXXX() 或 getXXX() 获取数据;
//获取boolean类型数据
boolean bValue = kv.decodeBool("bool");
kv.getString("address", "");
 
//3.删除数据,移除指定的key
kv.remove("bool");//remove函数调用removeValueForKey方法
kv.removeValueForKey("bool");
System.out.println("bool: " + kv.decodeBool("bool"));
// 移除一组key
kv.removeValuesForKeys(new String[]{
    
    "int", "long"});
String s1=Arrays.toString(kv.allKeys());
//kv.allKeys()方法获取数组返回所有的key,Arrays.toString方法把数组内容打印出来
//查询是否有这个键值对
boolean hasBool = kv.containsKey("bool");
 
//4.与SharedPreferences 一样,remove() 清除一条数据,clear() 清空全部数据;
kv.clear();

If you need to access the object, you can use the method of accessing the json string of the object to convert the object into json and store it, and then take out the json and convert it back to the object.
Code from actual Android project:

String spInfo = GISApplication.MMKVStorage.getString(KEY_SURVEY_POINT_DTO, "");
GISApplication.MMKVStorage.putString(GlobalConstant.KEY_SURVEY_POINT_DTO, "");
//GISApplication是安卓项目中的自己创建的Application,MMKVStorage是定义的一个MMKV类型的变量

5. File storage location

MMKV stores files in the $(FilesDir)/mmkv/ directory by default. You can customize the root directory when MMKV is initialized:

String dir = getFilesDir().getAbsolutePath() + "/mmkv";
String rootDir = MMKV.initialize(dir);

6.mmkv can also migrate SP data out

MMKV can call the importFromSharedPreferences method to migrate SP data. The sample code is as follows:
MMKV implements SharedPreferences and Editor interfaces, so the SP operation code does not need to be changed after migration.

MMKV kv = MMKV.mmkvWithID("myData");
SharedPreferences olderData = App.getInstance().getSharedPreferences("myData", MODE_PRIVATE);
kv.importFromSharedPreferences(olderData);
olderData.edit().clear().apply();    //删除其中的所有数据

二.SharedPreferences

1. Let’s talk about SharedPreferences first. Data is saved in XML files in the form of key/value (key-value pairs), but some cannot cross processes. So we won’t use it here, and use mmkv for storage. We will focus on mmkv later.

3. Detailed explanation of mmkv

1.mmkv introduction

MMKV respectively represents Memory Mapping Key Value, which is a key-value component based on mmap memory mapping. The underlying serialization/deserialization is implemented using protobuf. It has high performance, strong stability, and can be well replaced. SP storage. It is a lightweight storage framework currently used by WeChat; it is open sourced on multiple platforms including Android / macOS / Win32 / POSIX;

2.mmkv advantages

(1) Data format and update range optimization: SharedPreferences uses xml data storage, and each read and write operation will be globally updated; mmkv uses protobuf data storage, encoding/decoding values, which is more compact and supports local updates.

(2) Optimization of time-consuming file operations: mmkv uses mmap to maintain memory synchronization with files, uses MMap memory mapping to replace I/O operations, and uses 0-copy technology to increase update speed and achieve optimal performance.

(3) Multi-process concurrency: MMKV supports concurrent read and write access between processes.

(4) Easy to use: You can use mmkv at any time. All changes are saved immediately, no synchronization is required, and no apply calls are required.

(5) Small. A handful of files: MMKV contains process locks, encoding/decoding helpers, mmap logic, and more. Very neat. Approximately 60K binary size: MMKV adds approximately 60K to the application size on each architecture, and much less when compressed (apk).

(6) Cross-process status synchronization: SharedPreferences does not support cross-process status synchronization for thread safety; MMKV implements cross-process status updates through CRC check and file lock flock;

(7) Advantages and requirements compared to alternative SP: data encryption, multi-process sharing, anonymous memory, higher efficiency.

3.MMKV principle

(1) Memory preparation: Through the mmap memory mapping file, a memory block is provided that can be written at any time. The App only writes data into it, and the operating system is responsible for writing the memory back to the file. There is no need to worry about data loss due to crash.
(2) Data organization: We use the protobuf protocol for data serialization, and pb has good performance in terms of performance and space usage.
(3) Write optimization: Considering that the main usage scenario is frequent write updates, we need the ability to incrementally update. We consider serializing the incremental kv object and appending it to the end of the memory.
(4) Space growth: Using append to implement incremental updates brings a new problem, that is, if append is continued, the file size will grow uncontrollably. We need to make a compromise between performance and space.

4.Memory Mapping memory mapping

Memory Mapping, referred to as mmap, is a method of memory mapping files, which maps part or the entire file or other objects on the disk to the address space of the process (application), realizing the mapping of the file disk address and a virtual address in the process virtual address space. One-to-one correspondence, so that applications can access disk files by accessing memory;

Insert image description here
It can be seen that the advantages of mmap are obvious. Because of memory mapping, the process can use pointers to read and write operating memory. The system will automatically write back dirty pages to the corresponding file disk. Operating memory is equivalent to operating files. There is no need to start a new thread, and modifications to this area in the kernel space are directly reflected in the user space, thus enabling file sharing between different processes. Compared with I/O, file read and write operations only require one data copy process from disk to user main memory, which reduces the number of data copies and improves file operation efficiency; at the same time, mmap only needs to provide a section of memory and only needs Just focus on reading and writing operations to the memory file. When the operating system runs out of memory or the process exits, it is automatically written to the file. The operating system is responsible for writing the memory back to the file. There is no need to worry about data loss caused by crashes.

Of course, mmap also has its own disadvantages, because mmap needs to provide a memory block of one degree of length. The default length of its mapping area is one page, that is, 4kb. When the stored file content is small, it may cause a waste of space;

4.Protocol Buffers encoding structure

Protocol Buffers, or protobuf for short, is an extensible encoding format for serialized data produced by Google. It is a protocol developed by Google that allows serialization and deserialization of structured data. It is not just a message format, it Or a set of rules and tools for defining and exchanging these messages. Mainly used for communication protocols and data storage; after using the varint principle (a variable-length encoding method, the smaller the value, the fewer bytes used) to compress the data, the binary data is very compact;

Google developed it to provide a better way than XML to communicate between systems. This protocol even surpasses JSON, with better performance, better maintainability, and smaller size.

protobuf adopts the TLV (TAG-Length-Value) encoding format, which reduces the use of delimiters and makes the encoding more compact;
Insert image description here
when protobuf updates files, although it is not convenient for partial updates, it can be incrementally updated, that is, regardless of the previous Whether there is the same key, once new data is added to the end of the file, when the final file is read, the new data will overwrite the old data;

When adding new data, the file size is not enough and needs to be updated in full. At this time, the data in the Map needs to be serialized according to the MMKV method, filtered and saved to the required number of bytes, and compared with the file size based on the obtained number of bytes; If the saved file size allows adding new data, add it directly at the end. If the saved file size is still not enough to add new data, protobuf * 2 needs to be expanded at this time;

protobuf has simple functions, and as binary storage, its readability is poor; at the same time, it cannot represent complex concepts, and its versatility is poorer than xml; this is also the shortcoming of protobuf;

5.flock file lock + CRC check

SharedPreferences does not support data updates in multiple processes due to thread safety; MMKV supports multi-process read and write operations through flock file locks and CRC verification;

Xiaocai simply understands that MMKV updated the data in process A. When obtaining the current data in process B, it will first check whether the file has been updated through the CRC file. If it has not been updated, it will be read directly. If it has been updated, the file content will be re-obtained. Reading in progress;

In order to prevent multiple processes from writing to the file at the same time, MMKV uses the file lock flock method to ensure that only one process writes to the file at the same time;

File lock has the advantage of being naturally robust, but the disadvantage is that it does not support recursive locking, nor does it support read-write lock upgrade/downgrade, so it needs to be implemented by yourself.

Guess you like

Origin blog.csdn.net/zxz_zxz_zxz/article/details/130734095