Search in sources :

Example 11 with CacheKey

use of com.facebook.cache.common.CacheKey in project fresco by facebook.

the class DiskStorageCacheTest method testHasKeyWithPopulateAtStartupWithoutAwaitingIndex.

@Test
public void testHasKeyWithPopulateAtStartupWithoutAwaitingIndex() throws Exception {
    DiskStorageCache cache2 = createDiskCache(mStorage, true);
    CacheKey key = putOneThingInCache(cache2);
    // A new cache object in the same directory. Equivalent to a process restart.
    // Index may not yet updated.
    assertFalse(cache2.isIndexReady());
    assertTrue(cache2.hasKey(key));
    assertTrue(cache2.hasKeySync(key));
}
Also used : CacheKey(com.facebook.cache.common.CacheKey) MultiCacheKey(com.facebook.cache.common.MultiCacheKey) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Example 12 with CacheKey

use of com.facebook.cache.common.CacheKey in project fresco by facebook.

the class DiskStorageCacheTest method testVersioning.

/**
   * Test to make sure that the same item stored with two different versions
   * of the cache will be stored with two different file names.
   *
   * @throws UnsupportedEncodingException
   */
@Test
public void testVersioning() throws IOException {
    // Define data that will be written to cache
    CacheKey key = new SimpleCacheKey("version_test");
    byte[] value = new byte[32];
    value[0] = 'v';
    // Set up cache with version == 1
    DiskStorage storage1 = createDiskStorage(TESTCACHE_CURRENT_VERSION);
    DiskStorageCache cache1 = createDiskCache(storage1, false);
    // Write test data to cache 1
    cache1.insert(key, WriterCallbacks.from(value));
    // Get cached file
    BinaryResource resource1 = getResource(storage1, key);
    assertNotNull(resource1);
    // Set up cache with version == 2
    DiskStorage storageSupplier2 = createDiskStorage(TESTCACHE_NEXT_VERSION);
    DiskStorageCache cache2 = createDiskCache(storageSupplier2, false);
    // Write test data to cache 2
    cache2.insert(key, WriterCallbacks.from(value));
    // Get cached file
    BinaryResource resource2 = getResource(storageSupplier2, key);
    assertNotNull(resource2);
    // Make sure filenames of the two file are different
    assertFalse(resource2.equals(resource1));
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) CacheKey(com.facebook.cache.common.CacheKey) MultiCacheKey(com.facebook.cache.common.MultiCacheKey) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Example 13 with CacheKey

use of com.facebook.cache.common.CacheKey in project fresco by facebook.

the class ImagePipeline method evictFromDiskCache.

/**
   * Removes all images with the specified {@link Uri} from disk cache.
   *
   * @param imageRequest The imageRequest for the image to evict from disk cache
   */
public void evictFromDiskCache(final ImageRequest imageRequest) {
    CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, null);
    mMainBufferedDiskCache.remove(cacheKey);
    mSmallImageBufferedDiskCache.remove(cacheKey);
}
Also used : CacheKey(com.facebook.cache.common.CacheKey)

Example 14 with CacheKey

use of com.facebook.cache.common.CacheKey in project fresco by facebook.

the class ImagePipeline method isInDiskCache.

/**
   * Returns whether the image is stored in the disk cache.
   *
   * @param imageRequest the imageRequest for the image to be looked up.
   * @return true if the image was found in the disk cache, false otherwise.
   */
public DataSource<Boolean> isInDiskCache(final ImageRequest imageRequest) {
    final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, null);
    final SimpleDataSource<Boolean> dataSource = SimpleDataSource.create();
    mMainBufferedDiskCache.contains(cacheKey).continueWithTask(new Continuation<Boolean, Task<Boolean>>() {

        @Override
        public Task<Boolean> then(Task<Boolean> task) throws Exception {
            if (!task.isCancelled() && !task.isFaulted() && task.getResult()) {
                return Task.forResult(true);
            }
            return mSmallImageBufferedDiskCache.contains(cacheKey);
        }
    }).continueWith(new Continuation<Boolean, Void>() {

        @Override
        public Void then(Task<Boolean> task) throws Exception {
            dataSource.setResult(!task.isCancelled() && !task.isFaulted() && task.getResult());
            return null;
        }
    });
    return dataSource;
}
Also used : Continuation(bolts.Continuation) Task(bolts.Task) CacheKey(com.facebook.cache.common.CacheKey) CancellationException(java.util.concurrent.CancellationException)

Example 15 with CacheKey

use of com.facebook.cache.common.CacheKey in project fresco by facebook.

the class ImagePipeline method isInDiskCacheSync.

/**
   * Performs disk cache check synchronously. It is not recommended to use this
   * unless you know what exactly you are doing. Disk cache check is a costly operation,
   * the call will block the caller thread until the cache check is completed.
   * @param imageRequest the imageRequest for the image to be looked up.
   * @return true if the image was found in the disk cache, false otherwise.
   */
public boolean isInDiskCacheSync(final ImageRequest imageRequest) {
    final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, null);
    final ImageRequest.CacheChoice cacheChoice = imageRequest.getCacheChoice();
    switch(cacheChoice) {
        case DEFAULT:
            return mMainBufferedDiskCache.diskCheckSync(cacheKey);
        case SMALL:
            return mSmallImageBufferedDiskCache.diskCheckSync(cacheKey);
        default:
            return false;
    }
}
Also used : ImageRequest(com.facebook.imagepipeline.request.ImageRequest) CacheKey(com.facebook.cache.common.CacheKey)

Aggregations

CacheKey (com.facebook.cache.common.CacheKey)49 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)31 MultiCacheKey (com.facebook.cache.common.MultiCacheKey)29 Test (org.junit.Test)22 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)19 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)10 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)8 ArrayList (java.util.ArrayList)8 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)7 BinaryResource (com.facebook.binaryresource.BinaryResource)5 BitmapMemoryCacheKey (com.facebook.imagepipeline.cache.BitmapMemoryCacheKey)5 File (java.io.File)5 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Before (org.junit.Before)4 Uri (android.net.Uri)3 FileBinaryResource (com.facebook.binaryresource.FileBinaryResource)3 WriterCallback (com.facebook.cache.common.WriterCallback)3 CloseableReference (com.facebook.common.references.CloseableReference)3 IOException (java.io.IOException)3