Search in sources :

Example 1 with CacheKey

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

the class SmallCacheIfRequestedDiskCachePolicyTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    List<CacheKey> keys = new ArrayList<>(1);
    keys.add(new SimpleCacheKey("http://dummy.uri"));
    mCacheKey = new MultiCacheKey(keys);
    when(mCacheKeyFactory.getEncodedCacheKey(mImageRequest, mCallerContext)).thenReturn(mCacheKey);
    when(mImageRequest.getCacheChoice()).thenReturn(ImageRequest.CacheChoice.DEFAULT);
    when(mImageRequest.isDiskCacheEnabled()).thenReturn(true);
    mIsCancelled = new AtomicBoolean(false);
    mSmallCacheIfRequestedDiskCachePolicy = new SmallCacheIfRequestedDiskCachePolicy(mDefaultBufferedDiskCache, mSmallImageBufferedDiskCache, mCacheKeyFactory);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiCacheKey(com.facebook.cache.common.MultiCacheKey) ArrayList(java.util.ArrayList) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) CacheKey(com.facebook.cache.common.CacheKey) MultiCacheKey(com.facebook.cache.common.MultiCacheKey) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) Before(org.junit.Before)

Example 2 with CacheKey

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

the class SplitCachesByImageSizeDiskCachePolicy method createAndStartCacheReadTask.

@Override
public Task<EncodedImage> createAndStartCacheReadTask(ImageRequest imageRequest, Object callerContext, final AtomicBoolean isCancelled) {
    final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, callerContext);
    final boolean alreadyInSmall = mSmallImageBufferedDiskCache.containsSync(cacheKey);
    final boolean alreadyInMain = mDefaultBufferedDiskCache.containsSync(cacheKey);
    final BufferedDiskCache firstCache;
    final BufferedDiskCache secondCache;
    if (alreadyInSmall || !alreadyInMain) {
        firstCache = mSmallImageBufferedDiskCache;
        secondCache = mDefaultBufferedDiskCache;
    } else {
        firstCache = mDefaultBufferedDiskCache;
        secondCache = mSmallImageBufferedDiskCache;
    }
    return firstCache.get(cacheKey, isCancelled).continueWithTask(new Continuation<EncodedImage, Task<EncodedImage>>() {

        @Override
        public Task<EncodedImage> then(Task<EncodedImage> task) throws Exception {
            if (isTaskCancelled(task) || (!task.isFaulted() && task.getResult() != null)) {
                return task;
            }
            return secondCache.get(cacheKey, isCancelled);
        }
    });
}
Also used : Task(bolts.Task) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) CacheKey(com.facebook.cache.common.CacheKey) CancellationException(java.util.concurrent.CancellationException)

Example 3 with CacheKey

use of com.facebook.cache.common.CacheKey in project remusic by aa112901.

the class PlaylistActivity method setAlbumart.

private void setAlbumart() {
    playlistTitleView.setText(playlistName);
    if (albumPath == null) {
        albumArtSmall.setImageResource(R.drawable.placeholder_disk_210);
    } else {
        albumArtSmall.setImageURI(Uri.parse(albumPath));
    }
    try {
        if (isLocalPlaylist && !URLUtil.isNetworkUrl(albumPath)) {
            new setBlurredAlbumArt().execute(ImageUtils.getArtworkQuick(PlaylistActivity.this, Uri.parse(albumPath), 300, 300));
            L.D(d, TAG, "albumpath = " + albumPath);
        } else {
            // drawable = Drawable.createFromStream( new URL(albumPath).openStream(),"src");
            ImageRequest imageRequest = ImageRequest.fromUri(albumPath);
            CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(imageRequest);
            BinaryResource resource = ImagePipelineFactory.getInstance().getMainDiskStorageCache().getResource(cacheKey);
            File file = ((FileBinaryResource) resource).getFile();
            if (file != null)
                new setBlurredAlbumArt().execute(ImageUtils.getArtworkQuick(file, 300, 300));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) File(java.io.File) CacheKey(com.facebook.cache.common.CacheKey) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource)

Example 4 with CacheKey

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

the class DiskStorageCacheTest method testCacheEventListener.

@Test
public void testCacheEventListener() throws Exception {
    // 1. Add first cache file
    CacheKey key1 = new SimpleCacheKey("foo");
    int value1Size = 101;
    byte[] value1 = new byte[value1Size];
    // just so it's not all zeros for the equality test below.
    value1[80] = 'c';
    BinaryResource resource1 = mCache.insert(key1, WriterCallbacks.from(value1));
    verifyListenerOnWriteAttempt(key1);
    String resourceId1 = verifyListenerOnWriteSuccessAndGetResourceId(key1, value1Size);
    BinaryResource resource1Again = mCache.getResource(key1);
    assertEquals(resource1, resource1Again);
    verifyListenerOnHit(key1, resourceId1);
    BinaryResource resource1Again2 = mCache.getResource(key1);
    assertEquals(resource1, resource1Again2);
    verifyListenerOnHit(key1, resourceId1);
    SimpleCacheKey missingKey = new SimpleCacheKey("nonexistent_key");
    BinaryResource res2 = mCache.getResource(missingKey);
    assertNull(res2);
    verifyListenerOnMiss(missingKey);
    mCache.clearAll();
    verify(mCacheEventListener).onCleared();
    verifyNoMoreInteractions(mCacheEventListener);
}
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 5 with CacheKey

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

the class DiskStorageCacheTest method testHasKeyWithoutPopulateAtStartupWithAwaitingIndex.

@Test
public void testHasKeyWithoutPopulateAtStartupWithAwaitingIndex() throws Exception {
    // A new cache object in the same directory. Equivalent to a process restart.
    // Index may not yet updated.
    DiskStorageCache cache2 = createDiskCache(mStorage, false);
    CacheKey key = putOneThingInCache();
    // Wait for index populated in cache before use of cache
    cache2.awaitIndex();
    assertTrue(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)

Aggregations

CacheKey (com.facebook.cache.common.CacheKey)56 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)13 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)10 ArrayList (java.util.ArrayList)8 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 BinaryResource (com.facebook.binaryresource.BinaryResource)5 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)5 BitmapMemoryCacheKey (com.facebook.imagepipeline.cache.BitmapMemoryCacheKey)5 File (java.io.File)5 Before (org.junit.Before)5 Uri (android.net.Uri)4 Nullable (javax.annotation.Nullable)4 FileBinaryResource (com.facebook.binaryresource.FileBinaryResource)3 WriterCallback (com.facebook.cache.common.WriterCallback)3 CloseableReference (com.facebook.common.references.CloseableReference)3