• android 内部缓存器(手机自带的存储空间中的当前包文件的路径)


    关于Context中:

    1. getCacheDir()方法用于获取/data/data/<application package>/cache目录

    2. getFilesDir()方法用于获取/data/data/<application package>/files目录

    3. getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据

    4. Context.getExternalCacheDir()方法可以获取到 SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据

    注:上面的方法,当你的应用在被用户卸载后,SDCard/Android/data/你的应用的包名/ 这个目录下的所有文件都会被删除,不会留下垃圾信息。

    上面二个目录分别对应 设置->应用->应用详情里面的”清除数据“与”清除缓存“选项

    通过判断SDcard存在或者SDcard不可被移除的时候判断来获取缓存地址:

     1 publicStringgetDiskCacheDir(Contextcontext){
     2     StringcachePath = null;
     3     //Environment.getExtemalStorageState() 获取SDcard的状态
     4     //Environment.MEDIA_MOUNTED 手机装有SDCard,并且可以进行读写
     5 
     6     if(Environment.MEDIA_MOUNTED.equals(
     7                Environment.getExternalStorageState())
     8                || !Environment.isExternalStorageRemovable()){
     9             cachePath=context.getExternalCacheDir().getPath();
    10     }else{
    11             cachePath=context.getCacheDir().getPath();
    12     }
    13     returncachePath;
    14 }   

    AtomicLong、AtomicInteger原子量即操作变量的操作是“原子的”,该操作不可再分,因此是线程安全的。

    使用原子变量原因是多个线程对单个变量操作也会引起一些问题。

    Java5之后,专门提供了用来进行单变量多线程并发安全访问的工具包java.util.concurrent.atomic,其中的类也很简单。

    原子量虽然可以保证单个变量在某一个操作过程的安全,但无法保证你整个代码块,或者整个程序的安全性。因此,通常还应该使用锁等同步机制来控制整个程序的安全性。

    内部存储管理器:

      1 public class ACacheManager {
      2         private final AtomicLong cacheSize;
      3         private final AtomicInteger cacheCount;
      4         private final long sizeLimit;
      5         private final int countLimit;
      6         private final Map<File, Long> lastUsageDates = Collections
      7                 .synchronizedMap(new HashMap<File, Long>());
      8         protected File cacheDir;
      9 
     10         private ACacheManager(File cacheDir, long sizeLimit, int countLimit) {
     11             this.cacheDir = cacheDir;
     12             this.sizeLimit = sizeLimit;
     13             this.countLimit = countLimit;
     14             cacheSize = new AtomicLong();
     15             cacheCount = new AtomicInteger();
     16             calculateCacheSizeAndCacheCount();
     17         }
     18 
     19         /**
     20          * 计算 cacheSize 和 cacheCount
     21          */
     22         private void calculateCacheSizeAndCacheCount() {
     23             new Thread(new Runnable() {
     24                 @Override
     25                 public void run() {
     26                     int size = 0;
     27                     int count = 0;
     28                     File[] cachedFiles = cacheDir.listFiles();
     29                     if (cachedFiles != null) {
     30                         for (File cachedFile : cachedFiles) {
     31                             size += calculateSize(cachedFile);
     32                             count += 1;
     33                             lastUsageDates.put(cachedFile,
     34                                     cachedFile.lastModified());
     35                         }
     36                         cacheSize.set(size);
     37                         cacheCount.set(count);
     38                     }
     39                 }
     40             }).start();
     41         }
     42 
     43         private void put(File file) {
     44             int curCacheCount = cacheCount.get();
     45             while (curCacheCount + 1 > countLimit) {
     46                 long freedSize = removeHistoryFile();
     47                 cacheSize.addAndGet(-freedSize);
     48 
     49                 curCacheCount = cacheCount.addAndGet(-1);
     50             }
     51             cacheCount.addAndGet(1);
     52 
     53             long valueSize = calculateSize(file);
     54             long curCacheSize = cacheSize.get();
     55             while (curCacheSize + valueSize > sizeLimit) {
     56                 long freedSize = removeHistoryFile();
     57                 curCacheSize = cacheSize.addAndGet(-freedSize);
     58             }
     59             cacheSize.addAndGet(valueSize);
     60 
     61             Long currentTime = System.currentTimeMillis();
     62             file.setLastModified(currentTime);
     63             lastUsageDates.put(file, currentTime);
     64         }
     65 
     66         private File get(String key) {
     67             File file = newFile(key);
     68             Long currentTime = System.currentTimeMillis();
     69             file.setLastModified(currentTime);
     70             lastUsageDates.put(file, currentTime);
     71 
     72             return file;
     73         }
     74 
     75         private File newFile(String key) {
     76             return new File(cacheDir, key.hashCode() + "");
     77         }
     78 
     79         private boolean remove(String key) {
     80             File image = get(key);
     81             return image.delete();
     82         }
     83 
     84         private void clear() {
     85             lastUsageDates.clear();
     86             cacheSize.set(0);
     87             File[] files = cacheDir.listFiles();
     88             if (files != null) {
     89                 for (File f : files) {
     90                     f.delete();
     91                 }
     92             }
     93         }
     94 
     95         /**
     96          * 移除旧的文件
     97          * 
     98          * @return
     99          */
    100         private long removeHistoryFile() {
    101             if (lastUsageDates.isEmpty()) {
    102                 return 0;
    103             }
    104 
    105             Long oldestUsage = null;
    106             File mostLongUsedFile = null;
    107             Set<Entry<File, Long>> entries = lastUsageDates.entrySet();
    108             synchronized (lastUsageDates) {
    109                 for (Entry<File, Long> entry : entries) {
    110                     if (mostLongUsedFile == null) {
    111                         mostLongUsedFile = entry.getKey();
    112                         oldestUsage = entry.getValue();
    113                     } else {
    114                         Long lastValueUsage = entry.getValue();
    115                         if (lastValueUsage < oldestUsage) {
    116                             oldestUsage = lastValueUsage;
    117                             mostLongUsedFile = entry.getKey();
    118                         }
    119                     }
    120                 }
    121             }
    122 
    123             long fileSize = calculateSize(mostLongUsedFile);
    124             if (mostLongUsedFile.delete()) {
    125                 lastUsageDates.remove(mostLongUsedFile);
    126             }
    127             return fileSize;
    128         }
    129 
    130         private long calculateSize(File file) {
    131             return file.length();
    132         }
    133     }

    判断存储文件是否到期等工具类:

      1 private static class Utils {
      2 
      3         /**
      4          * 判断缓存的String数据是否到期
      5          * 
      6          * @param str
      7          * @return true:到期了 false:还没有到期
      8          */
      9         private static boolean isDue(String str) {
     10             return isDue(str.getBytes());
     11         }
     12 
     13         /**
     14          * 判断缓存的byte数据是否到期
     15          * 
     16          * @param data
     17          * @return true:到期了 false:还没有到期
     18          */
     19         private static boolean isDue(byte[] data) {
     20             String[] strs = getDateInfoFromDate(data);
     21             if (strs != null && strs.length == 2) {
     22                 String saveTimeStr = strs[0];
     23                 while (saveTimeStr.startsWith("0")) {
     24                     saveTimeStr = saveTimeStr
     25                             .substring(1, saveTimeStr.length());
     26                 }
     27                 long saveTime = Long.valueOf(saveTimeStr);
     28                 long deleteAfter = Long.valueOf(strs[1]);
     29                 if (System.currentTimeMillis() > saveTime + deleteAfter * 1000) {
     30                     return true;
     31                 }
     32             }
     33             return false;
     34         }
     35 
     36         // 
     37         private static String newStringWithDateInfo(int second, String strInfo) {
     38             return createDateInfo(second) + strInfo;
     39         }
     40 
     41         // 
     42         private static byte[] newByteArrayWithDateInfo(int second, byte[] data2) {
     43             byte[] data1 = createDateInfo(second).getBytes();
     44             byte[] retdata = new byte[data1.length + data2.length];
     45             System.arraycopy(data1, 0, retdata, 0, data1.length);
     46             System.arraycopy(data2, 0, retdata, data1.length, data2.length);
     47             return retdata;
     48         }
     49 
     50         // 
     51         private static String clearDateInfo(String strInfo) {
     52             if (strInfo != null && hasDateInfo(strInfo.getBytes())) {
     53                 strInfo = strInfo.substring(strInfo.indexOf(mSeparator) + 1,
     54                         strInfo.length());
     55             }
     56             return strInfo;
     57         }
     58 
     59         // 
     60         private static byte[] clearDateInfo(byte[] data) {
     61             if (hasDateInfo(data)) {
     62                 return copyOfRange(data, indexOf(data, mSeparator) + 1,
     63                         data.length);
     64             }
     65             return data;
     66         }
     67 
     68         // 
     69         private static boolean hasDateInfo(byte[] data) {
     70             return data != null && data.length > 15 && data[13] == '-'
     71                     && indexOf(data, mSeparator) > 14;
     72         }
     73 
     74         // 
     75         private static String[] getDateInfoFromDate(byte[] data) {
     76             if (hasDateInfo(data)) {
     77                 String saveDate = new String(copyOfRange(data, 0, 13));
     78                 String deleteAfter = new String(copyOfRange(data, 14,
     79                         indexOf(data, mSeparator)));
     80                 return new String[] { saveDate, deleteAfter };
     81             }
     82             return null;
     83         }
     84 
     85         // 
     86         private static int indexOf(byte[] data, char c) {
     87             for (int i = 0; i < data.length; i++) {
     88                 if (data[i] == c) {
     89                     return i;
     90                 }
     91             }
     92             return -1;
     93         }
     94 
     95         // 
     96         private static byte[] copyOfRange(byte[] original, int from, int to) {
     97             int newLength = to - from;
     98             if (newLength < 0)
     99                 throw new IllegalArgumentException(from + " > " + to);
    100             byte[] copy = new byte[newLength];
    101             System.arraycopy(original, from, copy, 0,
    102                     Math.min(original.length - from, newLength));
    103             return copy;
    104         }
    105 
    106         // 
    107         private static final char mSeparator = ' ';
    108 
    109         // 
    110         private static String createDateInfo(int second) {
    111             String currentTime = System.currentTimeMillis() + "";
    112             while (currentTime.length() < 13) {
    113                 currentTime = "0" + currentTime;
    114             }
    115             return currentTime + "-" + second + mSeparator;
    116         }
    117 
    118         /*
    119          * Bitmap to byte[]
    120          */
    121         private static byte[] Bitmap2Bytes(Bitmap bm) {
    122             if (bm == null) {
    123                 return null;
    124             }
    125             ByteArrayOutputStream baos = new ByteArrayOutputStream();
    126             bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    127             return baos.toByteArray();
    128         }
    129 
    130         /*
    131          * byte[] to Bitmap
    132          */
    133         private static Bitmap Bytes2Bimap(byte[] b) {
    134             if (b.length == 0) {
    135                 return null;
    136             }
    137             return BitmapFactory.decodeByteArray(b, 0, b.length);
    138         }
    139 
    140         /*
    141          * Drawable to Bitmap
    142          */
    143         private static Bitmap drawable2Bitmap(Drawable drawable) {
    144             if (drawable == null) {
    145                 return null;
    146             }
    147             int w = drawable.getIntrinsicWidth();
    148             int h = drawable.getIntrinsicHeight();
    149             Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
    150                     : Bitmap.Config.RGB_565;
    151             // 建立对应 bitmap
    152             Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    153             // 建立对应 bitmap 的画布
    154             Canvas canvas = new Canvas(bitmap);
    155             drawable.setBounds(0, 0, w, h);
    156             // drawable 内容画到画布
    157             drawable.draw(canvas);
    158             return bitmap;
    159         }
    160 
    161         /*
    162          * Bitmap to Drawable
    163          */
    164         @SuppressWarnings("deprecation")
    165         private static Drawable bitmap2Drawable(Bitmap bm) {
    166             if (bm == null) {
    167                 return null;
    168             }
    169             return new BitmapDrawable(bm);
    170         }
    171     }

    内部缓存使用类:

      1 public class ACache {
      2     public static final int TIME_HOUR = 60 * 60;
      3     public static final int TIME_DAY = TIME_HOUR * 24;
      4     private static final int MAX_SIZE = 1000 * 1000 * 50; // 50 mb
      5     private static final int MAX_COUNT = Integer.MAX_VALUE; // 不限制存放数据的数量
      6     private static Map<String, ACache> mInstanceMap = new HashMap<String, ACache>();
      7     private ACacheManager mCache;
      8 
      9     public static ACache get(Context ctx) {
     10         return get(ctx, "ACache");
     11     }
     12 
     13     public static ACache get(Context ctx, String cacheName) {
     14         File f = new File(ctx.getCacheDir(), cacheName);
     15         return get(f, MAX_SIZE, MAX_COUNT);
     16     }
     17 
     18     public static ACache get(File cacheDir) {
     19         return get(cacheDir, MAX_SIZE, MAX_COUNT);
     20     }
     21 
     22     public static ACache get(Context ctx, long max_zise, int max_count) {
     23         File f = new File(ctx.getCacheDir(), "ACache");
     24         return get(f, max_zise, max_count);
     25     }
     26 
     27     public static ACache get(File cacheDir, long max_zise, int max_count) {
     28         ACache manager = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid());
     29         if (manager == null) {
     30             manager = new ACache(cacheDir, max_zise, max_count);
     31             mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), manager);
     32         }
     33         return manager;
     34     }
     35 
     36     private static String myPid() {
     37         return "_" + android.os.Process.myPid();
     38     }
     39 
     40     private ACache(File cacheDir, long max_size, int max_count) {
     41         if (!cacheDir.exists() && !cacheDir.mkdirs()) {
     42             throw new RuntimeException("can't make dirs in "
     43                     + cacheDir.getAbsolutePath());
     44         }
     45         mCache = new ACacheManager(cacheDir, max_size, max_count);
     46     }
     47 
     48     // =======================================
     49     // ============ String数据 读写 ==============
     50     // =======================================
     51     /**
     52      * 保存 String数据
     53      * 
     54      * @param key
     55      *            保存的key
     56      * @param value
     57      *            保存的String数据
     58      */
     59     public void put(String key, String value) {
     60         File file = mCache.newFile(key);
     61         BufferedWriter out = null;
     62         try {
     63             out = new BufferedWriter(new FileWriter(file), 1024);
     64             out.write(value);
     65         } catch (IOException e) {
     66             e.printStackTrace();
     67         } finally {
     68             if (out != null) {
     69                 try {
     70                     out.flush();
     71                     out.close();
     72                 } catch (IOException e) {
     73                     e.printStackTrace();
     74                 }
     75             }
     76             mCache.put(file);
     77         }
     78     }
     79 
     80     /**
     81      * 保存 String数据
     82      * 
     83      * @param key
     84      *            保存的key
     85      * @param value
     86      *            保存的String数据
     87      * @param saveTime
     88      *            保存的时间,单位:秒
     89      */
     90     public void put(String key, String value, int saveTime) {
     91         put(key, Utils.newStringWithDateInfo(saveTime, value));
     92     }
     93 
     94     /**
     95      * 读取 String数据
     96      * 
     97      * @param key
     98      * @return String 数据
     99      */
    100     public String getAsString(String key) {
    101         File file = mCache.get(key);
    102         if (!file.exists())
    103             return null;
    104         boolean removeFile = false;
    105         BufferedReader in = null;
    106         try {
    107             in = new BufferedReader(new FileReader(file));
    108             String readString = "";
    109             String currentLine;
    110             while ((currentLine = in.readLine()) != null) {
    111                 readString += currentLine;
    112             }
    113             if (!Utils.isDue(readString)) {
    114                 return Utils.clearDateInfo(readString);
    115             } else {
    116                 removeFile = true;
    117                 return null;
    118             }
    119         } catch (IOException e) {
    120             e.printStackTrace();
    121             return null;
    122         } finally {
    123             if (in != null) {
    124                 try {
    125                     in.close();
    126                 } catch (IOException e) {
    127                     e.printStackTrace();
    128                 }
    129             }
    130             if (removeFile)
    131                 remove(key);
    132         }
    133     }
    134 
    135     // =======================================
    136     // ============= JSONObject 数据 读写 ==============
    137     // =======================================
    138     /**
    139      * 保存 JSONObject数据
    140      * 
    141      * @param key
    142      *            保存的key
    143      * @param value
    144      *            保存的JSON数据
    145      */
    146     public void put(String key, JSONObject value) {
    147         put(key, value.toString());
    148     }
    149 
    150     /**
    151      * 保存 JSONObject数据
    152      * 
    153      * @param key
    154      *            保存的key
    155      * @param value
    156      *            保存的JSONObject数据
    157      * @param saveTime
    158      *            保存的时间,单位:秒
    159      */
    160     public void put(String key, JSONObject value, int saveTime) {
    161         put(key, value.toString(), saveTime);
    162     }
    163 
    164     /**
    165      * 读取JSONObject数据
    166      * 
    167      * @param key
    168      * @return JSONObject数据
    169      */
    170     public JSONObject getAsJSONObject(String key) {
    171         String JSONString = getAsString(key);
    172         try {
    173             JSONObject obj = new JSONObject(JSONString);
    174             return obj;
    175         } catch (Exception e) {
    176             e.printStackTrace();
    177             return null;
    178         }
    179     }
    180 
    181     // =======================================
    182     // ============ JSONArray 数据 读写 =============
    183     // =======================================
    184     /**
    185      * 保存 JSONArray数据
    186      * 
    187      * @param key
    188      *            保存的key
    189      * @param value
    190      *            保存的JSONArray数据
    191      */
    192     public void put(String key, JSONArray value) {
    193         put(key, value.toString());
    194     }
    195 
    196     /**
    197      * 保存 JSONArray数据
    198      * 
    199      * @param key
    200      *            保存的key
    201      * @param value
    202      *            保存的JSONArray数据
    203      * @param saveTime
    204      *            保存的时间,单位:秒
    205      */
    206     public void put(String key, JSONArray value, int saveTime) {
    207         put(key, value.toString(), saveTime);
    208     }
    209 
    210     /**
    211      * 读取JSONArray数据
    212      * 
    213      * @param key
    214      * @return JSONArray数据
    215      */
    216     public JSONArray getAsJSONArray(String key) {
    217         String JSONString = getAsString(key);
    218         try {
    219             JSONArray obj = new JSONArray(JSONString);
    220             return obj;
    221         } catch (Exception e) {
    222             e.printStackTrace();
    223             return null;
    224         }
    225     }
    226 
    227     // =======================================
    228     // ============== byte 数据 读写 =============
    229     // =======================================
    230     /**
    231      * 保存 byte数据
    232      * 
    233      * @param key
    234      *            保存的key
    235      * @param value
    236      *            保存的数
    237      */
    238     public void put(String key, byte[] value) {
    239         File file = mCache.newFile(key);
    240         FileOutputStream out = null;
    241         try {
    242             out = new FileOutputStream(file);
    243             out.write(value);
    244         } catch (Exception e) {
    245             e.printStackTrace();
    246         } finally {
    247             if (out != null) {
    248                 try {
    249                     out.flush();
    250                     out.close();
    251                 } catch (IOException e) {
    252                     e.printStackTrace();
    253                 }
    254             }
    255             mCache.put(file);
    256         }
    257     }
    258 
    259     /**
    260      * 保存 byte数据
    261      * 
    262      * @param key
    263      *            保存的key
    264      * @param value
    265      *            保存的数
    266      * @param saveTime
    267      *            保存的时间,单位:秒
    268      */
    269     public void put(String key, byte[] value, int saveTime) {
    270         put(key, Utils.newByteArrayWithDateInfo(saveTime, value));
    271     }
    272 
    273     /**
    274      * 获取 byte 数据
    275      * 
    276      * @param key
    277      * @return byte 数据
    278      */
    279     public byte[] getAsBinary(String key) {
    280         RandomAccessFile RAFile = null;
    281         boolean removeFile = false;
    282         try {
    283             File file = mCache.get(key);
    284             if (!file.exists())
    285                 return null;
    286             RAFile = new RandomAccessFile(file, "r");
    287             byte[] byteArray = new byte[(int) RAFile.length()];
    288             RAFile.read(byteArray);
    289             if (!Utils.isDue(byteArray)) {
    290                 return Utils.clearDateInfo(byteArray);
    291             } else {
    292                 removeFile = true;
    293                 return null;
    294             }
    295         } catch (Exception e) {
    296             e.printStackTrace();
    297             return null;
    298         } finally {
    299             if (RAFile != null) {
    300                 try {
    301                     RAFile.close();
    302                 } catch (IOException e) {
    303                     e.printStackTrace();
    304                 }
    305             }
    306             if (removeFile)
    307                 remove(key);
    308         }
    309     }
    310 
    311     // =======================================
    312     // ============= 序列�?数据 读写 ===============
    313     // =======================================
    314     /**
    315      * 保存 Serializable数据
    316      * 
    317      * @param key
    318      *            保存的key
    319      * @param value
    320      *            保存的value
    321      */
    322     public void put(String key, Serializable value) {
    323         put(key, value, -1);
    324     }
    325 
    326     /**
    327      * 保存 Serializable数据
    328      * 
    329      * @param key
    330      *            保存的key
    331      * @param value
    332      *            保存的value
    333      * @param saveTime
    334      *            保存的时间,单位:秒
    335      */
    336     public void put(String key, Serializable value, int saveTime) {
    337         ByteArrayOutputStream baos = null;
    338         ObjectOutputStream oos = null;
    339         try {
    340             baos = new ByteArrayOutputStream();
    341             oos = new ObjectOutputStream(baos);
    342             oos.writeObject(value);
    343             byte[] data = baos.toByteArray();
    344             if (saveTime != -1) {
    345                 put(key, data, saveTime);
    346             } else {
    347                 put(key, data);
    348             }
    349         } catch (Exception e) {
    350             e.printStackTrace();
    351         } finally {
    352             try {
    353                 oos.close();
    354             } catch (IOException e) {
    355             }
    356         }
    357     }
    358 
    359     /**
    360      * 读取 Serializable数据
    361      * 
    362      * @param key
    363      * @return Serializable 数据
    364      */
    365     public Object getAsObject(String key) {
    366         byte[] data = getAsBinary(key);
    367         if (data != null) {
    368             ByteArrayInputStream bais = null;
    369             ObjectInputStream ois = null;
    370             try {
    371                 bais = new ByteArrayInputStream(data);
    372                 ois = new ObjectInputStream(bais);
    373                 Object reObject = ois.readObject();
    374                 return reObject;
    375             } catch (Exception e) {
    376                 e.printStackTrace();
    377                 return null;
    378             } finally {
    379                 try {
    380                     if (bais != null)
    381                         bais.close();
    382                 } catch (IOException e) {
    383                     e.printStackTrace();
    384                 }
    385                 try {
    386                     if (ois != null)
    387                         ois.close();
    388                 } catch (IOException e) {
    389                     e.printStackTrace();
    390                 }
    391             }
    392         }
    393         return null;
    394 
    395     }
    396 
    397     // =======================================
    398     // ============== bitmap 数据 读写 =============
    399     // =======================================
    400     /**
    401      * 保存 bitmap
    402      * 
    403      * @param key
    404      *            保存的key
    405      * @param value
    406      *            保存的bitmap数据
    407      */
    408     public void put(String key, Bitmap value) {
    409         put(key, Utils.Bitmap2Bytes(value));
    410     }
    411 
    412     /**
    413      * 保存 bitmap
    414      * 
    415      * @param key
    416      *            保存的key
    417      * @param value
    418      *            保存bitmap 数据
    419      * @param saveTime
    420      *            保存的时间,单位:秒
    421      */
    422     public void put(String key, Bitmap value, int saveTime) {
    423         put(key, Utils.Bitmap2Bytes(value), saveTime);
    424     }
    425 
    426     /**
    427      * 读取 bitmap 数据
    428      * 
    429      * @param key
    430      * @return bitmap 数据
    431      */
    432     public Bitmap getAsBitmap(String key) {
    433         if (getAsBinary(key) == null) {
    434             return null;
    435         }
    436         return Utils.Bytes2Bimap(getAsBinary(key));
    437     }
    438 
    439     // =======================================
    440     // ============= drawable 数据 读写 =============
    441     // =======================================
    442     /**
    443      * 保存 drawable
    444      * 
    445      * @param key
    446      *            保存的key
    447      * @param value
    448      *            保存的drawable数据
    449      */
    450     public void put(String key, Drawable value) {
    451         put(key, Utils.drawable2Bitmap(value));
    452     }
    453 
    454     /**
    455      * 保存 drawable
    456      * 
    457      * @param key
    458      *            保存的key
    459      * @param value
    460      *            保存drawable 数据
    461      * @param saveTime
    462      *            保存的时间,单位:秒
    463      */
    464     public void put(String key, Drawable value, int saveTime) {
    465         put(key, Utils.drawable2Bitmap(value), saveTime);
    466     }
    467 
    468     /**
    469      * 读取 Drawable 数据
    470      * 
    471      * @param key
    472      * @return Drawable 数据
    473      */
    474     public Drawable getAsDrawable(String key) {
    475         if (getAsBinary(key) == null) {
    476             return null;
    477         }
    478         return Utils.bitmap2Drawable(Utils.Bytes2Bimap(getAsBinary(key)));
    479     }
    480 
    481     /**
    482      * 获取缓存文件
    483      * 
    484      * @param key
    485      * @return value 缓存的文
    486      */
    487     public File file(String key) {
    488         File f = mCache.newFile(key);
    489         if (f.exists())
    490             return f;
    491         return null;
    492     }
    493 
    494     /**
    495      * 移除某个key
    496      * 
    497      * @param key
    498      * @return 是否移除成功
    499      */
    500     public boolean remove(String key) {
    501         return mCache.remove(key);
    502     }
    503 
    504     /**
    505      * 清除
    506      */
    507     public void clear() {
    508         mCache.clear();
    509     }
    510 }

    e.g. 缓存和获取,根据索引获取的json串。

     1   /**
     2      * 设置json串缓存数据(key,value)
     3      */
     4     public void setCacheStr(String key, String value) {
     5         if (!StringUtils.isEmpty(value)) {
     6             ACache.get(this).put(key, value);
     7         }
     8     }
     9 
    10     /**
    11      * 获取json串缓存数据更具key
    12      */
    13     public String getCacheStr(String key) {
    14         return ACache.get(this).getAsString(key);
    15     }
  • 相关阅读:
    如何用机器学习强化市场营销活动。
    大数据统计脚本, 分城市订单统计
    宇宙常量与增长黑客。
    病毒传播效果的衡量公式
    浅谈对增长黑客的理解
    大数据分析, 数据挖掘, 机器学习,找到产品改进的爆点。
    R语言的日期运算
    安装语言包-英文(美国)
    selenium page objects
    logging模块
  • 原文地址:https://www.cnblogs.com/CharlesGrant/p/4813671.html
Copyright © 2020-2023  润新知