每日一记之ASimpleCache缓存框架的使用方法

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

2ASimpleCache可以用于普通的字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象,和 byte数据。

2ASimpleCache可以用于普通的字符串

(1)轻:轻到只有一个JAVA文件;
(2)可配置:可以配置缓存路径,缓存大小,缓存数量等;
(3)可以设置缓存超时时间,缓存超时自动失效,并被删除;
(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);  

1.2缓存JsonObject

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); 

源码下载已经上传

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值