android 腾讯MMKV持久化框架使用

MMKV是腾讯18年底推出的一套持久化框架,有安卓,IOS,PC版本等等,微信的持久化功能使用的就是MMKV,项目地址:https://github.com/Tencent/MMKV

最大的特点就是高效,号称要比传统的持久化工具要高效100倍,目标是用来替代原生的SharedPreferences(后续SharedPreferences统称为SP)。本文主要是探究MMKV的实现原理以及为什么比SP高效。

在app下面的build.gradle中添加引用

//腾讯MMKV
implementation 'com.tencent:mmkv:1.2.11'

在MyApplication 中进行初始化

package com.nywnn.test;

import android.app.Application;

import com.tencent.mmkv.MMKV;

public class MyApplication  extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //MMKV初始化
        String initialize = MMKV.initialize(this);
        System.out.println("MMKV INIT " + initialize);
        //工具类初始化
        MVUtils.getInstance();
    }
}

封装一个MVUtils 工具类如下

package com.nywnn.test;

import android.os.Parcelable;

import com.tencent.mmkv.MMKV;

import java.util.Collections;
import java.util.Set;

public class MVUtils {
    private static MVUtils mInstance;
    private static MMKV mmkv;

    public MVUtils() {
        mmkv = MMKV.defaultMMKV();
    }

    public static MVUtils getInstance() {
        if (mInstance == null) {
            synchronized (MVUtils.class) {
                if (mInstance == null) {
                    mInstance = new MVUtils();
                }
            }
        }
        return mInstance;
    }

    /**
     * 写入基本数据类型缓存
     *
     * @param key    键
     * @param object 值
     */
    public static void put(String key, Object object) {
        if (object instanceof String) {
            mmkv.encode(key, (String) object);
        } else if (object instanceof Integer) {
            mmkv.encode(key, (Integer) object);
        } else if (object instanceof Boolean) {
            mmkv.encode(key, (Boolean) object);
        } else if (object instanceof Float) {
            mmkv.encode(key, (Float) object);
        } else if (object instanceof Long) {
            mmkv.encode(key, (Long) object);
        } else if (object instanceof Double) {
            mmkv.encode(key, (Double) object);
        } else if (object instanceof byte[]) {
            mmkv.encode(key, (byte[]) object);
        } else {
            mmkv.encode(key, object.toString());
        }
    }

    public static void putSet(String key, Set sets) {
        mmkv.encode(key, sets);
    }

    public static void putParcelable(String key, Parcelable obj) {
        mmkv.encode(key, obj);
    }

    public static Integer getInt(String key) {
        return mmkv.decodeInt(key, 0);
    }

    public static Integer getInt(String key, int defaultValue) {
        return mmkv.decodeInt(key, defaultValue);
    }

    public static Double getDouble(String key) {
        return mmkv.decodeDouble(key, 0.00);
    }

    public static Double getDouble(String key, double defaultValue) {
        return mmkv.decodeDouble(key, defaultValue);
    }

    public static Long getLong(String key) {
        return mmkv.decodeLong(key, 0L);
    }

    public static Long getLong(String key, long defaultValue) {
        return mmkv.decodeLong(key, defaultValue);
    }

    public static Boolean getBoolean(String key) {
        return mmkv.decodeBool(key, false);
    }

    public static Boolean getBoolean(String key, boolean defaultValue) {
        return mmkv.decodeBool(key, defaultValue);
    }

    public static Float getFloat(String key) {
        return mmkv.decodeFloat(key, 0F);
    }

    public static Float getFloat(String key, float defaultValue) {
        return mmkv.decodeFloat(key, defaultValue);
    }

    public static byte[] getBytes(String key) {
        return mmkv.decodeBytes(key);
    }

    public static byte[] getBytes(String key, byte[] defaultValue) {
        return mmkv.decodeBytes(key, defaultValue);
    }

    public static String getString(String key) {
        return mmkv.decodeString(key, "");
    }

    public static String getString(String key, String defaultValue) {
        return mmkv.decodeString(key, defaultValue);
    }

    public static Set getStringSet(String key) {
        return mmkv.decodeStringSet(key, Collections.emptySet());
    }

    public static Parcelable getParcelable(String key) {
        return mmkv.decodeParcelable(key, null);
    }

    /**
     * 移除某个key对
     *
     * @param key
     */
    public static void removeKey(String key) {
        mmkv.removeValueForKey(key);
    }

    /**
     * 清除所有key
     */
    public static void clearAll() {
        mmkv.clearAll();
    }

}

在activity里使用如下

package com.nywnn.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //存
        Log.d("TAG", "onCreate: 存");
        MVUtils.put("age",24);
        //取
        int age = MVUtils.getInt("age",0);
        Log.d("TAG", "onCreate: 取 :" + age);

    }
}

 

 

你可能感兴趣的:(android)