关闭

Android中DisLruCache缓存封装

标签: 缓存androidcache磁盘缓存
463人阅读 评论(0) 收藏 举报
分类:

Android中DisLruCache缓存封装

DisLruCache文件下载

CSDN下载

github下载

DisLruCache介绍

DisLruCache介绍(郭霖博客)

DisLruCache介绍(鸿洋博客)

DisLruCache之CacheManager封装

CacheManager是对DisLruCache的再次封装

public class CacheManager {

    private static CacheManager manager;
    private DiskLruCache mDiskLruCache = null;
    private static final int CACHE_MAX_SIZE = 10 * 1024 * 1024;
    private static final int VALUE_COUNT = 1;
    private static Context context = APP.getContext();
    private static final int BUFFER = 1024;

    private CacheManager() {
    }

    public static CacheManager getInstance() {
        if (manager == null) {
            synchronized (CacheManager.class) {
                if (manager == null) {
                    manager = new CacheManager();
                }
            }
        }
        return manager;
    }

    /**
     * 获取版本号
     */
    public int getAppVersion() {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 1;
    }

    /**
     * 获取缓存目录
     */
    public File getDiskCacheDir(@NonNull String uniqueName) {
        String cachePath = "";
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            File f = context.getExternalCacheDir();
            if (f != null) {
                cachePath = f.getPath();
            }
        } else {
            cachePath = context.getCacheDir().getPath();
        }
        Log.e("zhang", "getDiskCacheDir: " + cachePath);
        return new File(cachePath + File.separator + uniqueName);
    }

    /**
     * 一个缓存目录只有一个文件
     */
    public CacheManager open(String dir) {
        open(dir, VALUE_COUNT);
        return manager;
    }

    /**
     * dir:缓存的目录/路径
     * valueCount:缓存文件的个数
     */
    public CacheManager open(String dir, int valueCount) {
        try {
            mDiskLruCache = DiskLruCache.open(getDiskCacheDir(dir), getAppVersion(), valueCount, CACHE_MAX_SIZE);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return manager;
    }

    /**
     * md5生成缓存key
     */
    public String hashKeyForDisk(@NonNull String key) {
        String cacheKey;
        try {
            final MessageDigest mDigest = MessageDigest.getInstance("MD5");
            mDigest.update(key.getBytes());
            cacheKey = bytesToHexString(mDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            cacheKey = String.valueOf(key.hashCode());
        }
        return cacheKey;
    }

    private String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte aByte : bytes) {
            String hex = Integer.toHexString(0xFF & aByte);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }

    /**
     * 保存字符串
     * key是通过LinkedHashMap获取缓存中的Entry对象
     * index是获取目录中的文件索引
     * value是需要保存的信息值
     */
    public void saveString(String key, int index, @NonNull String value) {
        try {
            DiskLruCache.Editor editor = mDiskLruCache.edit(hashKeyForDisk(key));
            editor.set(index, value);
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void saveString(String key, @NonNull String value) {
        saveString(key, 0, value);
    }

    /**
     * 保存流信息
     * key是通过LinkedHashMap获取缓存中的Entry对象
     * index是获取目录中的文件索引
     * value是需要保存的信息流
     */
    public void saveStream(String key, int index, @NonNull InputStream value) {
        int len = 0;
        byte[] bytes = new byte[BUFFER];
        OutputStream os = null;
        try {
            DiskLruCache.Editor editor = mDiskLruCache.edit(hashKeyForDisk(key));
            os = editor.newOutputStream(index);
            while ((len = value.read(bytes)) != -1) {
                os.write(bytes, 0, len);
            }
            editor.commit();
            mDiskLruCache.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //快速关流
            DiskLruCache.closeQuietly(os);
        }
    }

    public void saveStream(@NonNull String key, @NonNull InputStream value) {
        saveStream(key, 0, value);
    }

    /**
     * 获取缓存的String字符串
     * key是通过LinkedHashMap获取缓存中的Entry对象
     * index是获取目录中的文件索引
     */
    public String getString(@NonNull String key, int index) {
        try {
            DiskLruCache.Snapshot snapshot = mDiskLruCache.get(hashKeyForDisk(key));
            if (snapshot != null) {
                return snapshot.getString(index);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public String getString(@NonNull String key) {
        return getString(key, 0);
    }

    /**
     * 获取缓存的String字符串
     * key是通过LinkedHashMap获取缓存中的Entry对象
     * index是获取目录中的文件索引
     */
    public InputStream getInputStream(@NonNull String key, int index) {
        try {
            DiskLruCache.Snapshot snapshot = mDiskLruCache.get(hashKeyForDisk(key));
            if (snapshot == null) return null;
            return snapshot.getInputStream(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public InputStream getInputStream(@NonNull String key) {
        return getInputStream(key, 0);
    }

    /**
     * 删除对应的key缓存
     */
    public boolean remove(@NonNull String key) {
        try {
            return mDiskLruCache.remove(hashKeyForDisk(key));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 清除所有信息
     */
    public void clear() {
        try {
            mDiskLruCache.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:226019次
    • 积分:4270
    • 等级:
    • 排名:第7441名
    • 原创:205篇
    • 转载:7篇
    • 译文:2篇
    • 评论:45条
    博客专栏
    最新评论