android轻量级缓存框架ASimpleCache分析

来源:互联网 发布:赚钱宝监控软件 编辑:程序博客网 时间:2024/05/16 14:35

官方简介:

ASimpleCache 是一个为android制定的 轻量级的 开源缓存框架。轻量到只有一个java文件(由十几个类精简而来)。

(有个问题是作者所说的自动失效,其实是在获取数据时判断存入缓存的数据是否过期,如果过期,则删除数据缓存,返回null。当然,如果真正的自动删除,应该只能开启服务,不断判断是否过期来删除吧,也没有必要)

转载自:http://www.2cto.com/kf/201504/391805.html
--------------------------------------------------------------------------------


1、它可以缓存什么东西?
普通的字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象,和 byte数据。

2、它有什么特色?
特色主要是:
1:轻,轻到只有一个JAVA文件。
2:可配置,可以配置缓存路径,缓存大小,缓存数量等。
3:可以设置缓存超时时间,缓存超时自动失效,并被删除。
4:支持多进程。
3、它在android中可以用在哪些场景?
1、替换SharePreference当做配置文件
2、可以缓存网络请求数据,比如oschina的android客户端可以缓存http请求的新闻内容,缓存时间假设为1个小时,超时后自动失效,让客户端重新请求新的数据,减少客户端流量,同时减少服务器并发量。
3、您来说...
4、如何使用 ASimpleCache?
以下有个小的demo,希望您能喜欢:

ACache mCache = ACache.get(this);
mCache.put(test_key1, test value);
mCache.put(test_key2, test value, 10);//保存10秒,如果超过10秒去获取这个key,将为null
mCache.put(test_key3, test value, 2 * ACache.TIME_DAY);//保存两天,如果超过两天去获取这个key,将为null
获取数据

ACache mCache = ACache.get(this);
String value = mCache.getAsString(test_key1);
 

主要分析下集万千宠爱于一身的ACache类吧(以字符串存储为例)

一,首先先要创建缓存

get(Context ctx, String cacheName)方法新建缓存目录

