文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

android实现缓存图片等数据

2022-06-06 09:57

关注

采用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;

public class NetByteWrapper implements Serializable {
  private final static long serialVersionUID = 1L;
  
  private byte[] data;
  
  int contentLength;
  
  private long lastModified;
  
  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;

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;

public class NetCacheManager {
  
  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;
      }
    };
  }
  
  public static synchronized NetCacheManager newInstance(){
    if (Objects.isNull(instance)) {
      instance = new NetCacheManager();
    }
    return instance;
  }
  
  public void setMaxCacheSize(long cacheSize) {
    this.MAX_CACHE_SIZE = cacheSize;
  }
  
  public long getCacheSize() {
    return cacheSize;
  }
  
  public void cacheInMemory(String key, byte[] value) {
    this.cacheInMemory(key, value, 0, null);
  }
  
  public void cacheInMemory(String key, byte[] value, long lastModified) {
    this.cacheInMemory(key, value, lastModified, null);
  }
  
  public void cacheInMemory(String key, byte[] value, String Etags) {
    this.cacheInMemory(key, value, 0, 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;
  }
  
  public void cacheInDisk(String key, byte[] value, String path) {
    cacheInDisk(key, value, path, false);
  }
  
  public void cacheInDisk(String key, byte[] value, String path, boolean cacheInMemory) {
    this.cacheInDisk(key, value, 0, null, path, 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();
    }
  }
  
  public NetByteWrapper getFromMemory(String key) {
    SoftReference<NetByteWrapper> softReference = cacheMap.get(key);
    return Objects.nonNull(softReference) ? softReference.get() : null;
  }
  
  public byte[] getByteFromMemory(String key) {
    NetByteWrapper wrapper = getFromMemory(key);
    return Objects.nonNull(wrapper) ? wrapper.getData() : null;
  }
  
  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;
    }
  }
  
  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;
    }
  }
  
  public byte[] getByteFromDisk(String path) {
    NetByteWrapper wrapper = getFromDisk(path);
    return Objects.isNull(wrapper) ? null : wrapper.getData();
  }
  
  public byte[] getByteFromDiskByMapped(String path) {
    NetByteWrapper wrapper = getFromDiskByMapped(path);
    return Objects.isNull(wrapper) ? null : wrapper.getData();
  }
  
  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();
    }
  }
  
  public void clearCache() {
    clearRecycledObject();
    cacheMap.clear();
    System.gc();
    System.runFinalization();
  }
}

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

您可能感兴趣的文章:Android图片缓存之Lru算法(二)Android图片缓存之Bitmap详解(一)Android中Glide加载库的图片缓存配置究极指南android中图片的三级缓存cache策略(内存/文件/网络)android异步加载图片并缓存到本地实现方法Android中使用二级缓存、异步加载批量加载图片完整案例android上的一个网络接口和图片缓存框架enif简析Android开发笔记之图片缓存、手势及OOM分析Android使用缓存机制实现文件下载及异步请求图片加三级缓存Android图片缓存之初识Glide(三)


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