Android缓存机制&缓存框架——ACache

来源:互联网 发布:c语言分隔符 编辑:程序博客网 时间:2024/05/16 02:29

缓存机制出现的意义:

Android开发本质上就是手机和互联网中的web服务器之间进行通信,就必然需要从服务端获取数据,而反复通过网络获取数据是比较耗时的,特别是访问比较多的时候,会极大影响了性能,Android中可通过二级缓存来减少频繁的网络操作,减少流量、提升性能。

二级缓存:

当Android端需要获得数据时比如获取网络中的图片,我们首先从内存中查找(按键查找),内存中没有的再从磁盘文件或sqlite中去查找,若磁盘中也没有才通过网络获取;当获得来自网络的数据,就以key-value对的方式先缓存到内存(一级缓存),同时缓存到文件或sqlite中(二级缓存)。注意:内存缓存会造成堆内存泄露,所有一级缓存通常要严格控制缓存的大小,一般控制在系统内存的1/4。

本地客户端与服务器交互流程图


缓存框架—— ACache

ACache介绍:

ACache类似于SharedPreferences,但是比SharedPreferences功能更加强大,SharedPreferences只能保存一些基本数据类型、Serializable、Bundle等数据;
而Acache可以缓存如下数据:

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

它是一个为android制定的轻量级的开源缓存框架。轻量到只有一个java文件(由十几个类精简而来):

特色:

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

应用场景:

1、替换SharePreference当做配置文件
2、可以缓存网络请求数据,比如oschina的android客户端可以缓存http请求的新闻内容,缓存时间假设为1个小时,超时后自动失效,让客户端重新请求新的数据,减少客户端流量,同时减少服务器并发量

下载链接:https://github.com/yangfuhai/ASimpleCache
框架分析:http://blog.csdn.net/zhoubin1992/article/details/46379055

使用:

初始化ACache组件:

ACacheacache = ACache.get(context)      或ACacheacache=ACache.get(context,max_size,max_count)  //max_size:设置限制缓存大小,默认为50M//max_count:设置缓存数据的数量,默认不限制 

设置缓存数据:
  

//将数据同时上存入一级缓存(内存Map)和二级缓存(文件)中acache.put(key,data,time)或acache.put(key,data) /**Key:为存入缓存的数据设置唯一标识,取数据时就根据key来获得的Data:要存入的数据,acache支持的数据类型如图所示:有String、可序列化的对象、字节数组、Drawable等  Time:设置缓存数据的有效时间,单位秒*/

一个简单小demo:

ACache mCache = ACache.get(this);mCache.put("test_key1", "test value");mCache.put("test_key2", "test value", 10);//保存10秒,如果超过10秒去获取这个key,将为nullmCache.put("test_key3", "test value", 2 * ACache.TIME_DAY);//保存两天,如果超过两天去获取这个key,将为null

获取数据:

ACache mCache = ACache.get(this);String value = mCache.getAsString("test_key1");

源码分析:

ACache类结构图:

ASimpleCache里只有一个JAVA文件——ACache.java

ACache类的详细结构图

官方demo分析

以字符串存储为例(官方给的demo里给出了很多种数据读取的例子,其实方法相似),打开SaveStringActivity.java:

缓存字符串读取:
  • 在onCreate里通过get方式获取缓存实例
    mCache = ACache.get(this);

  • 在save按钮的点击事件里,通过put方法往缓存实例里保存字符串
    mCache.put(“testString”, mEt_string_input.getText().toString(),300);

  • 在read按钮的点击事件里,通过getAsString方法从缓存实例里读取字符串
    mCache.getAsString(“testString”);

其他数据读取,方法相似,也是这三个步骤。300为保存时间300秒。

