Search in sources :

Example 31 with CacheKey

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

the class DiskStorageCacheTest method testDoesntHaveKey.

@Test
public void testDoesntHaveKey() {
    CacheKey key = new SimpleCacheKey("foo");
    assertFalse(mCache.hasKeySync(key));
    assertFalse(mCache.hasKey(key));
}
Also used : 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 32 with CacheKey

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

the class DiskStorageCacheTest method testHasKeyWithPopulateAtStartupWithAwaitingIndex.

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

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

the class DiskStorageCacheTest method testRemoveFileClearsIndex.

@Test
public void testRemoveFileClearsIndex() throws Exception {
    CacheKey key = putOneThingInCache();
    mStorage.clearAll();
    assertNull(mCache.getResource(key));
    assertFalse(mCache.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 34 with CacheKey

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

the class DiskStorageCacheTest method testCleanOldCache.

@Test
public void testCleanOldCache() throws IOException, NoSuchFieldException, IllegalAccessException {
    long cacheExpirationMs = TimeUnit.DAYS.toMillis(5);
    CacheKey key1 = new SimpleCacheKey("aaa");
    int value1Size = 41;
    byte[] value1 = new byte[value1Size];
    value1[25] = 'a';
    mCache.insert(key1, WriterCallbacks.from(value1));
    String resourceId1 = verifyListenerOnWriteSuccessAndGetResourceId(key1, value1Size);
    CacheKey key2 = new SimpleCacheKey("bbb");
    int value2Size = 42;
    byte[] value2 = new byte[value2Size];
    value2[25] = 'b';
    mCache.insert(key2, WriterCallbacks.from(value2));
    String resourceId2 = verifyListenerOnWriteSuccessAndGetResourceId(key2, value2Size);
    // Increment clock by default expiration time + 1 day
    when(mClock.now()).thenReturn(cacheExpirationMs + TimeUnit.DAYS.toMillis(1));
    CacheKey key3 = new SimpleCacheKey("ccc");
    int value3Size = 43;
    byte[] value3 = new byte[value3Size];
    value3[25] = 'c';
    mCache.insert(key3, WriterCallbacks.from(value3));
    long valueAge3 = TimeUnit.HOURS.toMillis(1);
    when(mClock.now()).thenReturn(cacheExpirationMs + TimeUnit.DAYS.toMillis(1) + valueAge3);
    long oldestEntry = mCache.clearOldEntries(cacheExpirationMs);
    assertEquals(valueAge3, oldestEntry);
    assertArrayEquals(value3, getContents(getResource(key3)));
    assertNull(getResource(key1));
    assertNull(getResource(key2));
    String[] resourceIds = new String[] { resourceId1, resourceId2 };
    long[] itemSizes = new long[] { value1Size, value2Size };
    long cacheSizeBeforeEviction = value1Size + value2Size + value3Size;
    verifyListenerOnEviction(resourceIds, itemSizes, CacheEventListener.EvictionReason.CONTENT_STALE, cacheSizeBeforeEviction);
}
Also used : 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 35 with CacheKey

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

the class DiskStorageCacheTest method testSizeEvictionClearsIndex.

@Test
public void testSizeEvictionClearsIndex() throws Exception {
    when(mClock.now()).thenReturn(TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));
    CacheKey key1 = putOneThingInCache();
    CacheKey key2 = new SimpleCacheKey("bar");
    CacheKey key3 = new SimpleCacheKey("duck");
    byte[] value2 = new byte[(int) FILE_CACHE_MAX_SIZE_HIGH_LIMIT];
    value2[80] = 'c';
    WriterCallback callback = WriterCallbacks.from(value2);
    when(mClock.now()).thenReturn(TimeUnit.MILLISECONDS.convert(2, TimeUnit.DAYS));
    mCache.insert(key2, callback);
    // now over limit. Next write will evict key1
    when(mClock.now()).thenReturn(TimeUnit.MILLISECONDS.convert(3, TimeUnit.DAYS));
    mCache.insert(key3, callback);
    assertFalse(mCache.hasKeySync(key1));
    assertFalse(mCache.hasKey(key1));
    assertTrue(mCache.hasKeySync(key3));
    assertTrue(mCache.hasKey(key3));
}
Also used : WriterCallback(com.facebook.cache.common.WriterCallback) 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)

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