Android Open Source - SIC Disk Cache






From Project

Back to project page SIC.

License

The source code is released under:

MIT License

If you think the Android project SIC listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.sun.imageloader.cache.impl;
// ww w . j  a  v  a2s.co m
import java.io.File;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.Calendar;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import com.sun.imageloader.cache.api.MemoryCache;
import com.sun.imageloader.core.ImageKey;
import com.sun.imageloader.utils.KeyUtils;
import com.sun.imageloader.utils.L;

public class DiskCache implements MemoryCache<ImageKey, File> {

  private static final String TAG = DiskCache.class.getName();
  private File _diskCacheLocationDir;
  private final ConcurrentMap<ImageKey, Reference<File>> _imageFiles;
  private final long _maxLastModifiedTime;
  private final TimeUnit _timeUnitToUse;

  /**
   * 
   * {@linkplain DiskCache} is used to maintain a soft cache of {@linkplain File} objects which are associated to an {@linkplain ImageKey}. 
   * This allows images to be retrieves easily from the disk according to the {@linkplain ImageKey}.    
   * 
   * Pass in 0 for maxLastModifiedTime_, if files should not be deleted
   * 
   * @param diskCacheLocationDir_
   *       The {@linkplain File} object containing the directory to cache  {@linkplain Bitmap} objects from
   * @param maxFilesToCache_
   *       The maximum number of files to cache
   * @param shouldLoadAllFilesFromDisk_
   *       whether to load all image files into cache on startup
   * @param maxLastModifiedTime_
   *       the max time allowed to keep files from when the file was created and the current date
   * @param timeUnitToUse_
   *       the {@link TimeUnit} to use in conjunction with the maxLastModifiedTime_, to see if a {@link File} should be 
   *      removed
   */
  public DiskCache(File diskCacheLocationDir_, int maxFilesToCache_,
      boolean shouldLoadAllFilesFromDisk_, long maxLastModifiedTime_, TimeUnit timeUnitToUse_) {
    _diskCacheLocationDir = diskCacheLocationDir_;
    _imageFiles = new ConcurrentHashMap<ImageKey, Reference<File>>(0, 0.75f);
    _maxLastModifiedTime = maxLastModifiedTime_;
    _timeUnitToUse = timeUnitToUse_;
    if (shouldLoadAllFilesFromDisk_)
      loadImagesFromDisk(_imageFiles);
  }

  /**
   * Load {@linkplain File} objects and fill internal cache will these objects
   * 
   * @param imageFiles_ 
   *       a {@linkplain ConcurrentMap} to add all {@linkplain File} objects into
   */
  private void loadImagesFromDisk(
      ConcurrentMap<ImageKey, Reference<File>> imageFiles_) {

    if(!_diskCacheLocationDir.exists()){
      L.v(TAG, "no dir exists for: " + _diskCacheLocationDir.getAbsolutePath());      
      if( _diskCacheLocationDir.mkdirs())
        L.v(TAG, "Successfully created new cache disk location: " + _diskCacheLocationDir.getAbsolutePath());      
    }
    
    String[] extentions = new String[] { ".JPG", ".JPEG", ".PNG" };

    final File[] imageFiles = _diskCacheLocationDir
        .listFiles(new ImageFileFilter(extentions));

    new Thread(new Runnable() {

      @Override
      public void run() {
        if (imageFiles != null) {
          long currentTime = Calendar.getInstance().getTimeInMillis();
          for (File imageFile : imageFiles) {
            if (imageFile != null && imageFile.exists()){
              long lastModifiedTime =  TimeUnit.MILLISECONDS.convert(_maxLastModifiedTime, _timeUnitToUse);
              long finalTime = lastModifiedTime + imageFile.lastModified();
              if(finalTime < currentTime && _maxLastModifiedTime > 0){
                if(!imageFile.delete()){
                  L.w(TAG, "Cannot delete file with path : " + imageFile.getAbsolutePath());
                }
              }else{
                ImageKey key=KeyUtils.createImageKey(imageFile.getName());
                if(key.hasValidKey()){
                  _imageFiles.put(key,
                      new SoftReference<File>(imageFile));
                  L.v(TAG, imageFile.getAbsolutePath());
                }        
              }
            }
          }
        }
      }
    }).start();
  }

