Skip to content

Commit

Permalink
增加List类型的缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
jialiang.ren committed Aug 21, 2017
1 parent 7fe0300 commit 1cfec23
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ allprojects {

```
// 添加编译
compile 'com.github.rjlatgithub:JsonCache:v1.0.1'
compile 'com.github.rjlatgithub:JsonCache:v1.2.0'
```

# Configuration
Expand Down
47 changes: 47 additions & 0 deletions app/src/main/java/com/makeunion/jsoncache/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import com.makeunion.jsoncachelib.api.JsonCache;
import com.makeunion.jsoncachelib.callback.ICallback;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

@Override
Expand Down Expand Up @@ -45,6 +48,38 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
}

// ========================== List ==========================

public void saveList(View view) {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
JsonCache.getInstance().saveList("list", list);
}

public void loadList(View view) {
List<String> list = JsonCache.getInstance().loadList("list", String.class);
printList(list);
}

public void saveListAsync(View view) {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
JsonCache.getInstance().saveListAsync("list", list);
}

public void loadListAsync(View view) {
JsonCache.getInstance().loadListAsync("list", String.class, new ICallback<List<String>>() {
@Override
public void onResult(List<String> result) {
printList(result);
}
});
}

// ========================== Object ==========================

public void saveObject(View view) {
Expand Down Expand Up @@ -177,4 +212,16 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
}
}

private void printList(List list) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (Object o : list) {
sb.append(o.toString());
sb.append(",");
}
sb.deleteCharAt(sb.length() - 1);
sb.append("]");
Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
}
}
43 changes: 43 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,49 @@
android:layout_marginRight="16dp"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="16dp"
android:padding="8dp"
android:background="#30f00000">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:textSize="16sp"
android:textColor="#353535"
android:text="List"
android:textStyle="bold"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="同步存"
android:onClick="saveList"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="同步取"
android:onClick="loadList"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="异步存"
android:onClick="saveListAsync"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="异步取"
android:onClick="loadListAsync"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.makeunion.jsoncachelib.callback.ICallback;
import com.makeunion.jsoncachelib.threadpool.ThreadPool;

import java.util.List;

