Skip to content

Commit

Permalink
增加 MMKV 拓展支持
Browse files Browse the repository at this point in the history
  • Loading branch information
taoweiji committed Aug 21, 2021
1 parent 4d40472 commit 016d5d8
Show file tree
Hide file tree
Showing 23 changed files with 359 additions and 25 deletions.
1 change: 1 addition & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

使用 yaml 配置文件声明键值对的字段名、类型、默认值、是否加密、分组等,通过 Gradle 插件自动生成对象类,让键值对存储的使用变成get/set形式,让代码调用更加安全、优雅。

- 支持对字段单独加密,避免敏感信息明文存储
- 支持对字段单独加密,避免敏感信息明文存储
- 通过 yaml 配置存储字段信息,配置简单易懂,一目了然;
- 支持基本类型、对象和列表数据,代替SharePreference及部分SQLite场景。
- 支持自定义文件名,实现多用户之间的数据隔离。
- 支持基本类型、对象和列表数据,代替SharePreference及部分SQLite场景;
- 支持自定义文件名,实现多用户之间的数据隔离;
- 支持自定义底层存储,提供 [MMKV拓展](#使用-mmkv-作为存储底层) 支持。

#### 推荐规范

Expand Down Expand Up @@ -177,6 +178,32 @@ KVStorage.init(this, new KVStorage.Interceptor() {



### 使用 MMKV 作为存储底层

默认是使用 SharedPreferences 作为存储底层,开发者可以自定义存储底层,比如使用 [MMKV](https://github.com/Tencent/MMKV) 作为底层存储,从而获取更高的性能。

#### 引入 mmkv 拓展

```groovy
implementation 'io.github.taoweiji.kvstorage:kvstorage-mmkv:+'
```

#### 初始化

```java
MMKV.initialize(this);
KVStorage.init(this, new KVStorage.Interceptor() {
@Override
public PreferencesAdapter createPreferencesAdapter(String fileName) {
// 返回 mmkv 适配器
return new MMKVPreferencesAdapter(fileName);
}
});
```

这样底层存储就会改成mmkv,其它用法没有任何差异。




Expand Down
6 changes: 5 additions & 1 deletion example/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import android.util.Base64
import androidx.annotation.NonNull

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kvstorage'
Expand Down Expand Up @@ -35,6 +38,7 @@ dependencies {
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation project(path: ':kvstorage')
implementation project(path: ':kvstorage-mmkv')
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand All @@ -45,4 +49,4 @@ kvstorage {
yamlFiles = "storage.yaml,fit_storage.yaml" // 配置文件,多个配置文件用","分割
packageName = "com.taoweiji.kvstorage.example" // 生成代码的包名
outputDir = "src/main/java" // 代码生成的目录
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.taoweiji.kvstorage.KVStorage;
import com.taoweiji.kvstorage.PreferencesAdapter;
import com.taoweiji.kvstorage.mmkv.MMKVPreferencesAdapter;
import com.tencent.mmkv.MMKV;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand All @@ -18,6 +21,7 @@ public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
MMKV.initialize(this);
KVStorage.init(this, new KVStorage.Interceptor() {
@NonNull
@Override
Expand All @@ -37,6 +41,12 @@ public String decryption(@NonNull String data) {
public String encryption(@NonNull String data) {
return Base64.encodeToString(data.getBytes(), Base64.DEFAULT);
}

@Override
public PreferencesAdapter createPreferencesAdapter(String fileName) {
return new MMKVPreferencesAdapter(fileName);
}
});

}
}
1 change: 1 addition & 0 deletions kvstorage-mmkv/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
39 changes: 39 additions & 0 deletions kvstorage-mmkv/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
plugins {
id 'com.android.library'
}

android {
compileSdkVersion 30

defaultConfig {
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation project(path: ':kvstorage')
api 'com.tencent:mmkv-static:1.2.10'
}
apply from: '../maven_public.gradle'
Empty file.
21 changes: 21 additions & 0 deletions kvstorage-mmkv/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.taoweiji.kvstorage.mmkv;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.taoweiji.kvstorage.mmkv.test", appContext.getPackageName());
}
}
5 changes: 5 additions & 0 deletions kvstorage-mmkv/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.taoweiji.kvstorage.mmkv">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.taoweiji.kvstorage.mmkv;

import com.taoweiji.kvstorage.PreferencesAdapter;
import com.tencent.mmkv.MMKV;

public class MMKVPreferencesAdapter implements PreferencesAdapter {
MMKV preferences;

public MMKVPreferencesAdapter(String fileName) {
preferences = MMKV.mmkvWithID(fileName);
}

@Override
public float getFloat(String name, float def) {
return preferences.getFloat(name, def);
}

@Override
public boolean getBoolean(String name, boolean def) {
return preferences.getBoolean(name, def);
}

@Override
public long getLong(String name, long def) {
return preferences.getLong(name, def);
}

@Override
public int getInt(String name, int def) {
return preferences.getInt(name, def);
}

@Override
public String getString(String name, String def) {
return preferences.getString(name, def);
}

@Override
public void putString(String name, String value) {
preferences.edit().putString(name, value).apply();
}

@Override
public void putBoolean(String name, boolean value) {
preferences.edit().putBoolean(name, value).apply();
}

@Override
public void putFloat(String name, float value) {
preferences.edit().putFloat(name, (float) value).apply();
}

@Override
public void putLong(String name, long value) {
preferences.edit().putLong(name, value).apply();
}

@Override
public void putInt(String name, int value) {
preferences.edit().putInt(name, value).apply();
}

@Override
public void remove(String name) {
preferences.edit().remove(name).apply();
}

@Override
public void clear() {
preferences.edit().clear().apply();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.taoweiji.kvstorage.mmkv;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}
2 changes: 1 addition & 1 deletion kvstorage/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'maven'
android {
compileSdkVersion 30
defaultConfig {
minSdkVersion 19
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.taoweiji.kvstorage;

import android.content.SharedPreferences;
import android.text.TextUtils;

public class EncryptMetadata extends Metadata {

protected EncryptMetadata(SharedPreferences preferences, String name) {
protected EncryptMetadata(PreferencesAdapter preferences, String name) {
super(preferences, name, false);
}

private void setEncryptData(Object value) {
if (value == null) {
preferences.edit().remove(name).apply();
preferences.remove(name);
return;
}
String data = value.toString();
String after = KVStorage.encryption(data);
preferences.edit().putString(name, after).apply();
preferences.putString(name, after);
}

private String getDecryption() {
Expand Down
8 changes: 3 additions & 5 deletions kvstorage/src/main/java/com/taoweiji/kvstorage/GroupData.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.taoweiji.kvstorage;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.util.LruCache;

public class GroupData {
private final SharedPreferences preferences;
private final PreferencesAdapter preferences;
final String name;
private final LruCache<String, Metadata> children = new LruCache<>(100);
private final LruCache<String, ListMetadata> listMetadataLruCache = new LruCache<>(100);
Expand All @@ -16,7 +14,7 @@ public class GroupData {
GroupData(String name,String fileName) {
this.name = name;
this.fileName = fileName;
preferences = KVStorage.getContext().getSharedPreferences(fileName, Context.MODE_PRIVATE);
preferences = KVStorage.createPreferencesAdapter(fileName);
}

protected Metadata get(String key, boolean encrypt) {
Expand Down Expand Up @@ -48,6 +46,6 @@ public ObjectMetadata getObjectMetadata(String key, boolean encrypt) {

public void clear() {
Log.e("KVStorage", name + " clear");
preferences.edit().clear().apply();
preferences.clear();
}
}
Loading

0 comments on commit 016d5d8

Please sign in to comment.