  @Override
  public boolean put(ImageKey key_, File value) {

    _imageFiles.put(key_,new SoftReference<File>(value));

    return true;
  }

  @Override
  public File getValue(ImageKey key_) {
    Reference<File> imageFile = _imageFiles.get(key_);
      
    if (imageFile != null) {
      return imageFile.get();
    }
    
    return null;
  }
  
  public File getNewImageFile(ImageKey key_){
    File imageFile = new File(_diskCacheLocationDir, key_.getImageFilename()); 
    _imageFiles.put(key_, new SoftReference<File>(imageFile));
    return imageFile;
  }

  /**
   * This implementation will remove the image file from both the internal map
   * and also the disk
   */
  @Override
  public void remove(ImageKey key_, boolean removeAllOccurences_) {
    File imageFileRef = remove(key_);
    File imageFileToRemove = imageFileRef;
    if (imageFileToRemove != null && removeAllOccurences_) {
      boolean sta = imageFileToRemove.delete();
      L.v(TAG,"Deleting file in disk, attempted to delete file with status: "  + sta);
    }

  }

  private File remove(ImageKey key_) {
    return _imageFiles.remove(key_).get();
  }

  /**
   * Clear the internal cache of files
   */
  @Override
  public void clear() {
    _imageFiles.clear();
  }

  @Override
  public Collection<ImageKey> getKeys() {

    Set<ImageKey> keys = _imageFiles.keySet();

    return keys;
  }



}




Java Source Code List

com.sun.imageloader.cache.api.MemoryCache.java
com.sun.imageloader.cache.impl.DiskCache.java
com.sun.imageloader.cache.impl.ImageFileFilter.java
com.sun.imageloader.cache.impl.LRUCache.java
com.sun.imageloader.cache.impl.SoftCache.java
com.sun.imageloader.computable.impl.ComputableImage.java
com.sun.imageloader.computable.impl.Computable.java
com.sun.imageloader.concurrent.ComputableCallable.java
com.sun.imageloader.concurrent.DisplayImageTask.java
com.sun.imageloader.concurrent.ImageLoaderTask.java
com.sun.imageloader.core.FlingLock.java
com.sun.imageloader.core.ImageKey.java
com.sun.imageloader.core.ImagePreferences.java
com.sun.imageloader.core.ImageSettings.java
com.sun.imageloader.core.ImageWriter.java
com.sun.imageloader.core.SimpleImageListenerImpl.java
com.sun.imageloader.core.UrlImageLoaderConfiguration.java
com.sun.imageloader.core.UrlImageLoader.java
com.sun.imageloader.core.UrlImageTaskExecutor.java
com.sun.imageloader.core.api.FailedTaskReason.java
com.sun.imageloader.core.api.ImageFailListenter.java
com.sun.imageloader.core.api.ImageTaskListener.java
com.sun.imageloader.core.api.Settings.java
com.sun.imageloader.downloader.api.ImageRetriever.java
com.sun.imageloader.downloader.impl.ImageDownloader.java
com.sun.imageloader.downloader.impl.ImageRetrieverFactory.java
com.sun.imageloader.downloader.impl.Scheme.java
com.sun.imageloader.imagedecoder.api.ImageDecoder.java
com.sun.imageloader.imagedecoder.impl.SimpleImageDecoder.java
com.sun.imageloader.memorizer.api.AMemorizer.java
com.sun.imageloader.memorizer.api.BitmapMemorizer.java
com.sun.imageloader.memorizer.api.IMemorizer.java
com.sun.imageloader.memorizer.api.InterruptedImageLoadException.java
com.sun.imageloader.utils.KeyUtils.java
com.sun.imageloader.utils.L.java
com.sun.imageloader.utils.ViewUtils.java