Java 类com.facebook.cache.common.WriterCallbacks 实例源码

项目:GitHub    文件:DiskStorageCacheTest.java   
@Test
public void testWithMultiCacheKeys() throws Exception {
  CacheKey insertKey1 = new SimpleCacheKey("foo");
  byte[] value1 = new byte[101];
  value1[50] = 'a'; // just so it's not all zeros for the equality test below.
  mCache.insert(insertKey1, WriterCallbacks.from(value1));

  List<CacheKey> keys1 = new ArrayList<>(2);
  keys1.add(new SimpleCacheKey("bar"));
  keys1.add(new SimpleCacheKey("foo"));
  MultiCacheKey matchingMultiKey = new MultiCacheKey(keys1);
  assertArrayEquals(value1, getContents(mCache.getResource(matchingMultiKey)));

  List<CacheKey> keys2 = new ArrayList<>(2);
  keys2.add(new SimpleCacheKey("one"));
  keys2.add(new SimpleCacheKey("two"));
  MultiCacheKey insertKey2 = new MultiCacheKey(keys2);
  byte[] value2 = new byte[101];
  value1[50] = 'b'; // just so it's not all zeros for the equality test below.
  mCache.insert(insertKey2, WriterCallbacks.from(value2));

  CacheKey matchingSimpleKey = new SimpleCacheKey("one");
  assertArrayEquals(value2, getContents(mCache.getResource(matchingSimpleKey)));
}
项目:GitHub    文件:DiskStorageCacheTest.java   
@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);
}
项目:GitHub    文件:DiskStorageCacheTest.java   
@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));
}
项目:fresco    文件:DiskStorageCacheTest.java   
@Test
public void testWithMultiCacheKeys() throws Exception {
  CacheKey insertKey1 = new SimpleCacheKey("foo");
  byte[] value1 = new byte[101];
  value1[50] = 'a'; // just so it's not all zeros for the equality test below.
  mCache.insert(insertKey1, WriterCallbacks.from(value1));

  List<CacheKey> keys1 = new ArrayList<>(2);
  keys1.add(new SimpleCacheKey("bar"));
  keys1.add(new SimpleCacheKey("foo"));
  MultiCacheKey matchingMultiKey = new MultiCacheKey(keys1);
  assertArrayEquals(value1, getContents(mCache.getResource(matchingMultiKey)));

  List<CacheKey> keys2 = new ArrayList<>(2);
  keys2.add(new SimpleCacheKey("one"));
  keys2.add(new SimpleCacheKey("two"));
  MultiCacheKey insertKey2 = new MultiCacheKey(keys2);
  byte[] value2 = new byte[101];
  value1[50] = 'b'; // just so it's not all zeros for the equality test below.
  mCache.insert(insertKey2, WriterCallbacks.from(value2));

  CacheKey matchingSimpleKey = new SimpleCacheKey("one");
  assertArrayEquals(value2, getContents(mCache.getResource(matchingSimpleKey)));
}
项目:fresco    文件:DiskStorageCacheTest.java   
@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);
}
项目:fresco    文件:DiskStorageCacheTest.java   
@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));
}
项目:GitHub    文件:DiskStorageCacheTest.java   
@Test
public void testCacheEventListener() throws Exception {
  // 1. Add first cache file
  CacheKey key1 = new SimpleCacheKey("foo");
  int value1Size = 101;
  byte[] value1 = new byte[value1Size];
  value1[80] = 'c'; // just so it's not all zeros for the equality test below.
  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);
}
项目:GitHub    文件:DiskStorageCacheTest.java   
/**
 * Test to make sure that the same item stored with two different versions
 * of the cache will be stored with two different file names.
 *
 * @throws UnsupportedEncodingException
 */
@Test
public void testVersioning() throws IOException {

  // Define data that will be written to cache
  CacheKey key = new SimpleCacheKey("version_test");
  byte[] value = new byte[32];
  value[0] = 'v';

  // Set up cache with version == 1
  DiskStorage storage1 = createDiskStorage(TESTCACHE_CURRENT_VERSION);
  DiskStorageCache cache1 = createDiskCache(storage1, false);

  // Write test data to cache 1
  cache1.insert(key, WriterCallbacks.from(value));

  // Get cached file
  BinaryResource resource1 = getResource(storage1, key);
  assertNotNull(resource1);

  // Set up cache with version == 2
  DiskStorage storageSupplier2 =
      createDiskStorage(TESTCACHE_NEXT_VERSION);
  DiskStorageCache cache2 = createDiskCache(storageSupplier2, false);

  // Write test data to cache 2
  cache2.insert(key, WriterCallbacks.from(value));

  // Get cached file
  BinaryResource resource2 = getResource(storageSupplier2, key);
  assertNotNull(resource2);

  // Make sure filenames of the two file are different
  assertFalse(resource2.equals(resource1));
}
项目:GitHub    文件:DiskStorageCacheTest.java   
private CacheKey putOneThingInCache(DiskStorageCache cache) throws IOException {
  CacheKey key = new SimpleCacheKey("foo");
  byte[] value1 = new byte[101];
  value1[80] = 'c';
  cache.insert(key, WriterCallbacks.from(value1));
  return key;
}
项目:cardinalsSample    文件:FrescoManager.java   
/**
 * 将图片插入缓存
 *
 * @param key         保存文件的可以
 * @param inputStream 图片转化成的InputStream
 * @return 是否插入成功
 */
