ASimpleCache缓存框架之简单使用

来源:互联网 发布:中国钓鱼运动协会数据 编辑:程序博客网 时间:2024/05/01 06:24

http://blog.csdn.net/dayongxin/article/details/47011683

0简述

简述为ASimpleCache官网(点击打开链接)对其介绍,ASimpleCache 是一个为android制定的 轻量级的 开源缓存框架。轻量到只有一个java文件(由十几个类精简而来)。

0.1ASimpleCache可以缓存什么

普通的字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象,和 byte数据。

0.2ASimpleCache有什么特色

(1)轻:轻到只有一个JAVA文件;
(2)可配置:可以配置缓存路径,缓存大小,缓存数量等;
(3)可以设置缓存超时时间,缓存超时自动失效,并被删除;
(4)支持多进程。

0.3ASimpleCache在Android中的使用场景

(1)替换SharePreference当做配置文件;
(2)可以缓存网络请求数据,可以作为app数据(字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象,和 byte数据)的缓存工具;
(3)可以作为预加载数据的缓存库;

1ASimpleCache的使用

ASimpleCache使用起来太简单了,尤其是使用过SharePreference。

1.1缓存字符串

首先,创建ASimpleCache对象:
[java] view plain copy
  1. //缓存  
  2. private ACache mACache;  
  3.   
  4. mACache = ACache.get(this);  
其次,存储数据:
[java] view plain copy
  1. mACache.put(Constants.KEY_STRING, result);  
还有,获取缓存数据:
[java] view plain copy
  1. String result = mACache.getAsString(Constants.KEY_STRING);  
最后,清除缓存:
[java] view plain copy
  1. mACache.remove(Constants.KEY_STRING);  

1.2缓存JsonObject

首先,创建ASimpleCache对象:
[java] view plain copy
  1. //缓存  
  2. private ACache mACache;  
  3.   
  4. mACache = ACache.get(this);  
其次,存储数据:
[java] view plain copy
  1. mACache.put(Constants.KEY_JSONOBJECT, jsonObject);  
还有,获取缓存数据:
[java] view plain copy
  1. JSONObject result = mACache.getAsJSONObject(Constants.KEY_JSONOBJECT);  
最后,清除缓存:
[java] view plain copy
  1. mACache.remove(Constants.KEY_JSONOBJECT);  

1.3缓存JsonArray

首先,创建ASimpleCache对象:
[java] view plain copy
  1. //缓存  
  2. private ACache mACache;  
  3.   
  4. mACache = ACache.get(this);  
其次,存储数据:
[java] view plain copy
  1. mACache.put(Constants.KEY_JSONARRAY, jsonArray);  
还有,获取缓存数据:
[java] view plain copy
  1. JSONArray result = mACache.getAsJSONArray(Constants.KEY_JSONARRAY);  
最后,清除缓存:
[java] view plain copy
  1. mACache.remove(Constants.KEY_JSONARRAY);  

1.4缓存Bitmap

首先,创建ASimpleCache对象:
[java] view plain copy
  1. //缓存  
  2. private ACache mACache;  
  3.   
  4. mACache = ACache.get(this);  
其次,存储数据:
[java] view plain copy
  1. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);  
  2.         mACache.put(Constants.KEY_BITMAP, bitmap);  
还有,获取缓存数据:
[java] view plain copy
  1. Bitmap bitmap = mACache.getAsBitmap(Constants.KEY_BITMAP);  
最后,清除缓存:
[java] view plain copy
  1. mACache.remove(Constants.KEY_BITMAP);  

1.5缓存Drawable

首先,创建ASimpleCache对象:
[java] view plain copy
  1. //缓存  
  2. private ACache mACache;  
  3.   
  4. mACache = ACache.get(this);  
其次,存储数据:
[java] view plain copy
  1. Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);  
  2.         mACache.put(Constants.KEY_DRAWABLE, drawable);  
还有,获取缓存数据:
[java] view plain copy
  1. Drawable bitmap = mACache.getAsDrawable(Constants.KEY_DRAWABLE);  
最后,清除缓存:
[java] view plain copy
  1. mACache.remove(Constants.KEY_DRAWABLE);  

1.6缓存序列化的java对象

首先,创建ASimpleCache对象:
[java] view plain copy
  1. //缓存  
  2. private ACache mACache;  
  3.   
  4. mACache = ACache.get(this);  
其次,存储数据:
[java] view plain copy
  1. mACache.put(Constants.KEY_JAVA_BEAN, mWeather);  
还有,获取缓存数据:
[java] view plain copy
  1. Weather result = (Weather) mACache.getAsObject(Constants.KEY_JAVA_BEAN);  
最后,清除缓存:
[java] view plain copy
  1. mACache.remove(Constants.KEY_JAVA_BEAN);  

1.7缓存byte数据

首先,创建ASimpleCache对象:
[java] view plain copy
  1. private ACache mACache;  
  2.   
  3. mACache = ACache.get(this);  
其次,存储数据:
[java] view plain copy
  1. private void saveByte() {  
  2.         OutputStream ostream = null;  
  3.         try {  
  4.             ostream = mACache.put(Constants.KEY_BYTE);  
  5.         } catch (FileNotFoundException e) {  
  6.             e.printStackTrace();  
  7.         }  
  8.         if (ostream == null) {  
  9.             ToastUtil.simpleToast(this"Open stream error!");  
  10.             return;  
  11.         }  
  12.         try {  
  13.             URL u = new URL(Constants.downloadUrl);  
  14.             HttpURLConnection conn = (HttpURLConnection) u.openConnection();  
  15.             conn.connect();  
  16.             InputStream stream = conn.getInputStream();  
  17.             byte[] buff = new byte[1024];  
  18.             int counter;  
  19.             while ((counter = stream.read(buff)) > 0) {  
  20.                 ostream.write(buff, 0, counter);  
  21.             }  
  22.         } catch (IOException e) {  
  23.             e.printStackTrace();  
  24.         } finally {  
  25.             try {  
  26.                 // cache update  
  27.                 ostream.close();  
  28.             } catch (IOException e) {  
  29.                 e.printStackTrace();  
  30.             }  
  31.             runOnUiThread(new Runnable() {  
  32.                 @Override  
  33.                 public void run() {  
  34.                     showTv.setText("done...");  
  35.                 }  
  36.             });  
  37.         }  
  38.     }  
还有,获取缓存数据:
[java] view plain copy
  1. private void getsByte() {  
  2.         InputStream stream = null;  
  3.         try {  
  4.             stream = mACache.get(Constants.KEY_BYTE);  
  5.         } catch (FileNotFoundException e) {  
  6.             e.printStackTrace();  
  7.         }  
  8.         if (stream == null) {  
  9.             ToastUtil.simpleToast(this"Bitmap cache is null ...");  
  10.             showTv.setText("file not found");  
  11.             return;  
  12.         }  
  13.         try {  
  14.             showTv.setText("file size: " + stream.available());  
  15.         } catch (IOException e) {  
  16.             showTv.setText("error " + e.getMessage());  
  17.         }  
  18.     }  
最后,清除缓存:
[java] view plain copy
  1. mACache.remove(Constants.KEY_BYTE);  
原创粉丝点击