LruCache和DiskLruCache


//**
 * 三级缓存:
 * 一级缓存:内存缓存
 * 二级缓存:硬盘缓存
 * 三级缓存:网络缓存
 *
 * @author 43497
 * @date 2018/3/15
 */

public class LruCacheUtil {

    /**
     * 内存缓存最大值--设置为可使用的内存的1/8
     * LruCache 如果缓存的大小已经达到了最大值,最近时间最少使用的缓存数据被移除
     */
    private static LruCache<String, Bitmap> mLruCache = new LruCache<String, Bitmap>((int) (Runtime.getRuntime().maxMemory() / 8)) {

        /**
         * 用于计算缓存数据的大小
         * @param key
         * @param bitmap
         * @return
         */
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            return bitmap.getRowBytes() * bitmap.getHeight();
        }
    };

    /**
     * 添加缓存
     *
     * @param url
     * @param bitmap
     */
    public static void putCache(String url, Bitmap bitmap) {
        mLruCache.put(url, bitmap);
    }

    /**
     * 获取缓存
     *
     * @param url
     * @return
     */
    public static Bitmap getCache(String url) {
        return mLruCache.get(url);
    }

    /**
     * 移除缓存
     *
     * @param url
     */
    public static void removeCache(String url) {
        if (getCache(url) != null) {
            mLruCache.remove(url);
        }
    }

    /**
     * 清除缓存
     */
    public static void clearCache() {
        if (mLruCache.size() > 0) {
            mLruCache.evictAll();
        }
    }
}
/**
 * 硬盘缓存
 * DiskLruCache
 *
 * 硬盘缓存的位置(该路径会随着App的卸载自动删除掉)
 * 外部存储:getExternalCacheDir()-->SDCard/Android/data/<应用程序包名>/cache
 * 内部存储:getCacheDir()-->data/data/<应用程序包名>/cache
 *
 * @author 43497
 * @date 2018/3/12
 */

public class DiskLruCacheUtil {

    public static DiskLruCache mDiskLruCache;
    private static int maxSize = 1024 * 1024 * 10;

    public static void init(Context context) {
        if (mDiskLruCache != null) {
            try {
                /**
                 * 参数1:磁盘缓存的路径
                 * 参数2:设置App的版本号,版本号升级,则磁盘缓存自动清空
                 * 参数3:表示一个key值最多可以对应多少个value
                 * 参数4:最大可以缓存多少数据
                 */
                mDiskLruCache = DiskLruCache.open(getCacheFile(context, "bitmap"), getAppVersion(context), 1, maxSize);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 写入缓存
     *
     * @param url
     * @param bitmap
     * @return
     */
    public static boolean writeCache(String url, Bitmap bitmap) {
        if (url != null && bitmap != null) {
            DiskLruCache.Editor editor = null;
            try {
                editor = mDiskLruCache.edit(url);
                OutputStream outputStream = editor.newOutputStream(0);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
                editor.commit();
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 读取缓存
     *
     * @param url
     * @return
     */
    public static Bitmap getCache(String url) {
        if (url != null){
            DiskLruCache.Snapshot snapshot = null;
            try {
                snapshot = mDiskLruCache.get(url);
                if (snapshot != null){
                    InputStream inputStream = snapshot.getInputStream(0);
                    return BitmapFactory.decodeStream(inputStream);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 清除缓存
     */
    public static void clearCache(){
        try {
            mDiskLruCache.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 同步日志文件
     */
    public static void flushLog(){
        try {
            mDiskLruCache.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭
     */
    public static void close(){
        try {
            mDiskLruCache.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取磁盘路径
     *
     * @param context
     * @param endPath
     * @return
     */
    private static File getCacheFile(Context context, String endPath) {
        String cachePath;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            cachePath = context.getExternalCacheDir().getAbsolutePath();
        } else {
            cachePath = context.getCacheDir().getAbsolutePath();
        }
        File cacheFile = new File(cachePath, endPath);
        if (!cacheFile.exists()) {
            cacheFile.mkdirs();
        }
        return cacheFile;
    }

    private static int getAppVersion(Context context) {
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值