use of com.facebook.cache.common.CacheKey in project fresco by facebook.
the class DiskStorageCacheTest method testHasKeyWithPopulateAtStartupWithoutAwaitingIndex.
@Test
public void testHasKeyWithPopulateAtStartupWithoutAwaitingIndex() throws Exception {
DiskStorageCache cache2 = createDiskCache(mStorage, true);
CacheKey key = putOneThingInCache(cache2);
// A new cache object in the same directory. Equivalent to a process restart.
// Index may not yet updated.
assertFalse(cache2.isIndexReady());
assertTrue(cache2.hasKey(key));
assertTrue(cache2.hasKeySync(key));
}
use of com.facebook.cache.common.CacheKey in project fresco by facebook.
the class DiskStorageCacheTest method testVersioning.
/**
* 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));
}
use of com.facebook.cache.common.CacheKey in project fresco by facebook.
the class ImagePipeline method evictFromDiskCache.
/**
* Removes all images with the specified {@link Uri} from disk cache.
*
* @param imageRequest The imageRequest for the image to evict from disk cache
*/
public void evictFromDiskCache(final ImageRequest imageRequest) {
CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, null);
mMainBufferedDiskCache.remove(cacheKey);
mSmallImageBufferedDiskCache.remove(cacheKey);
}
use of com.facebook.cache.common.CacheKey in project fresco by facebook.
the class ImagePipeline method isInDiskCache.
/**
* Returns whether the image is stored in the disk cache.
*
* @param imageRequest the imageRequest for the image to be looked up.
* @return true if the image was found in the disk cache, false otherwise.
*/
public DataSource<Boolean> isInDiskCache(final ImageRequest imageRequest) {
final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, null);
final SimpleDataSource<Boolean> dataSource = SimpleDataSource.create();
mMainBufferedDiskCache.contains(cacheKey).continueWithTask(new Continuation<Boolean, Task<Boolean>>() {
@Override
public Task<Boolean> then(Task<Boolean> task) throws Exception {
if (!task.isCancelled() && !task.isFaulted() && task.getResult()) {
return Task.forResult(true);
}
return mSmallImageBufferedDiskCache.contains(cacheKey);
}
}).continueWith(new Continuation<Boolean, Void>() {
@Override
public Void then(Task<Boolean> task) throws Exception {
dataSource.setResult(!task.isCancelled() && !task.isFaulted() && task.getResult());
return null;
}
});
return dataSource;
}
use of com.facebook.cache.common.CacheKey in project fresco by facebook.
the class ImagePipeline method isInDiskCacheSync.
/**
* Performs disk cache check synchronously. It is not recommended to use this
* unless you know what exactly you are doing. Disk cache check is a costly operation,
* the call will block the caller thread until the cache check is completed.
* @param imageRequest the imageRequest for the image to be looked up.
* @return true if the image was found in the disk cache, false otherwise.
*/
public boolean isInDiskCacheSync(final ImageRequest imageRequest) {
final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, null);
final ImageRequest.CacheChoice cacheChoice = imageRequest.getCacheChoice();
switch(cacheChoice) {
case DEFAULT:
return mMainBufferedDiskCache.diskCheckSync(cacheKey);
case SMALL:
return mSmallImageBufferedDiskCache.diskCheckSync(cacheKey);
default:
return false;
}
}
Aggregations