1.ASimpleCache官网对其介绍,ASimpleCache 是一个为android制定的 轻量级的 开源缓存框架。轻量到只有一个java文件(由十几个类精简而来)。
2ASimpleCache可以用于普通的字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象,和 byte数据。
2ASimpleCache可以用于普通的字符串
(1)轻:轻到只有一个JAVA文件;
(2)可配置:可以配置缓存路径,缓存大小,缓存数量等;
(3)可以设置缓存超时时间,缓存超时自动失效,并被删除;
(4)支持多进程。
(4)支持多进程。
1.1缓存字符串
//缓存
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);
其次,存储数据:
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);
首先,创建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);
源码下载已经上传