Search in sources :

Example 16 with SimpleCacheKey

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

the class DiskStorageCacheTest method testCacheFile.

/**
   * Tests size based file eviction of cache files. Also tests that unexpected
   * files (which are not in the format expected by the cache) do not count
   * towards the cache size, and are also evicted during both evictions (LRU and Old).
   *
   * @throws Exception
   */
@Test
public void testCacheFile() throws Exception {
    if (!mCacheDirectory.exists() && !mCacheDirectory.mkdirs()) {
        throw new RuntimeException("Cannot create cache dir");
    }
    // Write non-cache, non-lru file in the cache directory
    File unexpected1 = new File(mCacheDirectory, "unexpected1");
    RandomAccessFile rf1 = new RandomAccessFile(unexpected1, "rw");
    rf1.setLength(110);
    // Touch the non-cache, non-lru file, and assert that it succeeds.
    when(mClock.now()).thenReturn(TimeUnit.HOURS.toMillis(1));
    assertTrue(unexpected1.setLastModified(mClock.now()));
    // 1. Add first cache file
    CacheKey key1 = new SimpleCacheKey("foo");
    byte[] value1 = new byte[101];
    // just so it's not all zeros for the equality test below.
    value1[80] = 'c';
    mCache.insert(key1, WriterCallbacks.from(value1));
    // verify resource
    assertArrayEquals(value1, getContents(getResource(key1)));
    // 1. Touch the LRU file, and assert that it succeeds.
    when(mClock.now()).thenReturn(TimeUnit.HOURS.toMillis(2));
    assertTrue(mCache.probe(key1));
    // The cache size should be the size of the first file only
    // The unexpected files should not count towards size
    assertTrue(mCache.getSize() == 101);
    // Write another non-cache, non-lru file in the cache directory
    File unexpected2 = new File(mCacheDirectory, "unexpected2");
    RandomAccessFile rf2 = new RandomAccessFile(unexpected2, "rw");
    rf2.setLength(120);
    // Touch the non-cache, non-lru file, and assert that it succeeds.
    when(mClock.now()).thenReturn(TimeUnit.HOURS.toMillis(3));
    assertTrue(unexpected2.setLastModified(mClock.now()));
    // 2. Add second cache file
    CacheKey key2 = new SimpleCacheKey("bar");
    byte[] value2 = new byte[102];
    // just so it's not all zeros for the equality test below.
    value2[80] = 'd';
    mCache.insert(key2, WriterCallbacks.from(value2));
    // 2. Touch the LRU file, and assert that it succeeds.
    when(mClock.now()).thenReturn(TimeUnit.HOURS.toMillis(4));
    assertTrue(mCache.probe(key2));
    // The cache size should be the size of the first + second cache files
    // The unexpected files should not count towards size
    assertTrue(mCache.getSize() == 203);
    // At this point, the filecache size has exceeded
    // FILE_CACHE_MAX_SIZE_HIGH_LIMIT. However, eviction will be triggered
    // only when the next value will be inserted (to be more particular,
    // before the next value is inserted).
    // 3. Add third cache file
    CacheKey key3 = new SimpleCacheKey("foobar");
    byte[] value3 = new byte[103];
    // just so it's not all zeros for the equality test below.
    value3[80] = 'e';
    mCache.insert(key3, WriterCallbacks.from(value3));
    // At this point, the first file should have been evicted. Only the
    // files associated with the second and third entries should be in cache.
    // 1. Verify that the first cache, lru files are deleted
    assertNull(getResource(key1));
    // Verify the first unexpected file is deleted, but that eviction stops
    // before the second unexpected file
    assertFalse(unexpected1.exists());
    assertFalse(unexpected2.exists());
    // 2. Verify the second cache, lru files exist
    assertArrayEquals(value2, getContents(getResource(key2)));
    // 3. Verify that cache, lru files for third entry still exists
    assertArrayEquals(value3, getContents(getResource(key3)));
    // The cache size should be the size of the second + third files
    assertTrue(String.format(Locale.US, "Expected cache size of %d but is %d", 205, mCache.getSize()), mCache.getSize() == 205);
    // Write another non-cache, non-lru file in the cache directory
    File unexpected3 = new File(mCacheDirectory, "unexpected3");
    RandomAccessFile rf3 = new RandomAccessFile(unexpected3, "rw");
    rf3.setLength(120);
    assertTrue(unexpected3.exists());
    // After a clear, cache file size should be uninitialized (-1)
    mCache.clearAll();
    assertEquals(-1, mCache.getSize());
    assertFalse(unexpected3.exists());
    assertNull(getResource(key2));
    assertNull(getResource(key3));
}
Also used : RandomAccessFile(java.io.RandomAccessFile) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) 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 17 with SimpleCacheKey

