Skip to content

保存SD卡缓存有Bug #5

Description

@ttylinux

DataCache:
public void saveData(@nonnull String key, @nonnull T data) {
mLruCache.put(key, data);
mDiskCache.put(key, data, ACache.TIME_WEEK); // 数据缓存时间为 1 周
}
执行到最后:

/**
 * 保存 byte数据 到 缓存中
 *
 * @param key   保存的key
 * @param value 保存的数据
 */
public void put(String key, byte[] value) {
    File file = mCache.newFile(key);
....
  mCache.put(file);

}

private File newFile(String key) {
return new File(cacheDir, key.hashCode() + "");
}
private void put(File file) {
.....
Long currentTime = System.currentTimeMillis();
file.setLastModified(currentTime);
lastUsageDates.put(file, currentTime);
}
每次put操作,会新建一个File对象,然后将该引用保存到lastUsageDates中
///////////////////////////////////////////////////////
get操作
public T getData(@nonnull String key) {
T result = (T) mLruCache.get(key);
if (result == null) {
result = (T) mDiskCache.getAsObject(key);
if (result != null) {
mLruCache.put(key, result);
}
}
return result;
}

/**
 * 获取 byte 数据
 *
 * @param key
 * @return byte 数据
 */
public byte[] getAsBinary(String key) {
    RandomAccessFile RAFile = null;
    boolean removeFile = false;
    try {
        File file = mCache.get(key);
  ....
 }
  private File get(String key) {
        File file = newFile(key);
        Long currentTime = System.currentTimeMillis();
        file.setLastModified(currentTime);
        lastUsageDates.put(file, currentTime);

        return file;
    }

每次get操作,都会创建一个新的File对象,然后将这个File引用保存到lastUsageDates中。
//////////////////////////////////////////////////////////////////
那么,bug就是,比如,我存放一个SD卡缓存,key是loginInfo,保存到SD卡中的一个文件。
然后,我不断去get,会不断添加新的File引用到lastUsageDates。lastUsageDates中,使用的key是File引用值,每个新创建的File对象,它们的引用值都是不同的,即使它们指向相同的文件。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions