android实现缓存图片等数据

 更新时间:2015年07月31日 10:11:42   投稿:hebedich  
本文给大家分享的是Android采用LinkedHashMap自带的LRU 算法缓存数据的方法和示例,有需要的小伙伴可以参考下。

采用LinkedHashMap自带的LRU 算法缓存数据, 可检测对象是否已被虚拟机回收,并且重新计算当前缓存大小,清除缓存中无用的键值对象(即已经被虚拟机回收但未从缓存清除的数据);
 * 默认内存缓存大小为: 4 * 1024 * 1024 可通过通过setMaxCacheSize重新设置缓存大小,可手动清空内存缓存
 * <br>支持内存缓存和磁盘缓存方式, 通过 {@link cc.util.cache.NetByteWrapper} 支持HTTP缓存 (注:详细参考cc.util.http包); 注:使用JDK7

package cc.util.cache;
 
import java.io.Serializable;
import java.util.Objects;
 
/**
  封装网络数据, 将数据的Etag、lastModified获取到, 下次请求的时候提取出来到服务器比对
 * Help to wrap byte data which obtains from network, It will work with {@link cc.util.cache.NetChacheManager} 
 * @author wangcccong
 * @version 1.1406
 * <br> create at: Tues, 10 Jun 2014
 */
public class NetByteWrapper implements Serializable {
 
  private final static long serialVersionUID = 1L;
 
  /** data from network */
  private byte[] data;
  /** data size */
  int contentLength;
  /** latested modify time */
  private long lastModified;
  /** ETag: look up HTTP Protocol */
  private String ETag;
 
  public NetByteWrapper(byte[] data, long lastModified, String Etag) {
    this.data = data;
    this.lastModified = lastModified;
    this.ETag = Etag;
  }
   
  public byte[] getData() {
    return data;
  }
  public void setData(byte[] data) {
    this.data = data;
  }
 
  public long getLastModified() {
    return lastModified;
  }
  public void setLastModified(long lastModified) {
    this.lastModified = lastModified;
  }
 
  public String getETag() {
    return ETag;
  }
   
  public void setETag(String eTag) {
    this.ETag = eTag;
  }
   
  public int getContentLength() {
    return Objects.isNull(data) ? 0 : data.length;
  }
}
 
 
package cc.util.cache;
 
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
 
/**采用软引用方式将数据存放起来
 * enclose {@link cc.util.cache.NetByteWrapper} with {@link java.lang.ref.SoftReference}, In order to recycle the memory
 * @author wangcccong
 * @version 1.1406
 * <br> create at: Tues, 10 Jun. 2014
 */
public class NetByteSoftReference extends SoftReference<NetByteWrapper> {
   
  private String key = "";
  private long length = 0;
 
  public NetByteSoftReference(String key, NetByteWrapper arg0) {
    this(key, arg0, null);
  }
 
  public NetByteSoftReference(String key, NetByteWrapper arg0,
      ReferenceQueue<? super NetByteWrapper> arg1) {
    super(arg0, arg1);
    // TODO Auto-generated constructor stub
    this.key = key;
    this.length = arg0.getContentLength();
  }
   
  public String getKey() {
    return key;
  }
   
  public long getLength() {
    return length;
  }
 
}
 
 
package cc.util.cache;
 
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Objects;
 
/**
 * 采用LinkedHashMap自带的LRU 算法缓存数据, 可检测对象是否已被虚拟机回收,并且重新计算当前缓存大小,清除缓存中无用的键值对象(即已经被虚拟机回收但未从缓存清除的数据);
 * 默认内存缓存大小为: 4 * 1024 * 1024 可通过通过setMaxCacheSize重新设置缓存大小,可手动清空内存缓存,支持采用内存映射方式读取缓存
 * <br>支持内存缓存和磁盘缓存方式, 通过 {@link cc.util.cache.NetByteWrapper} 支持HTTP缓存 (注:详细参考cc.util.http包)
 * @author wangcccong
 * @version 1.1406
 * <br> create at: Tues, 10 Jun 2014
 */
public class NetCacheManager {
 
  /** max cache size */
  private long MAX_CACHE_SIZE = 4 * 1024 * 1024;
  private long cacheSize = 0; 
 
  private static NetCacheManager instance = null;
   
  private final ReferenceQueue<NetByteWrapper> referenceQueue;
  private final LinkedHashMap<String, NetByteSoftReference> cacheMap;
   
  private NetCacheManager(){
    referenceQueue = new ReferenceQueue<NetByteWrapper>();
    cacheMap = new LinkedHashMap<String, NetByteSoftReference>(16, 0.75f, true) {
 
      private static final long serialVersionUID = -8378285623387632829L;
      @Override
      protected boolean removeEldestEntry(
          java.util.Map.Entry<String, NetByteSoftReference> eldest) {
        // TODO Auto-generated method stub
        boolean shouldRemove = cacheSize > MAX_CACHE_SIZE;
        if (shouldRemove) {
          cacheSize -= eldest.getValue().getLength();
          System.gc();
        }
        return shouldRemove;
      }
    };
  }
   
  /** singleton model */
  public static synchronized NetCacheManager newInstance(){
    if (Objects.isNull(instance)) {
      instance = new NetCacheManager();
    }
    return instance;
  }
   
  /**
   * reset the memory cache size
   * @param cacheSize
   */
  public void setMaxCacheSize(long cacheSize) {
    this.MAX_CACHE_SIZE = cacheSize;
  }
   
  /**
   * 获取当前内存缓存大小
   * @return
   */
  public long getCacheSize() {
    return cacheSize;
  }
   
  /**
   * 将数据缓存至内存, 如果http返回的数据<b>不支持</b>缓存则采用此方法,缓存的key一般为请求的url
   * @param key
   * @param value
   */
  public void cacheInMemory(String key, byte[] value) {
    this.cacheInMemory(key, value, 0, null);
  }
   
  /**
   * 将数据缓存至内存, 如果http返回的数据<b>支持</b>缓存则采用此方法
   * @param key
   * @param value
   * @param lastModified
   */
  public void cacheInMemory(String key, byte[] value, long lastModified) {
    this.cacheInMemory(key, value, lastModified, null);
  }
   
  /**
   * 将数据缓存至内存, 如果http返回的数据<b>支持</b>缓存则采用此方法
   * @param key
   * @param value
   * @param Etags
   */
  public void cacheInMemory(String key, byte[] value, String Etags) {
    this.cacheInMemory(key, value, 0, Etags);
  }
   
  /**
   * 将数据缓存至内存, 如果http返回的数据<b>支持</b>缓存则采用此方法
   * @param key
   * @param value
   * @param lastModified
   * @param Etags
   */
  private void cacheInMemory(String key, byte[] value, long lastModified, String Etags) {
    Objects.requireNonNull(key, "key must not be null");
    clearRecycledObject();
    NetByteWrapper wrapper = new NetByteWrapper(value, lastModified, Etags);
    NetByteSoftReference byteRef = new NetByteSoftReference(key, wrapper, referenceQueue);
    cacheMap.put(key, byteRef);
    value = null;
    wrapper = null;
  }
   
  /**
   * 缓存至磁盘, 默认不首先缓存到内存
   * @param key
   * @param value
   * @param path
   */
  public void cacheInDisk(String key, byte[] value, String path) {
    cacheInDisk(key, value, path, false);
  }
   
  /**
   * 
   * @param key
   * @param value
   * @param path
   * @param cacheInMemory
   */
  public void cacheInDisk(String key, byte[] value, String path, boolean cacheInMemory) {
    this.cacheInDisk(key, value, 0, null, path, cacheInMemory);
  }
   
  /**
   * 
   * @param key
   * @param value
   * @param lastModified
   * @param Etags
   * @param path
   * @param cacheInMemory
   */
  private void cacheInDisk(String key, byte[] value, long lastModified, String Etags, String path, boolean cacheInMemory) {
    if (cacheInMemory) cacheInMemory(key, value, lastModified, Etags);
    try (FileOutputStream fos = new FileOutputStream(path);
        ObjectOutputStream oos = new ObjectOutputStream(fos)) {
        NetByteWrapper wrapper = new NetByteWrapper(value, lastModified, Etags);
        oos.writeObject(wrapper);
    } catch (Exception e) {
        // TODO: handle exception
      e.printStackTrace();
    }
  }
   
  /**
   * get {@link cc.util.cache.NetByteWrapper} from memory according to key
   * @param key
   * @return {@link cc.util.cache.NetByteWrapper}
   */
  public NetByteWrapper getFromMemory(String key) {
    SoftReference<NetByteWrapper> softReference = cacheMap.get(key);
    return Objects.nonNull(softReference) ? softReference.get() : null;
  }
   
  /**
   * get byte[] from memory according to key
   * @param context
   * @param key
   * @return
   */
  public byte[] getByteFromMemory(String key) {
    NetByteWrapper wrapper = getFromMemory(key);
    return Objects.nonNull(wrapper) ? wrapper.getData() : null;
  }
   
  /**
   * 从磁盘获取数据
   * @param path
   * @return {@link cc.util.cache.NetByteWrapper}
   */
  public NetByteWrapper getFromDisk(String path) {
    try (FileInputStream fis = new FileInputStream(path);
        ObjectInputStream ois = new ObjectInputStream(fis)) {
      NetByteWrapper wrapper = (NetByteWrapper) ois.readObject();
      return wrapper;
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      return null;
    }
  }
   