use of com.facebook.cache.common.SimpleCacheKey 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 18 with SimpleCacheKey

use of com.facebook.cache.common.SimpleCacheKey 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 19 with SimpleCacheKey

use of com.facebook.cache.common.SimpleCacheKey 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)

Example 20 with SimpleCacheKey

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

the class EncodedImageTest method testCloneOrNull.

@Test
public void testCloneOrNull() {
    EncodedImage encodedImage = new EncodedImage(mByteBufferRef);
    encodedImage.setImageFormat(DefaultImageFormats.JPEG);
    encodedImage.setRotationAngle(0);
    encodedImage.setWidth(1);
    encodedImage.setHeight(2);
    encodedImage.setSampleSize(4);
    encodedImage.setEncodedCacheKey(new SimpleCacheKey("key"));
    EncodedImage encodedImage2 = EncodedImage.cloneOrNull(encodedImage);
    assertEquals(3, mByteBufferRef.getUnderlyingReferenceTestOnly().getRefCountTestOnly());
    assertSame(encodedImage.getByteBufferRef().getUnderlyingReferenceTestOnly(), encodedImage2.getByteBufferRef().getUnderlyingReferenceTestOnly());
    assertEquals(encodedImage.getImageFormat(), encodedImage2.getImageFormat());
    assertEquals(encodedImage.getRotationAngle(), encodedImage2.getRotationAngle());
    assertEquals(encodedImage.getHeight(), encodedImage2.getHeight());
    assertEquals(encodedImage.getWidth(), encodedImage2.getWidth());
    assertEquals(encodedImage.getSampleSize(), encodedImage2.getSampleSize());
    assertEquals(encodedImage.getEncodedCacheKey(), encodedImage2.getEncodedCacheKey());
    encodedImage = new EncodedImage(mInputStreamSupplier, 100);
    encodedImage.setImageFormat(DefaultImageFormats.JPEG);
    encodedImage.setRotationAngle(0);
    encodedImage.setWidth(1);
    encodedImage.setHeight(2);
    encodedImage2 = EncodedImage.cloneOrNull(encodedImage);
    assertSame(encodedImage.getInputStream(), encodedImage2.getInputStream());
    assertEquals(encodedImage2.getSize(), encodedImage.getSize());
}
Also used : SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey)

Aggregations

SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)26 CacheKey (com.facebook.cache.common.CacheKey)20 MultiCacheKey (com.facebook.cache.common.MultiCacheKey)19 Test (org.junit.Test)12 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)10 ArrayList (java.util.ArrayList)7 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)6 BinaryResource (com.facebook.binaryresource.BinaryResource)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Before (org.junit.Before)4 WriterCallback (com.facebook.cache.common.WriterCallback)3 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)3 Uri (android.net.Uri)2 Predicate (com.android.internal.util.Predicate)2 BitmapMemoryCacheKey (com.facebook.imagepipeline.cache.BitmapMemoryCacheKey)2 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 Bitmap (android.graphics.Bitmap)1 FileBinaryResource (com.facebook.binaryresource.FileBinaryResource)1