Android http 请求 Json数据缓存到内存

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.innjoo.store.cache;  
  2. import com.ferris.utils.StringUtils;  
  3. import android.support.v4.util.LruCache;  
  4. /** 
  5.  *  
  6.  * @ClassName: LruJsonCache json缓存类 
  7.  * @Description: TODO 
  8.  * @author 重播 
  9.  * @email 459821731@qq.com 
  10.  * @date 2014-12-27 下午5:33:49 
  11.  * @csdn blog.csdn.net/xufeifandj 
  12.  */  
  13. public class LruJsonCache {  
  14.     private LruCache<String, String> mMemoryCache;  
  15.   
  16.     public LruJsonCache() {  
  17.         int maxMemory = (int) Runtime.getRuntime().maxMemory() / 10;  
  18.         mMemoryCache = new LruCache<String, String>(maxMemory) {  
  19.             @Override  
  20.             protected int sizeOf(String key, String value) {  
  21.                 return value.length();  
  22.             }  
  23.         };  
  24.     }  
  25.   
  26.     /** 
  27.      *  
  28.      * @Title: addJsonToMemoryCache 
  29.      * @Description: TODO 添加json内存 
  30.      * @return void 
  31.      */  
  32.     public void addJsonToMemoryCache(String key, String jsonString) {  
  33.         if (mMemoryCache == null) {  
  34.             return;  
  35.         }  
  36.         if (StringUtils.isEmpty(key)) {  
  37.             return;  
  38.         }  
  39.   
  40.         if (getJsonFromMemCache(key) == null && jsonString != null) {  
  41.             mMemoryCache.put(key, jsonString);  
  42.         }  
  43.     }  
  44.   
  45.     /** 
  46.      * 从内存缓存中获取一个Json 
  47.      * @param key 
  48.      * @return 
  49.      */  
  50.     public String getJsonFromMemCache(String key) {  
  51.         if (mMemoryCache == null) {  
  52.             return null;  
  53.         }  
  54.         return mMemoryCache.get(key);  
  55.     }  
  56. }  
posted @ 2016-11-29 15:23  天涯海角路  阅读(232)  评论(0)    收藏  举报