  /**
   * 采用内存映射的方式从磁盘获取数据(加快读取缓存的大文件)
   * @param path
   * @return
   */
  public NetByteWrapper getFromDiskByMapped(String path) {
    try (FileInputStream fis = new FileInputStream(path);
        FileChannel channel= fis.getChannel();
        ByteArrayOutputStream baos = new ByteArrayOutputStream()){
      MappedByteBuffer mbb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
      byte[] bts = new byte[1024]; 
      int len = (int) channel.size();
      for (int offset = 0; offset < len; offset += 1024) { 
        if (len - offset > 1024) mbb.get(bts);
        else mbb.get((bts = new byte[len - offset])); 
        baos.write(bts);
      } 
      ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
      ObjectInputStream ois = new ObjectInputStream(bais);
      NetByteWrapper wrapper = (NetByteWrapper) ois.readObject();
      bais.close();
      ois.close();
      return wrapper;
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      return null;
    }
  }
   
  /**
   * 从磁盘获取缓存的byte[] 数据
   * @param path
   * @return
   */
  public byte[] getByteFromDisk(String path) {
    NetByteWrapper wrapper = getFromDisk(path);
    return Objects.isNull(wrapper) ? null : wrapper.getData();
  }
   
  /**
   * 通过内存映射放射从磁盘获取缓存的byte[] 数据
   * @param path
   * @return
   */
  public byte[] getByteFromDiskByMapped(String path) {
    NetByteWrapper wrapper = getFromDiskByMapped(path);
    return Objects.isNull(wrapper) ? null : wrapper.getData();
  }
   
  /**
   * calculate the size of the cache memory
   */
  private void clearRecycledObject() {
    NetByteSoftReference ref = null;
    //检测对象是否被回收,如果被回收则从缓存中移除死项
    while (Objects.nonNull((ref = (NetByteSoftReference) referenceQueue.poll()))) {
      cacheMap.remove(ref.getKey());
    }
    cacheSize = 0;
    Iterator<String> keys = cacheMap.keySet().iterator();
    while (keys.hasNext()) {
      cacheSize += cacheMap.get(keys.next()).getLength();
    }
  }
   
  /**
   * clear the memory cache
   */
  public void clearCache() {
    clearRecycledObject();
    cacheMap.clear();
    System.gc();
    System.runFinalization();
  }
   
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

  • Kotlin高阶函数reduce与fold使用实例

    Kotlin高阶函数reduce与fold使用实例

    Kotlin的高阶函数reduce和fold可以用来对集合进行聚合操作。reduce函数将集合元素逐个累加,而fold函数则可以指定一个初始值进行累加。这两个函数在处理大数据集时非常有用
    2023-04-04
  • Flutter质感设计之弹出菜单

    Flutter质感设计之弹出菜单

    这篇文章主要为大家详细介绍了Flutter质感设计之弹出菜单,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • 一篇文章就能了解Rxjava

    一篇文章就能了解Rxjava

    最近偶然接触到了rxjava。关于rxjava,褒贬不一,有人说好用,有人说难用,不过它到底是什么?它有什么用?接下来,本文通过部分代码及分析,向大家介绍rxjava及其相关内容。
    2017-10-10
  • Android Studio配置国内镜像源(利用hosts)

    Android Studio配置国内镜像源(利用hosts)

    这篇文章主要介绍了Android Studio配置国内镜像源(利用hosts),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Android来电拦截的实现方法

    Android来电拦截的实现方法

    这篇文章主要为大家详细介绍了Android来电拦截的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • 基于Android代码实现常用布局

    基于Android代码实现常用布局

    大家在日常中经常见到用xml文件实现android常用布局,但是大家知道如何用代码实现呢?使用代码实现可以帮助我们学习sdk api,所以小编把我日常整理些关于android常用布局代码实现分享给大家
    2015-11-11
  • Kotlin编程基础数据类型示例详解

    Kotlin编程基础数据类型示例详解

    这篇文章主要为大家介绍了Kotlin编程基础数据类型示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Android利用CountDownTimer实现验证码倒计时效果实例

    Android利用CountDownTimer实现验证码倒计时效果实例

    这篇文章主要给大家介绍了关于Android如何利用CountDownTimer实现验证码倒计时效果的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-10-10
  • Android内核源码 在Ubuntu上下载,编译,安装

    Android内核源码 在Ubuntu上下载,编译,安装

    本文主要介绍Android内核源码,想学习深入研究Android的朋友肯定要看看Android内核知识的,这里对下载Android内核源代码的下载,安装,编译做了详细的介绍,有兴趣的小伙伴可以参考下
    2016-08-08
  • Android 多线程处理之多线程详解

    Android 多线程处理之多线程详解

    本文主要介绍Android 多线程处理的知识资料,这里整理下来详细的知识,和简单代码实现和实现效果图,有需要的朋友可以参考下
    2016-09-09

最新评论