0简述
简述为ASimpleCache官网(
点击打开链接)对其介绍,ASimpleCache 是一个为android制定的 轻量级的 开源缓存框架。轻量到只有一个java文件(由十几个类精简而来)。
0.1ASimpleCache可以缓存什么
普通的字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象,和 byte数据。
0.2ASimpleCache有什么特色
(1)轻:轻到只有一个JAVA文件;
(2)可配置:可以配置缓存路径,缓存大小,缓存数量等;
(3)可以设置缓存超时时间,缓存超时自动失效,并被删除;
(4)支持多进程。
(4)支持多进程。
0.3ASimpleCache在Android中的使用场景
(1)替换SharePreference当做配置文件;
(2)可以缓存网络请求数据,可以作为app数据(
字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象,和 byte数据)的缓存工具;
(3)可以作为预加载数据的缓存库;
1ASimpleCache的使用
ASimpleCache使用起来太简单了,尤其是使用过SharePreference。
1.1缓存字符串
首先,创建
ASimpleCache对象:
//缓存
private ACache mACache;
mACache = ACache.get(this);
其次,存储数据:
mACache.put(Constants.KEY_STRING, result);
还有,获取缓存数据:
String result = mACache.getAsString(Constants.KEY_STRING);
最后,清除缓存:
mACache.remove(Constants.KEY_STRING);
1.2缓存JsonObject
首先,创建ASimpleCache对象:
//缓存
private ACache mACache;
mACache = ACache.get(this);
其次,存储数据:
mACache.put(Constants.KEY_JSONOBJECT, jsonObject);
还有,获取缓存数据:
JSONObject result = mACache.getAsJSONObject(Constants.KEY_JSONOBJECT);
最后,清除缓存:
mACache.remove(Constants.KEY_JSONOBJECT);
1.3缓存JsonArray
首先,创建ASimpleCache对象:
//缓存
private ACache mACache;
mACache = ACache.get(this);
其次,存储数据:
mACache.put(Constants.KEY_JSONARRAY, jsonArray);
还有,获取缓存数据:
JSONArray result = mACache.getAsJSONArray(Constants.KEY_JSONARRAY);
最后,清除缓存:
mACache.remove(Constants.KEY_JSONARRAY);
1.4缓存Bitmap
首先,创建ASimpleCache对象:
//缓存
private ACache mACache;
mACache = ACache.get(this);
其次,存储数据:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
mACache.put(Constants.KEY_BITMAP, bitmap);
还有,获取缓存数据:
Bitmap bitmap = mACache.getAsBitmap(Constants.KEY_BITMAP);
最后,清除缓存:
mACache.remove(Constants.KEY_BITMAP);
1.5缓存Drawable
首先,创建ASimpleCache对象:
//缓存
private ACache mACache;
mACache = ACache.get(this);
其次,存储数据:
Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
mACache.put(Constants.KEY_DRAWABLE, drawable);
还有,获取缓存数据:
Drawable bitmap = mACache.getAsDrawable(Constants.KEY_DRAWABLE);
最后,清除缓存:
mACache.remove(Constants.KEY_DRAWABLE);
1.6缓存序列化的java对象
首先,创建ASimpleCache对象:
//缓存
private ACache mACache;
mACache = ACache.get(this);
其次,存储数据:
mACache.put(Constants.KEY_JAVA_BEAN, mWeather);
还有,获取缓存数据:
Weather result = (Weather) mACache.getAsObject(Constants.KEY_JAVA_BEAN);
最后,清除缓存:
mACache.remove(Constants.KEY_JAVA_BEAN);
1.7缓存byte数据
首先,创建ASimpleCache对象:
private ACache mACache;
mACache = ACache.get(this);
其次,存储数据:
private void saveByte() {
OutputStream ostream = null;
try {
ostream = mACache.put(Constants.KEY_BYTE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (ostream == null) {
ToastUtil.simpleToast(this, "Open stream error!");
return;
}
try {
URL u = new URL(Constants.downloadUrl);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.connect();
InputStream stream = conn.getInputStream();
byte[] buff = new byte[1024];
int counter;
while ((counter = stream.read(buff)) > 0) {
ostream.write(buff, 0, counter);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// cache update
ostream.close();
} catch (IOException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
showTv.setText("done...");
}
});
}
}
还有,获取缓存数据:
private void getsByte() {
InputStream stream = null;
try {
stream = mACache.get(Constants.KEY_BYTE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (stream == null) {
ToastUtil.simpleToast(this, "Bitmap cache is null ...");
showTv.setText("file not found");
return;
}
try {
showTv.setText("file size: " + stream.available());
} catch (IOException e) {
showTv.setText("error " + e.getMessage());
}
}
最后,清除缓存:
mACache.remove(Constants.KEY_BYTE);
2总结
对于一个框架的学习这还没有完哦!接下来我将会从以下方面进行撰写博客:
(1)结合项目来玩ASimpleCache;
(2)分析ASimpleCache源码;
(3)如果要我设计一个缓存框架,我会怎么玩?