get(File cacheDir, long max_zise, int max_count)方法新建缓存实例,存入实例map,key为缓存目录加上每次应用开启的进程id

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public static ACache get(Context ctx) {
        returnget(ctx, ACache);
    }
 
    publicstatic ACache get(Context ctx, String cacheName) {
        //新建缓存目录
        ///data/data/com.yangfuhai.asimplecachedemo/cache/ACache
        File f =new File(ctx.getCacheDir(), cacheName);
        returnget(f, MAX_SIZE, MAX_COUNT);
    }
 
    publicstatic ACache get(File cacheDir) {
        returnget(cacheDir, MAX_SIZE, MAX_COUNT);
    }
 
    publicstatic ACache get(Context ctx,long max_zise, int max_count) {
        File f =new File(ctx.getCacheDir(), ACache);
        returnget(f, max_zise, max_count);
    }
 
    publicstatic ACache get(File cacheDir,long max_zise, int max_count) {
        ///data/data/com.yangfuhai.asimplecachedemo/cache/ACache
        ACache manager = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid());
        if(manager == null) {
            manager =new ACache(cacheDir, max_zise, max_count);
            //{/data/data/com.yangfuhai.asimplecachedemo/cache/ACache_4137=org.afinal.simplecache.ACache@2bc38270}
            //{/data/data/com.yangfuhai.asimplecachedemo/cache/ACache_12189=org.afinal.simplecache.ACache@2bc3d890}
            mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), manager);
        }
        returnmanager;
    }
 
    privatestatic String myPid() {
        return_ + android.os.Process.myPid();
    }
 
    privateACache(File cacheDir, longmax_size, int max_count) {
        if(!cacheDir.exists() && !cacheDir.mkdirs()) {
            thrownew RuntimeException(can't make dirs in  + cacheDir.getAbsolutePath());
        }
        mCache =new ACacheManager(cacheDir, max_size, max_count);
    }

 

二,存入数据

put(String key, String value)方法写数据到文件

put(String key, String value)方法中的mCache.put(file)方法做了如下设置

文件放入程序缓存后,统计缓存总量,总数,文件存放到文件map中(value值为文件最后修改时间,便于根据设置的销毁时间进行销毁)
缓存没有超过限制,则增加缓存总量,总数的数值
缓存超过限制,则减少缓存总量,总数的数值
通过removeNext方法找到最老文件的大小

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void put(String key, String value) {
        File file = mCache.newFile(key);
        BufferedWriter out =null;
        try{
            out =new BufferedWriter(newFileWriter(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);
        }
    }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//文件放入程序缓存后,统计缓存总量,总数,文件存放到文件map中(value值为文件最后修改时间,便于根据设置的销毁时间进行销毁)
      //缓存没有超过限制,则增加缓存总量,总数的数值
      //缓存超过限制,则减少缓存总量,总数的数值
      //通过removeNext方法找到最老文件的大小
      privatevoid put(File file) {
          intcurCacheCount = cacheCount.get();
          while(curCacheCount + 1> countLimit) {
              longfreedSize = removeNext();
              cacheSize.addAndGet(-freedSize);
 
              curCacheCount = cacheCount.addAndGet(-1);
          }
          cacheCount.addAndGet(1);
 
          longvalueSize = calculateSize(file);
          longcurCacheSize = cacheSize.get();
          while(curCacheSize + valueSize > sizeLimit) {
              longfreedSize = removeNext();
              curCacheSize = cacheSize.addAndGet(-freedSize);
          }
          cacheSize.addAndGet(valueSize);
 
          Long currentTime = System.currentTimeMillis();
          file.setLastModified(currentTime);
          lastUsageDates.put(file, currentTime);
      }

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
         * 移除旧的文件(冒泡,找到最后修改时间最小的文件)
         *
         * @return
         */
        privatelong removeNext() {
            if(lastUsageDates.isEmpty()) {
                return0;
            }
 
            Long oldestUsage =null;
            File mostLongUsedFile =null;
            Set<entry<file,long="">> entries = lastUsageDates.entrySet();
            synchronized(lastUsageDates) {
                for(Entry<file, long=""> entry : entries) {
                    if(mostLongUsedFile == null) {
                        mostLongUsedFile = entry.getKey();
                        oldestUsage = entry.getValue();
                    }else {
                        Long lastValueUsage = entry.getValue();
                        if(lastValueUsage < oldestUsage) {
                            oldestUsage = lastValueUsage;
                            mostLongUsedFile = entry.getKey();
                        }
                    }
                }
            }
 
            longfileSize = calculateSize(mostLongUsedFile);
            if(mostLongUsedFile.delete()) {
                lastUsageDates.remove(mostLongUsedFile);
            }
            returnfileSize;
        }</file,></entry<file,>


三,获取数据

getAsString(String key)方法从缓存文件中读取数据,其中通过Utils.isDue(readString)方法判断数据是否过期,是否要删除

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public String getAsString(String key) {
        ///data/data/com.yangfuhai.asimplecachedemo/cache/ACache/1727748931
        File file = mCache.get(key);
        if(!file.exists())
            returnnull;
        booleanremoveFile = false;
        BufferedReader in =null;
        try{
            in =new BufferedReader(newFileReader(file));
            String readString = ;
            String currentLine;
            while((currentLine = in.readLine()) != null) {
                readString += currentLine;
            }
            if(!Utils.isDue(readString)) {
                returnUtils.clearDateInfo(readString);
            }else {
                removeFile =true;
                returnnull;
            }
        }catch (IOException e) {
            e.printStackTrace();
            returnnull;
        }finally {
            if(in != null) {
                try{
                    in.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(removeFile)
                remove(key);
        }
    }

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
         * 判断缓存的String数据是否到期
         *
         * @param str
         * @return true:到期了 false:还没有到期
         */
        privatestatic booleanisDue(String str) {
            returnisDue(str.getBytes());
        }
 
        /**
         * 判断缓存的byte数据是否到期(到期:当前时间大于保存时间加上保存后的存留时间)
         *
         * @param data
         * @return true:到期了 false:还没有到期
         */
        privatestatic booleanisDue(byte[] data) {
            String[] strs = getDateInfoFromDate(data);
            if(strs != null && strs.length == 2) {
                String saveTimeStr = strs[0];
                while(saveTimeStr.startsWith(0)) {
                    saveTimeStr = saveTimeStr.substring(1, saveTimeStr.length());
                }
                longsaveTime = Long.valueOf(saveTimeStr);
                longdeleteAfter = Long.valueOf(strs[1]);
                if(System.currentTimeMillis() > saveTime + deleteAfter * 1000) {
                    returntrue;
                }
            }
            returnfalse;
        }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
//数据有无存留时间设置
       privatestatic booleanhasDateInfo(byte[] data) {
           returndata != null && data.length > 15 && data[13] == '-' && indexOf(data, mSeparator) >14;
       }
       //saveDate文件保存时间毫秒数,deleteAfter文件保存后的保留时间毫秒数
       privatestatic String[] getDateInfoFromDate(byte[] data) {
           if(hasDateInfo(data)) {
               String saveDate =new String(copyOfRange(data,0, 13));
               String deleteAfter =new String(copyOfRange(data,14, indexOf(data, mSeparator)));
               returnnew String[] { saveDate, deleteAfter };
           }
           returnnull;
       }

0 0