Example #1
0
 /** 从缓存中获取图片 */
 public Bitmap getBitmapFromCache(String url) {
   Bitmap bitmap;
   // 先从硬引用缓存中获取
   synchronized (mLruCache) {
     bitmap = mLruCache.get(url);
     if (bitmap != null) {
       // 如果找到的话,把元素移到LinkedHashMap的最前面,从而保证在LRU算法中是最后被删除
       mLruCache.remove(url);
       mLruCache.put(url, bitmap);
       System.out.println("硬引用中找到了");
       return bitmap;
     } else {
       System.out.println("硬引用中没找到");
     }
   }
   // 如果硬引用缓存中找不到,到软引用缓存中找
   synchronized (mSoftCache) {
     SoftReference<Bitmap> bitmapReference = mSoftCache.get(url);
     if (bitmapReference != null) {
       bitmap = bitmapReference.get();
       if (bitmap != null) {
         // 将图片移回硬缓存
         mLruCache.put(url, bitmap);
         mSoftCache.remove(url);
         System.out.println("软引用中找到了");
         return bitmap;
       } else {
         mSoftCache.remove(url);
         System.out.println("软引用中没找到");
       }
     }
   }
   return null;
 }
  /**
   * 从缓存中获取图片
   *
   * @param url
   * @return
   */
  public Bitmap getBitmapFromCache(String url) {
    Bitmap bitmap;
    // 先从硬引用缓存获取数据
    synchronized (mLruCache) {
      bitmap = mLruCache.get(url);
      if (bitmap != null) {
        // 如果找到的话,把元素移到LinkedHashMap的最前面,从而保证在LRU算法中是最后被删除
        mLruCache.remove(url);
        mLruCache.put(url, bitmap);
        return bitmap;
      }
    }

    // 如果硬引用缓存找不到数据,进入软引用缓存
    synchronized (mSoftCache) {
      SoftReference<Bitmap> bitmapReference = mSoftCache.get(url);
      if (bitmapReference != null) {
        bitmap = bitmapReference.get();
        if (bitmap != null) {
          //	将图片移回硬缓存
          mLruCache.put(url, bitmap);
          mSoftCache.remove(url);
          return bitmap;
        } else {
          mSoftCache.remove(url);
        }
      }
    }
    return null;
  }
 // 从缓存中获取Bitmap数据
 public Bitmap getBitmapFromCache(String url) {
   Bitmap bitmap;
   // 加上同步锁保证同一时刻只执行一个对象
   // 从硬引用获得数据
   synchronized (mLruCache) {
     bitmap = mLruCache.get(url);
     if (bitmap != null) {
       // 如果有bitmap对象。则将这个对象从硬引用中先移除在添加。放到前面防止被逐出。
       System.out.println("!!!!-----》命中硬存mLrucache");
       mLruCache.remove(url);
       mLruCache.put(url, bitmap);
       return bitmap;
     }
   }
   // 从软引用获得数据
   synchronized (mSoftCache) {
     SoftReference<Bitmap> bitmapReference = mSoftCache.get(url);
     if (bitmapReference != null) {
       bitmap = bitmapReference.get();
       if (bitmap != null) {
         // 把存在的这个图片提升到硬缓存
         System.out.println("!!!!命中软缓存mSoftCache");
       } else {
         mSoftCache.remove(url);
       }
     }
   }
   return null;
 }
 /**
  * 从缓存中移除bitmap
  *
  * @param key
  * @return
  */
 public static boolean removeBitmapFromMemoryCache(String key) {
   boolean isSuccess = false;
   if (mMemoryCache == null) {
     return isSuccess;
   }
   Bitmap bitmap = mMemoryCache.remove(key);
   if (bitmap != null) {
     isSuccess = true;
   }
   return isSuccess;
 }
  public static void removeThumb(String thumbKey) {
    if (thumbKey == null) {
      return;
    }

    synchronized (sThumbsCache) {
      sThumbsCache.remove(thumbKey);
    }

    sHandler.removeCallbacks(sNotifyChangesRunnable);
    sHandler.post(sNotifyChangesRunnable);
  }
Example #6
0
  public void remove(String url) {
    if (null != mMemoryCache) {
      mMemoryCache.remove(url);
    }

    if (null != mDiskCache) {
      checkNotOnMainThread();

      try {
        mDiskCache.remove(transformUrlForDiskCacheKey(url));
        scheduleDiskCacheFlush();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
Example #7
0
  public CacheItem getFromMemoryCache(final String url, boolean checkExpiration) {
    CacheItem result = null;

    if (null != mMemoryCache) {
      synchronized (mMemoryCache) {
        result = mMemoryCache.get(url);

        // If we get a value, that has expired
        if (null != result && result.getExpiresAt().isBeforeNow() && checkExpiration) {
          mMemoryCache.remove(url);
          result = null;
        }
      }
    }

    return result;
  }
Example #8
0
 /**
  * 根据key删除MemoryCache的图片缓存
  *
  * @param url
  */
 public void remove(String url) {
   mLruCache.remove(url);
   mSoftCache.remove(url);
 }
 public Bitmap remove(String url) {
   return cache.remove(url);
 }
 @Override
 public void remove(String key) {
   mResponseCache.remove(key);
 }