ACache 源码解读

1 首先直接看ACache的构造方法

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

其中构造了一个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();
}

里面申明了一个Collections.synchronizedMap(new HashMap<File, Long>()) 这个东西是用来维护cache的大小用的,并没有什么好谈,类似Lurcache 里面那个双端队列的维护。

3 在获取实例对象的方法中

public static ACache get(Context ctx, String cacheName) {
        File f = new File(ctx.getCacheDir(), cacheName);
        return get(f, MAX_SIZE, MAX_COUNT);
    }

创建了一个文件,这个文件是放在 \Android\data\com.packagename\ 这个目录下面,这个目录下面的文件是会随着APP 的卸载而被清空的,是通用的缓存存放位置

4 接下来看put方法

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

    private File newFile(String key) {
            return new File(cacheDir, key.hashCode() + "");
        }

在上面说到的目录下创建一个文件,把相应的string 写入文件,这个就实现了缓存效果,很简单把!

5 接下来我们看看怎么实现过期效果的

    public void put(String key, String value, int saveTime) {
        put(key, Utils.newStringWithDateInfo(saveTime, value));
    }

    private static String newStringWithDateInfo(int second, String strInfo) {
            return createDateInfo(second) + strInfo;
        }

    private static String createDateInfo(int second) {
            String currentTime = System.currentTimeMillis() + "";
            while (currentTime.length() < 13) {
                currentTime = "0" + currentTime;
            }
            return currentTime + "-" + second + mSeparator;
        }

就是在创建文件的时间加上一个当时的时间 和 过期时间 ,然后在对取的时候在去获取 时间比较一下,如果过期了,就renturn null,这样就可以了,英文最新项目很多地方感觉可以使用缓存,我想mrleam自己实现一套缓存机制。

 

转载于:https://www.cnblogs.com/seanzhoutao/p/6054452.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值