Search in sources :

Example 51 with CacheKey

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

the class DiskStorageCacheTest method testInsertionInIndex.

@Test
public void testInsertionInIndex() throws Exception {
    CacheKey key = putOneThingInCache();
    assertTrue(mCache.hasKeySync(key));
    assertTrue(mCache.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 52 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)

Example 53 with CacheKey

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

the class DiskStorageCacheTest method testCleanOldCacheNoEntriesRemaining.

@Test
public void testCleanOldCacheNoEntriesRemaining() throws IOException {
    long cacheExpirationMs = TimeUnit.DAYS.toMillis(5);
    CacheKey key1 = new SimpleCacheKey("aaa");
    byte[] value1 = new byte[41];
    mCache.insert(key1, WriterCallbacks.from(value1));
    CacheKey key2 = new SimpleCacheKey("bbb");
    byte[] value2 = new byte[42];
    mCache.insert(key2, WriterCallbacks.from(value2));
    // Increment clock by default expiration time + 1 day
    when(mClock.now()).thenReturn(cacheExpirationMs + TimeUnit.DAYS.toMillis(1));
    long oldestEntry = mCache.clearOldEntries(cacheExpirationMs);
    assertEquals(0L, oldestEntry);
}
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 54 with CacheKey

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

the class DiskStorageCacheTest method testHasKeyWithoutPopulateAtStartupWithoutAwaitingIndex.

@Test
public void testHasKeyWithoutPopulateAtStartupWithoutAwaitingIndex() throws Exception {
    CacheKey key = putOneThingInCache();
    // A new cache object in the same directory. Equivalent to a process restart.
    // Index may not yet updated.
    DiskStorageCache cache2 = createDiskCache(mStorage, false);
    assertTrue(cache2.isIndexReady());
    assertFalse(cache2.hasKeySync(key));
    assertTrue(cache2.hasKey(key));
    // hasKey() adds item to the index
    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 55 with CacheKey

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

the class BaseFrescoStethoPlugin method memcache.

private void memcache(PrintStream writer, List<String> args) throws DumpException {
    CountingMemoryCacheInspector.DumpInfo<CacheKey, CloseableImage> dumpInfo = mBitmapMemoryCacheInspector.dumpCacheContent();
    try {
        writer.println(mBitmapMemoryCacheInspector.getClass().getSimpleName());
        writer.println();
        writer.println("Params:");
        writer.println(formatStrLocaleSafe("Max size:          %7.2fMB", dumpInfo.maxSize / (1024.0 * KB)));
        writer.println(formatStrLocaleSafe("Max entries count: %9d", dumpInfo.maxEntriesCount));
        writer.println(formatStrLocaleSafe("Max entry size:    %7.2fMB", dumpInfo.maxEntrySize / (1024.0 * KB)));
        writer.println();
        writer.println("Summary of current content:");
        writer.println(formatStrLocaleSafe("Total size:        %7.2fMB (includes in-use content)", dumpInfo.size / (1024.0 * KB)));
        writer.println(formatStrLocaleSafe("Entries count:     %9d", dumpInfo.lruEntries.size() + dumpInfo.sharedEntries.size()));
        writer.println(formatStrLocaleSafe("LRU size:          %7.2fMB", dumpInfo.lruSize / (1024.0 * KB)));
        writer.println(formatStrLocaleSafe("LRU count:         %9d", dumpInfo.lruEntries.size()));
        writer.println(formatStrLocaleSafe("Shared size:       %7.2fMB", (dumpInfo.size - dumpInfo.lruSize) / (1024.0 * KB)));
        writer.println(formatStrLocaleSafe("Shared count:      %9d", dumpInfo.sharedEntries.size()));
        writer.println();
        writer.println("The cache consists of two parts: Things " + "currently being used and things not.");
        writer.println("Those things that are *not* currently being used are in the LRU.");
        writer.println("Things currently being used are considered to be shared. They will be added");
        writer.println("to the LRU if/when they stop being used.");
        writer.println();
        writer.println("LRU contents: (things near the top will be evicted first)");
        for (CountingMemoryCacheInspector.DumpInfoEntry entry : dumpInfo.lruEntries) {
            writeCacheEntry(writer, entry);
        }
        writer.println();
        writer.println("Shared contents:");
        for (CountingMemoryCacheInspector.DumpInfoEntry entry : dumpInfo.sharedEntries) {
            writeCacheEntry(writer, entry);
        }
        if (!args.isEmpty() && "-g".equals(args.get(0))) {
            getFiles(writer, dumpInfo);
        }
    } catch (IOException e) {
        throw new DumpException(e.getMessage());
    } finally {
        dumpInfo.release();
    }
}
Also used : DumpException(com.facebook.stetho.dumpapp.DumpException) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) IOException(java.io.IOException) CacheKey(com.facebook.cache.common.CacheKey) BitmapMemoryCacheKey(com.facebook.imagepipeline.cache.BitmapMemoryCacheKey) CountingMemoryCacheInspector(com.facebook.imagepipeline.cache.CountingMemoryCacheInspector)

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