public class JsonCache {

private static JsonCache sInstance;
Expand Down Expand Up @@ -62,6 +64,58 @@ public void init(Context context, Configuration config) {
this.mDiskCache.setConfig(config);
}

/**
* 保存列表
*
* @param key key
* @param list 列表
* @param <T> 泛型
*/
public <T> void saveList(String key, List<T> list) {
mMemoryCache.saveList(key, list);
mDiskCache.saveList(key, list);
}

/**
* 异步保存列表
*
* @param key key
* @param list 列表
* @param <T> 泛型
*/
public <T> void saveListAsync(String key, List<T> list) {
mThreadPool.submitSaveList(key, list);
}

/**
* 加载列表
*
* @param key key
* @param clazz 类型
* @param <T> 泛型
*
* @return 缓存
*/
public <T> List<T> loadList(String key, Class<T> clazz) {
List<T> cache = mMemoryCache.loadList(key, clazz);
if (cache == null) {
cache = mDiskCache.loadList(key, clazz);
}
return cache;
}

/**
* 异步加载列表
*
* @param key key
* @param clazz 类型
* @param <T> 泛型
* @param callback 回调
*/
public <T> void loadListAsync(String key, Class<T> clazz, ICallback<List<T>> callback) {
mThreadPool.submitLoadList(key, clazz, callback);
}

/**
* 保存对象bean
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.makeunion.jsoncachelib.compress.CompressUtil;
import com.makeunion.jsoncachelib.util.StringUtil;

import java.util.List;


public abstract class BaseCache {

Expand All @@ -15,6 +17,10 @@ public abstract class BaseCache {

protected Configuration mConfig;

public abstract <T> void saveList(String key, List<T> list);

public abstract <T> List<T> loadList(String key, Class<T> clazz);

public abstract void saveObject(String key, Object object);

public abstract <T> T loadObject(String key, Class<T> clazz);
Expand Down Expand Up @@ -82,6 +88,18 @@ protected final <T> T parseBytesToObject(String key, byte[] bytes, Class<T> claz
return JSON.parseObject(value, clazz);
}

protected final <T> List<T> parseBytesToList(String key, byte[] bytes, Class<T> clazz) {
JSONObject jsonObject = parseJsonObject(key, bytes);
if (jsonObject == null) {
return null;
}
String value = jsonObject.getString(VALUE);
if (StringUtil.isNull(value)) {
return null;
}
return JSON.parseArray(value, clazz);
}

protected final byte[] parseStringToBytes(String value) {
return buildBytes(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,30 @@
import com.makeunion.jsoncachelib.api.Configuration;
import com.makeunion.jsoncachelib.io.FileHelper;

import java.util.List;


public class DiskCache extends BaseCache {

private FileHelper mFileHelper;

@Override
public <T> void saveList(String key, List<T> list) {
saveObject(key, list);
}

@Override
public <T> List<T> loadList(String key, Class<T> clazz) {
if (key == null) {
return null;
}
if (clazz == null) {
return null;
}
byte[] data = mFileHelper.readObject(key);
return parseBytesToList(key, data, clazz);
}

@Override
public void saveObject(String key, Object object) {
if (key == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,39 @@
import com.makeunion.jsoncachelib.log.Logger;

import java.io.UnsupportedEncodingException;
import java.util.List;


public class MemoryCache extends BaseCache {

private LruCache<String, String> mLruCache;

@Override
public <T> void saveList(String key, List<T> list) {
saveObject(key, list);
}

@Override
public <T> List<T> loadList(String key, Class<T> clazz) {
if (key == null) {
return null;
}
if (clazz == null) {
return null;
}
String loadString = mLruCache.get(key);
if (loadString == null) {
return null;
}
byte[] bytes = null;
try {
bytes = loadString.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException e) {
Logger.e("load UnsupportedEncodingException=" + e.getMessage());
}
return parseBytesToList(key, bytes, clazz);
}

@Override
public void saveObject(String key, Object object) {
if (key == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.makeunion.jsoncachelib.cache.BaseCache;
import com.makeunion.jsoncachelib.callback.ICallback;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -42,6 +43,14 @@ public ThreadPool(BaseCache memoryCache, BaseCache diskCache) {
new DefaultThreadFactory(Thread.NORM_PRIORITY, "cache-pool-d-"));
}

public <T> void submitSaveList(String key, List<T> list) {
mExecutor.execute(new SaveListTask<T>(key, list));
}

public <T> void submitLoadList(String key, Class<T> clazz, ICallback<List<T>> callback) {
mExecutor.execute(new LoadListTask<T>(key, clazz, callback));
}

public void submitSaveObject(String key, Object object) {
mExecutor.execute(new SaveObjectTask(key, object));
}
Expand Down Expand Up @@ -82,6 +91,45 @@ public void submitLoadDouble(String key, double defaultValue, ICallback<Double>
mExecutor.execute(new LoadDoubleTask(key, defaultValue, callback));
}

private class SaveListTask<T> implements Runnable {

String key;
List<T> list;

public SaveListTask(String key, List<T> list) {
this.key = key;
this.list = list;
}

@Override
public void run() {
mMemoryCache.saveList(key, list);
mDiskCache.saveList(key, list);
}
}

private class LoadListTask<T> implements Runnable {

String key;
Class<T> clazz;
ICallback<List<T>> callback;

public LoadListTask(String key, Class<T> clazz, ICallback<List<T>> callback) {
this.key = key;
this.clazz = clazz;
this.callback = callback;
}

@Override
public void run() {
List<T> cache = mMemoryCache.loadList(key, clazz);
if (cache == null) {
cache = mDiskCache.loadList(key, clazz);
}
mHandler.post(new SyncRunnable<List<T>>(cache, callback));
}
}

private class SaveObjectTask implements Runnable {

String key;
Expand Down

0 comments on commit 1cfec23

Please sign in to comment.