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));
}
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));
}
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));
}
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);
}
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));
}
Aggregations