LruDiskCache

 
  1. /**
  2.      * Opens the cache in {@code directory}, creating a cache if none exists
  3.      * there.
  4.      *
  5.      * @param directory a writable directory
  6.      * @param appVersion
  7.      * @param valueCount the number of values per cache entry. Must be positive.  每份缓存限定字节数
  8.      * @param maxSize the maximum number of bytes this cache should use to store  文件缓存总字节数
  9.      * @throws IOException if reading or writing the cache directory fails
  10.      */

//理解 本方法就是要创建一个对于磁盘文件的LruDiskCache


    public static LruDiskCache open(File directory, int appVersion, int valueCount, long maxSize)
            throws IOException {
        if (maxSize <= 0) {
            throw new IllegalArgumentException("maxSize <= 0");
        }
        if (valueCount <= 0) {
            throw new IllegalArgumentException("valueCount <= 0");
        }

        // 是否存在备份文件
        File backupFile = new File(directory, JOURNAL_FILE_BACKUP);
        if (backupFile.exists()) {
            File journalFile = new File(directory, JOURNAL_FILE);
            // 如果正常文件存在就删除备份文件
            if (journalFile.exists()) {
                backupFile.delete();
            } else {
                renameTo(backupFile, journalFile, false);
            }
        }

        // 读取每个缓存
        LruDiskCache cache = new LruDiskCache(directory, appVersion, valueCount, maxSize);
        if (cache.journalFile.exists()) {
            try {
                cache.readJournal();
                cache.processJournal();
                cache.journalWriter = new BufferedWriter(
                        new OutputStreamWriter(new FileOutputStream(cache.journalFile, true), HTTP.US_ASCII));
                return cache;
            } catch (Throwable journalIsCorrupt) {
                LogUtils.e("LruDiskCache"
                        + directory
                        + " is corrupt: "
                        + journalIsCorrupt.getMessage()
                        + ", removing", journalIsCorrupt);
                cache.delete();
            }
        }

        // 创建一个新的空的缓存
        if (directory.exists() || directory.mkdirs()) {
            cache = new LruDiskCache(directory, appVersion, valueCount, maxSize);
            cache.rebuildJournal();
        }
        return cache;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值