ASimpleCache icon indicating copy to clipboard operation
ASimpleCache copied to clipboard

代码优化建议

Open mosentest opened this issue 6 years ago • 1 comments

`

//private static Map<String, ACache> mInstanceMap = Collections.synchronizedMap(new HashMap<String, ACache>()); private static ACache instance = null;

/**
 * 个人觉得没必要用map来缓存实例,一个就够用了
 * @param cacheDir
 * @param max_zise
 * @param max_count
 * @return
 */
public  static ACache get(File cacheDir, long max_zise, int max_count) {
    if (instance == null) {
        synchronized (ACache.class) {
            //ACache manager = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid());
            if (instance == null) {
                instance = new ACache(cacheDir, max_zise, max_count);
                //mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), manager);
            }
        }
    }
    return instance;
}

    /**
     * hashCode发生碰撞怎么办,我建议用base64
     * @param key
     * @return
     */
    private File newFile(String key) {
        //return new File(cacheDir, key.hashCode() + "");
        return new File(cacheDir, Base64.encodeToString(key.getBytes(), Base64.NO_WRAP) + "");
    }

` 我改过的版本在这里: https://github.com/moz1q1/WalleLibrary/tree/master/library_utils/src/main/java/org/wall/mo/utils/cache 你都不合并人家代码,我只能这样改了

mosentest avatar Jun 23 '18 08:06 mosentest

你这么改,全局只有一个Acahe,那么cacheDir就无法修改了。会产生三个问题 1、用户自定义存储的目录失效 2、只存在一个目录,一旦存储内容多,50m上限达到,容易造成文件频繁删除 3、lastUsageDates的map初次缓存特别大,对于内存属于浪费

MartinLi2015 avatar Nov 26 '20 17:11 MartinLi2015