package com.yangfuhai.asimplecachedemo;import org.afinal.simplecache.ACache;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;/**  * @ClassName: SaveStringActivity * @Description: 缓存string  */public class SaveStringActivity extends Activity{    private EditText mEt_string_input;    private TextView mTv_string_res;    private ACache mCache;    @Override    protected void onCreate(Bundle savedInstanceState)     {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_save_string);        // 初始化控件        initView();        mCache = ACache.get(this);    }    /**     * 初始化控件      */    private void initView()     {        mEt_string_input =(EditText)findViewById(R.id.et_string_input);        mTv_string_res = (TextView)findViewById(R.id.tv_string_res);    }    /**      * 点击save事件     */    public void save(View v)     {        if(mEt_string_input.getText().toString().trim().length() == 0)         {            Toast.makeText(                    this,                    "Cuz u input is a nullcharacter ... So , when u press \"read\" , if do not show any result , plz don't be surprise",                    Toast.LENGTH_SHORT).show();        }// mCache.put("testString", mEt_string_input.getText().toString());        mCache.put("testString", mEt_string_input.getText().toString(),300);    }    /**     * 点击read事件      */    public void read(View v)     {        String testString = mCache.getAsString("testString");        if (testString == null)         {            Toast.makeText(this, "String cache is null ...", Toast.LENGTH_SHORT)                    .show();            mTv_string_res.setText(null);            return;        }        mTv_string_res.setText(testString);    }    /**      * 点击clear事件     */    public void clear(View v)     {        mCache.remove("testString");    }}

ACache源码分析

1、获取缓存实例
ACache类的构造方法为private的,所以新建缓存实例只能通过ACache.get方式获取。

//实例化应用程序场景缓存public static ACache get(Context ctx) {   return get(ctx, "ACache");}//新建缓存目录public static ACache get(Context ctx, String cacheName) {   //新建文件夹,文件路径为应用场景缓存路径目录,文件夹名ACache(new File()也可新建文件,带上后缀即可)   File f = new File(ctx.getCacheDir(), cacheName);       return get(f, MAX_SIZE, MAX_COUNT);}//新建缓存实例,存入实例map,key为缓存目录+每次应用开启的进程idpublic static ACache get(File cacheDir, long max_zise, int max_count) {   //返回key为缓存目录+每次应用开启的进程id的map的value值,赋给缓存实例manager   ACache manager = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid());   if (manager == null)  //缓存实例为空时,   {       manager = new ACache(cacheDir, max_zise, max_count);       mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), manager);//插入map   }   return manager;}

在调用ACache.get(Context)方法过程中,其实执行了三个get方法
(1)get(Context ctx)->(2)get(Context ctx, String cacheName)->(3)get(File cacheDir, long max_zise, int max_count)
在(2)中新建了缓存目录,路径为:
/data/data/app-package-name/cache/ACache
缓存大小MAX_SIZE和数量MAX_COUNT均由final变量控制。
其实最终调用(3)获取实例:
mInstanceMap的key为缓存目录+每次应用开启的进程id,value为ACache.
初次运行,mInstanceMap没有任何键值对,所以manager == null。故通过ACache构造方法构造新实例,最后将该实例引用存入mInstanceMap。

接下来看看ACache构造方法:

 //ACache构造函数 为private私有(所以在其他类里获得实例只能通过get()方法)private ACache(File cacheDir, long max_size, int max_count) {    //缓存目录不存在并且无法创建时,抛出异常    if (!cacheDir.exists() && !cacheDir.mkdirs())     {            throw new RuntimeException("can't make dirs in " + cacheDir.getAbsolutePath());    }    //实例化ACacheManager内部类实例    mCache = new ACacheManager(cacheDir, max_size, max_count);}

缓存目录不存在并且无法创建时,抛出异常,否则实例化ACacheManager内部类实例(缓存管理器)。ACacheManager内部类的构造函数如下:

//内部类ACacheManager的构造函数private ACacheManager(File cacheDir, long sizeLimit, int countLimit){    this.cacheDir = cacheDir;    this.sizeLimit = sizeLimit;    this.countLimit = countLimit;    //原子类实例cacheSize,不用加锁保证线程安全    cacheSize = new AtomicLong();           //原子类实例cacheCount,不用加锁保证线程安全    cacheCount = new AtomicInteger();       calculateCacheSizeAndCacheCount();}

构造函数得到原子类实例cacheSize和cacheCount,通过calculateCacheSizeAndCacheCount();方法计算cacheSize和cacheCount如下:

/** * 计算 cacheSize和cacheCount  */private void calculateCacheSizeAndCacheCount() {    new Thread(new Runnable()     {        @Override        public void run()         {            //int size = 0;            long size = 0;  //这里long类型才对——by牧之丶            int count = 0;            //返回缓存目录cacheDir下的文件数组            File[] cachedFiles = cacheDir.listFiles();              if (cachedFiles != null)             {                //对文件数组遍历                for (File cachedFile : cachedFiles)                 {                                 size += calculateSize(cachedFile);                      count += 1;                    //将缓存文件和最后修改时间插入map                    lastUsageDates.put(cachedFile, cachedFile.lastModified());                  }                cacheSize.set(size); //设置为给定值                cacheCount.set(count); //设置为给定值            }        }    }).start();}

calculateCacheSizeAndCacheCount方法中开启线程进行大小和数量的计算。计算完后存入cacheSize和cacheCount,cacheSize和cacheCount在内部类中定义为AtomicLong和AtomicInteger量子类,也就是线程安全的。其基本的特性就是在多线程环境下,当有多个线程同时执行这些类的实例包含的方法时,具有排他性,即当某个线程进入方法,执行其中的指令时,不会被其他线程打断,而别的线程就像自旋锁一样,一直等到该方法执行完成,才由JVM从等待队列中选择一个另一个线程进入。


到这里获取缓存实例工作完成,主要完成了如下工作:

  • 新建了缓存目录
  • 通过ACache构造方法构造新实例,并且将该实例引用插入mInstanceMap
  • 实例化ACacheManager,计算cacheSize和cacheCount

2、往缓存实例存入数据
从上面的思维导图public method(各种数据的读写方法)中,有各种public的put和get等方法来缓存各种数据类型的数据。由上面的demo的put方法
mCache.put(“testString”, mEt_string_input.getText().toString(),300);我们找到原形:

  /**   * 保存 String数据 到 缓存中   * @param key 保存的key   * @param value 保存的String数据   * @param saveTime 保存的时间,单位:秒    */    public void put(String key, String value, int saveTime)     {        put(key, Utils.newStringWithDateInfo(saveTime, value));    }

这里的put方法可以指定缓存时间。调用他自身的另一个put方法:

/** * 保存 String数据 到 缓存中 *  @param key 保存的key * @param value 保存的String数据  */public void put(String key, String value) {    File file = mCache.newFile(key); //新建文件    BufferedWriter out = null; //缓冲字符输出流,作用是为其他字符输出流添加一些缓冲功能    try     {        out = new BufferedWriter(new FileWriter(file), 1024); //获取file字符流        out.write(value); // 把value写入    } catch (IOException e)    {        e.printStackTrace();    } finally     {        if (out != null)         {            try             {                out.flush();                        out.close();            } catch (IOException e)             {                e.printStackTrace();            }        }        //更新cacheCount和cacheSize lastUsageDates插入新建文件和时间的键值对        mCache.put(file);       }}

在put(String key, String value)方法中首先调用mCache.newFile(key)新建一个文件:

//新建文件private File newFile(String key) {    //新建文件,文件名为key的整型哈希码    return new File(cacheDir, key.hashCode() + "");     }

新建的文件名为key的整型哈希码。回到put(String key, String value)中,然后通过out.write(value);将数据存入文件。最后调用mCache.put(file);进行ACacheManager实例的更新操作

/** * 更新cacheCount和cacheSize lastUsageDates插入新建文件和时间的键值对 * 文件放入程序缓存后,统计缓存总量,总数,文件存放到文件map中(value值为文件最后修改时间,便于根据设置的销毁时间进行销毁) * 缓存没有超过限制,则增加缓存总量,总数的数值 * 缓存超过限制,则减少缓存总量,总数的数值 * 通过removeNext方法找到最老文件的大小 */private void put(File file){    int curCacheCount = cacheCount.get(); //获取数量     while (curCacheCount + 1 > countLimit) //大于上限    {        //移除旧的文件,返回文件大小        long freedSize = removeNext();          //更新cacheSize                cacheSize.addAndGet(-freedSize);                //更新cacheCount        curCacheCount = cacheCount.addAndGet(-1);    }    //更新cacheCount    cacheCount.addAndGet(1);    long valueSize = calculateSize(file); //计算文件大小    long curCacheSize = cacheSize.get(); //获取当前缓存大小    while (curCacheSize + valueSize > sizeLimit) //大于上限     {          //移除旧的文件,返回文件大小        long freedSize = removeNext();                      //更新curCacheSize        curCacheSize = cacheSize.addAndGet(-freedSize);         }    //更新cacheSize    cacheSize.addAndGet(valueSize);                             Long currentTime = System.currentTimeMillis();    //设置文件最后修改时间    file.setLastModified(currentTime);              //插入map    lastUsageDates.put(file, currentTime);      }

分析完ACacheManager的put()后,回到put(key, Utils.newStringWithDateInfo(saveTime, value))
其中第二个参数value传入的是Utils.newStringWithDateInfo(saveTime, value),而newStringWithDateInfo是ACache的内部工具类的一个方法,在value内容前面加上了时间信息

 //返回时间信息+valueprivate static String newStringWithDateInfo(int second, String strInfo) {    return createDateInfo(second) + strInfo;}

返回拼接createDateInfo(second)和value的字符串。createDateInfo()如下:

//时间信息private static String createDateInfo(int second){    String currentTime = System.currentTimeMillis() + "";    while (currentTime.length() < 13) //小于13,前面补0     {             currentTime = "0" + currentTime;    }    //返回的时间:当前时间-保存时间     return currentTime + "-" + second + mSeparator; }

3、从缓存实例读取数据

由上面的demo的get方法mCache.getAsString(“testString”);我们找到原形:

/** * 读取 String数据 * @param key  * @return String 数据  */public String getAsString(String key){    File file = mCache.get(key);    //获取文件    if (!file.exists())        return null;    boolean removeFile = false;    BufferedReader in = null;    try     {        in = new BufferedReader(new FileReader(file));        String readString = "";        String currentLine;        while ((currentLine = in.readLine()) != null) //逐行遍历         {             readString += currentLine;  //每行字符串连接        }        if (!Utils.isDue(readString)) //String数据未到期        {                             return Utils.clearDateInfo(readString);//去除时间信息的字符串内容        } else         {            //移除文件标志位为真            removeFile = true;                  return null;        }    } catch (IOException e)     {        e.printStackTrace();        return null;    } finally     {        if (in != null)         {            try             {                in.close();            } catch (IOException e)             {                e.printStackTrace();            }        }        if (removeFile)                     remove(key);    //移除缓存    }}

getAsString(String key)方法里首先通过缓存管理器的mCache.get(key)方法获取文件,然后用Utils.isDue(readString)**判断是否字符串数据到期,未到期返回去除时间信息的字符串内容;到期则移除缓存,返回空。**Utils.isDue(readString)调用了isDue(byte[] data)判断:

 /**  * 判断缓存的byte数据是否到期  * @param data  * @return true:到期了 false:还没有到期  */private static boolean isDue(byte[] data) {    String[] strs = getDateInfoFromDate(data);    if (strs != null && strs.length == 2)    {        String saveTimeStr = strs[0];        while (saveTimeStr.startsWith("0"))         {            saveTimeStr = saveTimeStr.substring(1, saveTimeStr.length());        }        long saveTime = Long.valueOf(saveTimeStr);        long deleteAfter = Long.valueOf(strs[1]);        if (System.currentTimeMillis() > saveTime + deleteAfter * 1000)        {            return true;        }    }    return false;}

至此整个缓存字符串读取过程在ACache的源码分析完成,其他缓存数据类型读取方法分析过程一样。
JsonObject、JsonArray、Bitmap、Drawable、序列化的存入缓存都是转化为字符串/byte格式,再调用函数put(String key, String value)即可。
比如BitMap的保存:

 /**  * 保存 bitmap 到 缓存中,有保存时间  * @param key 保存的key  * @param value 保存的 bitmap数据  * @param saveTime 保存的时间,单位:秒   */public void put(String key, Bitmap value, int saveTime){    put(key, Utils.Bitmap2Bytes(value), saveTime);}

Bitmap的读取:

/** * 读取 bitmap 数据 * @param key * @return bitmap 数据 */public Bitmap getAsBitmap(String key){    if (getAsBinary(key) == null)     {        return null;    }    return Utils.Bytes2Bimap(getAsBinary(key));}

思想就是把bitmap转化为byte[], 再调用缓存byte的函数 。通过Utils.Bitmap2Bytes(value)完成Bitmap → byte[] 的转换。通过Utils.Bytes2Bimap(getAsBinary(key))完成byte[] → Bitmap的转换。

/** * Bitmap → byte[]  */private static byte[] Bitmap2Bytes(Bitmap bm){    if (bm == null)     {        return null;    }    ByteArrayOutputStream baos = new ByteArrayOutputStream();//byte[]输出流    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);//按指定的图片格式以及画质,将图片转换为输出流。压缩图片,不压缩是100,表示压缩率为0     return baos.toByteArray();}/* * byte[] → Bitmap */private static Bitmap Bytes2Bimap(byte[] b) {    if (b.length == 0)     {        return null;    }        return BitmapFactory.decodeByteArray(b, 0, b.length);//解析}

ACache代码片(完整):

package com.example.acachetest.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.RandomAccessFile;import java.io.Serializable;import java.util.Collections;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.atomic.AtomicLong;import org.json.JSONArray;import org.json.JSONObject;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.PixelFormat;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;public class ACache{    public static final int TIME_HOUR = 60 * 60;    public static final int TIME_DAY = TIME_HOUR * 24;    private static final int MAX_SIZE = 1000 * 1000 * 50; // 50 mb    private static final int MAX_COUNT = Integer.MAX_VALUE; // 不限制存放数据的数量    private static Map<string, acache=""> mInstanceMap = new HashMap<string, acache="">();    private ACacheManager mCache;    public static ACache get(Context ctx)     {        return get(ctx, "ACache");    }    public static ACache get(Context ctx, String cacheName)    {        File f = new File(ctx.getCacheDir(), cacheName);        return get(f, MAX_SIZE, MAX_COUNT);    }    public static ACache get(File cacheDir)     {        return get(cacheDir, MAX_SIZE, MAX_COUNT);    }    public static ACache get(Context ctx, long max_zise, int max_count)    {        File f = new File(ctx.getCacheDir(), "ACache");        return get(f, max_zise, max_count);    }    public static ACache get(File cacheDir, long max_zise, int max_count)    {        ACache manager = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid());        if (manager == null)        {            manager = new ACache(cacheDir, max_zise, max_count);            mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), manager);        }        return manager;    }    private static String myPid()    {        return "_" + android.os.Process.myPid();    }    private ACache(File cacheDir, long max_size, int max_count)    {        if (!cacheDir.exists() && !cacheDir.mkdirs())        {            throw new RuntimeException("can't make dirs in "                    + cacheDir.getAbsolutePath());        }        mCache = new ACacheManager(cacheDir, max_size, max_count);    }    // =======================================    // ============ String数据 读写 ==============    // =======================================    /**     * 保存 String数据 到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的String数据     */    public void put(String key, String value)    {        File file = mCache.newFile(key);        BufferedWriter out = null;        try        {            out = new BufferedWriter(new FileWriter(file), 1024);            out.write(value);        } catch (IOException e)        {            e.printStackTrace();        } finally        {            if (out != null)             {                try                 {                    out.flush();                    out.close();                } catch (IOException e)                 {                    e.printStackTrace();                }            }            mCache.put(file);        }    }    /**     * 保存 String数据 到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的String数据     * @param saveTime     *            保存的时间,单位:秒     */    public void put(String key, String value, int saveTime)    {        put(key, Utils.newStringWithDateInfo(saveTime, value));    }    /**     * 读取 String数据     *      * @param key     * @return String 数据     */    public String getAsString(String key)    {        File file = mCache.get(key);        if (!file.exists())            return null;        boolean removeFile = false;        BufferedReader in = null;        try         {            in = new BufferedReader(new FileReader(file));            String readString = "";            String currentLine;            while ((currentLine = in.readLine()) != null)             {                readString += currentLine;            }            if (!Utils.isDue(readString))             {                return Utils.clearDateInfo(readString);            } else             {                removeFile = true;                return null;            }        } catch (IOException e)         {            e.printStackTrace();            return null;        } finally         {            if (in != null)             {                try                 {                    in.close();                } catch (IOException e)                 {                    e.printStackTrace();                }            }            if (removeFile)                remove(key);        }    }    // =======================================    // ============= JSONObject 数据 读写 ==============    // =======================================    /**     * 保存 JSONObject数据 到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的JSON数据     */    public void put(String key, JSONObject value)     {        put(key, value.toString());    }    /**     * 保存 JSONObject数据 到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的JSONObject数据     * @param saveTime     *            保存的时间,单位:秒     */    public void put(String key, JSONObject value, int saveTime)    {        put(key, value.toString(), saveTime);    }    /**     * 读取JSONObject数据     *      * @param key     * @return JSONObject数据     */    public JSONObject getAsJSONObject(String key)     {        String JSONString = getAsString(key);        try         {            JSONObject obj = new JSONObject(JSONString);            return obj;        } catch (Exception e)         {            e.printStackTrace();            return null;        }    }    // =======================================    // ============ JSONArray 数据 读写 =============    // =======================================    /**     * 保存 JSONArray数据 到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的JSONArray数据     */    public void put(String key, JSONArray value)     {        put(key, value.toString());    }    /**     * 保存 JSONArray数据 到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的JSONArray数据     * @param saveTime     *            保存的时间,单位:秒     */    public void put(String key, JSONArray value, int saveTime)    {        put(key, value.toString(), saveTime);    }    /**     * 读取JSONArray数据     *      * @param key     * @return JSONArray数据     */    public JSONArray getAsJSONArray(String key)    {        String JSONString = getAsString(key);        try        {            JSONArray obj = new JSONArray(JSONString);            return obj;        } catch (Exception e)        {            e.printStackTrace();            return null;        }    }    // =======================================    // ============== byte 数据 读写 =============    // =======================================    /**     * 保存 byte数据 到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的数据     */    public void put(String key, byte[] value)     {        File file = mCache.newFile(key);        FileOutputStream out = null;        try         {            out = new FileOutputStream(file);            out.write(value);        } catch (Exception e)         {            e.printStackTrace();        } finally         {            if (out != null)             {                try                 {                    out.flush();                    out.close();                } catch (IOException e)                 {                    e.printStackTrace();                }            }            mCache.put(file);        }    }    /**     * 保存 byte数据 到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的数据     * @param saveTime     *            保存的时间,单位:秒     */    public void put(String key, byte[] value, int saveTime)     {        put(key, Utils.newByteArrayWithDateInfo(saveTime, value));    }    /**     * 获取 byte 数据     *      * @param key     * @return byte 数据     */    public byte[] getAsBinary(String key)     {        RandomAccessFile RAFile = null;        boolean removeFile = false;        try         {            File file = mCache.get(key);            if (!file.exists())                return null;            RAFile = new RandomAccessFile(file, "r");            byte[] byteArray = new byte[(int) RAFile.length()];            RAFile.read(byteArray);            if (!Utils.isDue(byteArray))             {                return Utils.clearDateInfo(byteArray);            } else             {                removeFile = true;                return null;            }        } catch (Exception e)         {            e.printStackTrace();            return null;        } finally         {            if (RAFile != null)             {                try                 {                    RAFile.close();                } catch (IOException e)                 {                    e.printStackTrace();                }            }            if (removeFile)                remove(key);        }    }    // =======================================    // ============= 序列化 数据 读写 ===============    // =======================================    /**     * 保存 Serializable数据 到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的value     */    public void put(String key, Serializable value)     {        put(key, value, -1);    }    /**     * 保存 Serializable数据到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的value     * @param saveTime     *            保存的时间,单位:秒     */    public void put(String key, Serializable value, int saveTime)    {        ByteArrayOutputStream baos = null;        ObjectOutputStream oos = null;        try         {            baos = new ByteArrayOutputStream();            oos = new ObjectOutputStream(baos);            oos.writeObject(value);            byte[] data = baos.toByteArray();            if (saveTime != -1)             {                put(key, data, saveTime);            } else             {                put(key, data);            }        } catch (Exception e)         {            e.printStackTrace();        } finally         {            try             {                oos.close();            } catch (IOException e)             {            }        }    }    /**     * 读取 Serializable数据     *      * @param key     * @return Serializable 数据     */    public Object getAsObject(String key)     {        byte[] data = getAsBinary(key);        if (data != null)         {            ByteArrayInputStream bais = null;            ObjectInputStream ois = null;            try             {                bais = new ByteArrayInputStream(data);                ois = new ObjectInputStream(bais);                Object reObject = ois.readObject();                return reObject;            } catch (Exception e)             {                e.printStackTrace();                return null;            } finally             {                try                 {                    if (bais != null)                        bais.close();                } catch (IOException e)                 {                    e.printStackTrace();                }                try                 {                    if (ois != null)                        ois.close();                } catch (IOException e)                 {                    e.printStackTrace();                }            }        }        return null;      }    // =======================================    // ============== bitmap 数据 读写 =============    // =======================================    /**     * 保存 bitmap 到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的bitmap数据     */    public void put(String key, Bitmap value)     {        put(key, Utils.Bitmap2Bytes(value));    }    /**     * 保存 bitmap 到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的 bitmap 数据     * @param saveTime     *            保存的时间,单位:秒     */    public void put(String key, Bitmap value, int saveTime)     {        put(key, Utils.Bitmap2Bytes(value), saveTime);    }    /**     * 读取 bitmap 数据     *      * @param key     * @return bitmap 数据     */    public Bitmap getAsBitmap(String key)     {        if (getAsBinary(key) == null)         {            return null;        }        return Utils.Bytes2Bimap(getAsBinary(key));    }    // =======================================    // ============= drawable 数据 读写 =============    // =======================================    /**     * 保存 drawable 到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的drawable数据     */    public void put(String key, Drawable value)     {        put(key, Utils.drawable2Bitmap(value));    }    /**     * 保存 drawable 到 缓存中     *      * @param key     *            保存的key     * @param value     *            保存的 drawable 数据     * @param saveTime     *            保存的时间,单位:秒     */    public void put(String key, Drawable value, int saveTime)    {        put(key, Utils.drawable2Bitmap(value), saveTime);    }    /**     * 读取 Drawable 数据     *      * @param key     * @return Drawable 数据     */    public Drawable getAsDrawable(String key)     {        if (getAsBinary(key) == null)         {            return null;        }        return Utils.bitmap2Drawable(Utils.Bytes2Bimap(getAsBinary(key)));    }    /**     * 获取缓存文件     *      * @param key     * @return value 缓存的文件     */    public File file(String key)     {        File f = mCache.newFile(key);        if (f.exists())            return f;        return null;    }    /**     * 移除某个key     *      * @param key     * @return 是否移除成功     */    public boolean remove(String key)     {        return mCache.remove(key);    }    /**     * 清除所有数据     */    public void clear()     {        mCache.clear();    }    /**     * @title 缓存管理器     * @version 1.0     */    public class ACacheManager     {        private final AtomicLong cacheSize;        private final AtomicInteger cacheCount;        private final long sizeLimit;        private final int countLimit;        private final Map<file, long=""> lastUsageDates = Collections                .synchronizedMap(new HashMap<file, long="">());        protected File cacheDir;        private ACacheManager(File cacheDir, long sizeLimit, int countLimit)         {            this.cacheDir = cacheDir;            this.sizeLimit = sizeLimit;            this.countLimit = countLimit;            cacheSize = new AtomicLong();            cacheCount = new AtomicInteger();            calculateCacheSizeAndCacheCount();        }        /**         * 计算 cacheSize和cacheCount         */        private void calculateCacheSizeAndCacheCount()         {            new Thread(new Runnable()             {                @Override                public void run()                 {                    int size = 0;                    int count = 0;                    File[] cachedFiles = cacheDir.listFiles();                    if (cachedFiles != null)                     {                        for (File cachedFile : cachedFiles)                         {                            size += calculateSize(cachedFile);                            count += 1;                            lastUsageDates.put(cachedFile,                                    cachedFile.lastModified());                        }                        cacheSize.set(size);                        cacheCount.set(count);                    }                }            }).start();        }        private void put(File file)         {            int curCacheCount = cacheCount.get();            while (curCacheCount + 1 > countLimit)             {                long freedSize = removeNext();                cacheSize.addAndGet(-freedSize);                curCacheCount = cacheCount.addAndGet(-1);            }            cacheCount.addAndGet(1);            long valueSize = calculateSize(file);            long curCacheSize = cacheSize.get();            while (curCacheSize + valueSize > sizeLimit)             {                long freedSize = removeNext();                curCacheSize = cacheSize.addAndGet(-freedSize);            }            cacheSize.addAndGet(valueSize);            Long currentTime = System.currentTimeMillis();            file.setLastModified(currentTime);            lastUsageDates.put(file, currentTime);        }        private File get(String key)         {            File file = newFile(key);            Long currentTime = System.currentTimeMillis();            file.setLastModified(currentTime);            lastUsageDates.put(file, currentTime);            return file;        }        private File newFile(String key)         {            return new File(cacheDir, key.hashCode() + "");        }        private boolean remove(String key)        {            File image = get(key);            return image.delete();        }        private void clear()         {            lastUsageDates.clear();            cacheSize.set(0);            File[] files = cacheDir.listFiles();            if (files != null)             {                for (File f : files)                 {                    f.delete();                }            }        }        /**         * 移除旧的文件         *          * @return         */        private long removeNext()         {            if (lastUsageDates.isEmpty())             {                return 0;            }            Long oldestUsage = null;            File mostLongUsedFile = null;            Set<entry<file, long="">> entries = lastUsageDates.entrySet();            synchronized (lastUsageDates)             {                for (Entry<file, long=""> entry : entries)                 {                    if (mostLongUsedFile == null)                     {                        mostLongUsedFile = entry.getKey();                        oldestUsage = entry.getValue();                    } else                     {                        Long lastValueUsage = entry.getValue();                        if (lastValueUsage < oldestUsage)                         {                            oldestUsage = lastValueUsage;                            mostLongUsedFile = entry.getKey();                        }                    }                }            }            long fileSize = calculateSize(mostLongUsedFile);            if (mostLongUsedFile.delete())            {                lastUsageDates.remove(mostLongUsedFile);            }            return fileSize;        }        private long calculateSize(File file)        {            return file.length();        }    }    /**     * @title 时间计算工具类     * @version 1.0     */    private static class Utils     {        /**         * 判断缓存的String数据是否到期         *          * @param str         * @return true:到期了 false:还没有到期         */        private static boolean isDue(String str)         {            return isDue(str.getBytes());        }        /**         * 判断缓存的byte数据是否到期         *          * @param data         * @return true:到期了 false:还没有到期         */        private static boolean isDue(byte[] data)         {            String[] strs = getDateInfoFromDate(data);            if (strs != null && strs.length == 2)             {                String saveTimeStr = strs[0];                while (saveTimeStr.startsWith("0"))                 {                    saveTimeStr = saveTimeStr                            .substring(1, saveTimeStr.length());                }                long saveTime = Long.valueOf(saveTimeStr);                long deleteAfter = Long.valueOf(strs[1]);                if (System.currentTimeMillis() > saveTime + deleteAfter * 1000)                {                    return true;                }            }            return false;        }        private static String newStringWithDateInfo(int second, String strInfo)         {            return createDateInfo(second) + strInfo;        }        private static byte[] newByteArrayWithDateInfo(int second, byte[] data2)         {            byte[] data1 = createDateInfo(second).getBytes();            byte[] retdata = new byte[data1.length + data2.length];            System.arraycopy(data1, 0, retdata, 0, data1.length);            System.arraycopy(data2, 0, retdata, data1.length, data2.length);            return retdata;        }        private static String clearDateInfo(String strInfo)         {            if (strInfo != null && hasDateInfo(strInfo.getBytes()))             {                strInfo = strInfo.substring(strInfo.indexOf(mSeparator) + 1,                        strInfo.length());            }            return strInfo;        }        private static byte[] clearDateInfo(byte[] data)         {            if (hasDateInfo(data))             {                return copyOfRange(data, indexOf(data, mSeparator) + 1,                        data.length);            }            return data;        }        private static boolean hasDateInfo(byte[] data)         {            return data != null && data.length > 15 && data[13] == '-'                    && indexOf(data, mSeparator) > 14;        }        private static String[] getDateInfoFromDate(byte[] data)         {            if (hasDateInfo(data))             {                String saveDate = new String(copyOfRange(data, 0, 13));                String deleteAfter = new String(copyOfRange(data, 14,                        indexOf(data, mSeparator)));                return new String[] { saveDate, deleteAfter };            }            return null;        }        private static int indexOf(byte[] data, char c)         {            for (int i = 0; i < data.length; i++)             {                if (data[i] == c)                 {                    return i;                }            }            return -1;        }        private static byte[] copyOfRange(byte[] original, int from, int to)        {            int newLength = to - from;            if (newLength < 0)                throw new IllegalArgumentException(from + " > " + to);            byte[] copy = new byte[newLength];            System.arraycopy(original, from, copy, 0,                    Math.min(original.length - from, newLength));            return copy;        }        private static final char mSeparator = ' ';        private static String createDateInfo(int second)         {            String currentTime = System.currentTimeMillis() + "";            while (currentTime.length() < 13)             {                currentTime = "0" + currentTime;            }            return currentTime + "-" + second + mSeparator;        }        /*         * Bitmap → byte[]         */        private static byte[] Bitmap2Bytes(Bitmap bm)         {            if (bm == null)             {                return null;            }            ByteArrayOutputStream baos = new ByteArrayOutputStream();            bm.compress(Bitmap.CompressFormat.PNG, 100, baos);            return baos.toByteArray();        }        /*         * byte[] → Bitmap         */        private static Bitmap Bytes2Bimap(byte[] b)         {            if (b.length == 0)             {                return null;            }            return BitmapFactory.decodeByteArray(b, 0, b.length);        }        /*         * Drawable → Bitmap         */        private static Bitmap drawable2Bitmap(Drawable drawable)         {            if (drawable == null)             {                return null;            }            // 取 drawable 的长宽            int w = drawable.getIntrinsicWidth();            int h = drawable.getIntrinsicHeight();            // 取 drawable 的颜色格式            Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888                    : Bitmap.Config.RGB_565;            // 建立对应 bitmap            Bitmap bitmap = Bitmap.createBitmap(w, h, config);            // 建立对应 bitmap 的画布            Canvas canvas = new Canvas(bitmap);            drawable.setBounds(0, 0, w, h);            // 把 drawable 内容画到画布中            drawable.draw(canvas);            return bitmap;        }        /*         * Bitmap → Drawable         */        @SuppressWarnings("deprecation")        private static Drawable bitmap2Drawable(Bitmap bm)         {            if (bm == null)             {                return null;            }            return new BitmapDrawable(bm);        }    }}

总结

该开源库类简单,容易理解。

  1. 可以使用ACache把那些不需要实时更新的数据缓存起来,一来减少网络请求,二来本地加载速度也快。
  2. 可以设置缓存时间。
  3. 可以替换SharePreference当做配置文件,保存多种数据类型,比如可以保存头像信息。
0 0
原创粉丝点击