最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android實現(xiàn)緩存圖片等數(shù)據(jù)

 更新時間:2015年07月31日 10:11:42   投稿:hebedich  
本文給大家分享的是Android采用LinkedHashMap自帶的LRU 算法緩存數(shù)據(jù)的方法和示例,有需要的小伙伴可以參考下。

采用LinkedHashMap自帶的LRU 算法緩存數(shù)據(jù), 可檢測對象是否已被虛擬機回收,并且重新計算當(dāng)前緩存大小,清除緩存中無用的鍵值對象(即已經(jīng)被虛擬機回收但未從緩存清除的數(shù)據(jù));
 * 默認(rèn)內(nèi)存緩存大小為: 4 * 1024 * 1024 可通過通過setMaxCacheSize重新設(shè)置緩存大小,可手動清空內(nèi)存緩存
 * <br>支持內(nèi)存緩存和磁盤緩存方式, 通過 {@link cc.util.cache.NetByteWrapper} 支持HTTP緩存 (注:詳細(xì)參考cc.util.http包); 注:使用JDK7

package cc.util.cache;
 
import java.io.Serializable;
import java.util.Objects;
 
/**
  封裝網(wǎng)絡(luò)數(shù)據(jù), 將數(shù)據(jù)的Etag、lastModified獲取到, 下次請求的時候提取出來到服務(wù)器比對
 * 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;
 
/**采用軟引用方式將數(shù)據(jù)存放起來
 * 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 算法緩存數(shù)據(jù), 可檢測對象是否已被虛擬機回收,并且重新計算當(dāng)前緩存大小,清除緩存中無用的鍵值對象(即已經(jīng)被虛擬機回收但未從緩存清除的數(shù)據(jù));
 * 默認(rèn)內(nèi)存緩存大小為: 4 * 1024 * 1024 可通過通過setMaxCacheSize重新設(shè)置緩存大小,可手動清空內(nèi)存緩存,支持采用內(nèi)存映射方式讀取緩存
 * <br>支持內(nèi)存緩存和磁盤緩存方式, 通過 {@link cc.util.cache.NetByteWrapper} 支持HTTP緩存 (注:詳細(xì)參考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;
  }
   
  /**
   * 獲取當(dāng)前內(nèi)存緩存大小
   * @return
   */
  public long getCacheSize() {
    return cacheSize;
  }
   
  /**
   * 將數(shù)據(jù)緩存至內(nèi)存, 如果http返回的數(shù)據(jù)<b>不支持</b>緩存則采用此方法,緩存的key一般為請求的url
   * @param key
   * @param value
   */
  public void cacheInMemory(String key, byte[] value) {
    this.cacheInMemory(key, value, 0, null);
  }
   
  /**
   * 將數(shù)據(jù)緩存至內(nèi)存, 如果http返回的數(shù)據(jù)<b>支持</b>緩存則采用此方法
   * @param key
   * @param value
   * @param lastModified
   */
  public void cacheInMemory(String key, byte[] value, long lastModified) {
    this.cacheInMemory(key, value, lastModified, null);
  }
   
  /**
   * 將數(shù)據(jù)緩存至內(nèi)存, 如果http返回的數(shù)據(jù)<b>支持</b>緩存則采用此方法
   * @param key
   * @param value
   * @param Etags
   */
  public void cacheInMemory(String key, byte[] value, String Etags) {
    this.cacheInMemory(key, value, 0, Etags);
  }
   
  /**
   * 將數(shù)據(jù)緩存至內(nèi)存, 如果http返回的數(shù)據(jù)<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;
  }
   
  /**
   * 緩存至磁盤, 默認(rèn)不首先緩存到內(nèi)存
   * @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;
  }
   
  /**
   * 從磁盤獲取數(shù)據(jù)
   * @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;
    }
  }
   
  /**
   * 采用內(nèi)存映射的方式從磁盤獲取數(shù)據(jù)(加快讀取緩存的大文件)
   * @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[] 數(shù)據(jù)
   * @param path
   * @return
   */
  public byte[] getByteFromDisk(String path) {
    NetByteWrapper wrapper = getFromDisk(path);
    return Objects.isNull(wrapper) ? null : wrapper.getData();
  }
   
  /**
   * 通過內(nèi)存映射放射從磁盤獲取緩存的byte[] 數(shù)據(jù)
   * @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();
  }
   
}

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

相關(guān)文章

  • Kotlin高階函數(shù)reduce與fold使用實例

    Kotlin高階函數(shù)reduce與fold使用實例

    Kotlin的高階函數(shù)reduce和fold可以用來對集合進行聚合操作。reduce函數(shù)將集合元素逐個累加,而fold函數(shù)則可以指定一個初始值進行累加。這兩個函數(shù)在處理大數(shù)據(jù)集時非常有用
    2023-04-04
  • Flutter質(zhì)感設(shè)計之彈出菜單

    Flutter質(zhì)感設(shè)計之彈出菜單

    這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計之彈出菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 一篇文章就能了解Rxjava

    一篇文章就能了解Rxjava

    最近偶然接觸到了rxjava。關(guān)于rxjava,褒貶不一,有人說好用,有人說難用,不過它到底是什么?它有什么用?接下來,本文通過部分代碼及分析,向大家介紹rxjava及其相關(guān)內(nèi)容。
    2017-10-10
  • Android Studio配置國內(nèi)鏡像源(利用hosts)

    Android Studio配置國內(nèi)鏡像源(利用hosts)

    這篇文章主要介紹了Android Studio配置國內(nèi)鏡像源(利用hosts),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Android來電攔截的實現(xiàn)方法

    Android來電攔截的實現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了Android來電攔截的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • 基于Android代碼實現(xiàn)常用布局

    基于Android代碼實現(xiàn)常用布局

    大家在日常中經(jīng)常見到用xml文件實現(xiàn)android常用布局,但是大家知道如何用代碼實現(xiàn)呢?使用代碼實現(xiàn)可以幫助我們學(xué)習(xí)sdk api,所以小編把我日常整理些關(guān)于android常用布局代碼實現(xiàn)分享給大家
    2015-11-11
  • Kotlin編程基礎(chǔ)數(shù)據(jù)類型示例詳解

    Kotlin編程基礎(chǔ)數(shù)據(jù)類型示例詳解

    這篇文章主要為大家介紹了Kotlin編程基礎(chǔ)數(shù)據(jù)類型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Android利用CountDownTimer實現(xiàn)驗證碼倒計時效果實例

    Android利用CountDownTimer實現(xiàn)驗證碼倒計時效果實例

    這篇文章主要給大家介紹了關(guān)于Android如何利用CountDownTimer實現(xiàn)驗證碼倒計時效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • Android內(nèi)核源碼 在Ubuntu上下載,編譯,安裝

    Android內(nèi)核源碼 在Ubuntu上下載,編譯,安裝

    本文主要介紹Android內(nèi)核源碼,想學(xué)習(xí)深入研究Android的朋友肯定要看看Android內(nèi)核知識的,這里對下載Android內(nèi)核源代碼的下載,安裝,編譯做了詳細(xì)的介紹,有興趣的小伙伴可以參考下
    2016-08-08
  • Android 多線程處理之多線程詳解

    Android 多線程處理之多線程詳解

    本文主要介紹Android 多線程處理的知識資料,這里整理下來詳細(xì)的知識,和簡單代碼實現(xiàn)和實現(xiàn)效果圖,有需要的朋友可以參考下
    2016-09-09

最新評論

和田市| 临夏县| 永平县| 清镇市| 延吉市| 肇源县| 汝州市| 色达县| 古丈县| 利川市| 安仁县| 华阴市| 个旧市| 桃江县| 蛟河市| 卢氏县| 封丘县| 凉城县| 梓潼县| 萍乡市| 康保县| 青龙| 三穗县| 昂仁县| 美姑县| 威海市| 板桥市| 京山县| 阿鲁科尔沁旗| 乌审旗| 万州区| 漳州市| 柯坪县| 北票市| 武宁县| 武冈市| 开阳县| 阿瓦提县| 寿光市| 海南省| 潮安县|