public static boolean insertImageToCache(String key, InputStream inputStream) {
    try {
        CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(ImageRequest.fromUri(key), null);
        ImagePipelineFactory.getInstance().getMainFileCache().insert(cacheKey, WriterCallbacks.from(inputStream));
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        CloseUtil.close(inputStream);
    }
    return true;

}
项目:fresco    文件:DiskStorageCacheTest.java   
@Test
public void testCacheEventListener() throws Exception {
  // 1. Add first cache file
  CacheKey key1 = new SimpleCacheKey("foo");
  int value1Size = 101;
  byte[] value1 = new byte[value1Size];
  value1[80] = 'c'; // just so it's not all zeros for the equality test below.
  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);
}
项目:fresco    文件:DiskStorageCacheTest.java   
/**
 * Test to make sure that the same item stored with two different versions
 * of the cache will be stored with two different file names.
 *
 * @throws UnsupportedEncodingException
 */
@Test
public void testVersioning() throws IOException {

  // Define data that will be written to cache
  CacheKey key = new SimpleCacheKey("version_test");
  byte[] value = new byte[32];
  value[0] = 'v';

  // Set up cache with version == 1
  DiskStorage storage1 = createDiskStorage(TESTCACHE_CURRENT_VERSION);
  DiskStorageCache cache1 = createDiskCache(storage1, false);

  // Write test data to cache 1
  cache1.insert(key, WriterCallbacks.from(value));

  // Get cached file
  BinaryResource resource1 = getResource(storage1, key);
  assertNotNull(resource1);

  // Set up cache with version == 2
  DiskStorage storageSupplier2 =
      createDiskStorage(TESTCACHE_NEXT_VERSION);
  DiskStorageCache cache2 = createDiskCache(storageSupplier2, false);

  // Write test data to cache 2
  cache2.insert(key, WriterCallbacks.from(value));

  // Get cached file
  BinaryResource resource2 = getResource(storageSupplier2, key);
  assertNotNull(resource2);

  // Make sure filenames of the two file are different
  assertFalse(resource2.equals(resource1));
}
项目:fresco    文件:DiskStorageCacheTest.java   
private CacheKey putOneThingInCache(DiskStorageCache cache) throws IOException {
  CacheKey key = new SimpleCacheKey("foo");
  byte[] value1 = new byte[101];
  value1[80] = 'c';
  cache.insert(key, WriterCallbacks.from(value1));
  return key;
}
项目:GitHub    文件:DiskStorageCacheTest.java   
@Test
public void testCacheFileWithIOException() throws IOException {
  CacheKey key1 = new SimpleCacheKey("aaa");

  // Before inserting, make sure files not exist.
  final BinaryResource resource1 = getResource(key1);
  assertNull(resource1);

  // 1. Should not create cache files if IOException happens in the middle.
  final IOException writeException = new IOException();
  try {
    mCache.insert(
        key1, new WriterCallback() {
          @Override
          public void write(OutputStream os) throws IOException {
            throw writeException;
          }
        });
    fail();
  } catch (IOException e) {
    assertNull(getResource(key1));
  }

  verifyListenerOnWriteAttempt(key1);
  verifyListenerOnWriteException(key1, writeException);

  // 2. Test a read failure from DiskStorage
  CacheKey key2 = new SimpleCacheKey("bbb");
  int value2Size = 42;
  byte[] value2 = new byte[value2Size];
  value2[25] = 'b';
  mCache.insert(key2, WriterCallbacks.from(value2));

  verifyListenerOnWriteAttempt(key2);
  String resourceId2 = verifyListenerOnWriteSuccessAndGetResourceId(key2, value2Size);

  ((DiskStorageWithReadFailures) mStorage).setPoisonResourceId(resourceId2);

  assertNull(mCache.getResource(key2));
  verifyListenerOnReadException(key2, DiskStorageWithReadFailures.POISON_EXCEPTION);

  assertFalse(mCache.probe(key2));
  verifyListenerOnReadException(key2, DiskStorageWithReadFailures.POISON_EXCEPTION);

  verifyNoMoreInteractions(mCacheEventListener);
}
项目:GitHub    文件:DiskStorageCacheTest.java   
@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);
}
项目:fresco    文件:DiskStorageCacheTest.java   
@Test
public void testCacheFileWithIOException() throws IOException {
  CacheKey key1 = new SimpleCacheKey("aaa");

  // Before inserting, make sure files not exist.
  final BinaryResource resource1 = getResource(key1);
  assertNull(resource1);

  // 1. Should not create cache files if IOException happens in the middle.
  final IOException writeException = new IOException();
  try {
    mCache.insert(
        key1, new WriterCallback() {
          @Override
          public void write(OutputStream os) throws IOException {
            throw writeException;
          }
        });
    fail();
  } catch (IOException e) {
    assertNull(getResource(key1));
  }

  verifyListenerOnWriteAttempt(key1);
  verifyListenerOnWriteException(key1, writeException);

  // 2. Test a read failure from DiskStorage
  CacheKey key2 = new SimpleCacheKey("bbb");
  int value2Size = 42;
  byte[] value2 = new byte[value2Size];
  value2[25] = 'b';
  mCache.insert(key2, WriterCallbacks.from(value2));

  verifyListenerOnWriteAttempt(key2);
  String resourceId2 = verifyListenerOnWriteSuccessAndGetResourceId(key2, value2Size);

  ((DiskStorageWithReadFailures) mStorage).setPoisonResourceId(resourceId2);

  assertNull(mCache.getResource(key2));
  verifyListenerOnReadException(key2, DiskStorageWithReadFailures.POISON_EXCEPTION);

  assertFalse(mCache.probe(key2));
  verifyListenerOnReadException(key2, DiskStorageWithReadFailures.POISON_EXCEPTION);

  verifyNoMoreInteractions(mCacheEventListener);
}
项目:fresco    文件:DiskStorageCacheTest.java   
@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);
}