use of com.koushikdutta.async.util.FileCache in project AndroidAsync by koush.
the class ResponseCacheMiddleware method addCache.
public static ResponseCacheMiddleware addCache(AsyncHttpClient client, File cacheDir, long size) throws IOException {
for (AsyncHttpClientMiddleware middleware : client.getMiddleware()) {
if (middleware instanceof ResponseCacheMiddleware)
throw new IOException("Response cache already added to http client");
}
ResponseCacheMiddleware ret = new ResponseCacheMiddleware();
ret.server = client.getServer();
ret.cache = new FileCache(cacheDir, size, false);
client.insertMiddleware(ret);
return ret;
}
use of com.koushikdutta.async.util.FileCache in project AndroidAsync by koush.
the class FileCacheTests method testEviction.
public void testEviction() throws Exception {
FileCache cache = new FileCache(new File(getContext().getCacheDir(), "filecache"), 25, false);
cache.setBlockSize(1);
for (int i = 0; i < 10; i++) {
File temp = cache.getTempFile();
StreamUtility.writeFile(temp, "hello");
cache.commitTempFiles("test" + i, temp);
String value = StreamUtility.readToEnd(cache.get("test" + i));
assertEquals(value, "hello");
}
assertEquals(cache.size(), 25);
File dir = new File(getContext().getCacheDir(), "filecache");
File[] files = dir.listFiles();
assertEquals(files.length, 5);
for (int i = 5; i < 10; i++) {
assertTrue(cache.exists("test" + i));
}
}
use of com.koushikdutta.async.util.FileCache in project AndroidAsync by koush.
the class FileCacheTests method testSimple.
public void testSimple() throws Exception {
FileCache cache = new FileCache(new File(getContext().getCacheDir(), "filecache"), 100000, false);
cache.setBlockSize(1);
File temp = cache.getTempFile();
StreamUtility.writeFile(temp, "hello");
cache.commitTempFiles("test", temp);
String value = StreamUtility.readToEnd(cache.get("test"));
assertEquals(value, "hello");
}
use of com.koushikdutta.async.util.FileCache in project AndroidAsync by koush.
the class FileCacheTests method testReinit.
public void testReinit() throws Exception {
FileCache cache = new FileCache(new File(getContext().getCacheDir(), "filecache"), 10, false);
cache.setBlockSize(1);
File temp = cache.getTempFile();
StreamUtility.writeFile(temp, "hello");
cache.commitTempFiles("test", temp);
temp = cache.getTempFile();
StreamUtility.writeFile(temp, "hello");
cache.commitTempFiles("test2", temp);
assertEquals(cache.size(), 10);
cache = new FileCache(new File(getContext().getCacheDir(), "filecache"), 10, false);
cache.setBlockSize(1);
String value = StreamUtility.readToEnd(cache.get("test"));
assertEquals(value, "hello");
value = StreamUtility.readToEnd(cache.get("test2"));
assertEquals(value, "hello");
}
use of com.koushikdutta.async.util.FileCache in project AndroidAsync by koush.
the class FileCacheTests method testMultipartEvictionAgain.
public void testMultipartEvictionAgain() throws Exception {
FileCache cache = new FileCache(new File(getContext().getCacheDir(), "filecache"), 72, false);
cache.setBlockSize(1);
File[] temps = new File[10];
for (int i = 0; i < temps.length; i++) {
File temp = temps[i] = cache.getTempFile();
StreamUtility.writeFile(temp, "hello" + i);
}
cache.commitTempFiles("test", temps);
assertEquals(cache.size(), 60);
File dir = new File(getContext().getCacheDir(), "filecache");
File[] files = dir.listFiles();
assertEquals(files.length, 10);
for (int i = 0; i < temps.length; i++) {
assertTrue(cache.exists("test", i));
}
FileInputStream[] fins = cache.get("test", temps.length);
StreamUtility.closeQuietly(fins);
temps = new File[10];
for (int i = 0; i < temps.length; i++) {
File temp = temps[i] = cache.getTempFile();
StreamUtility.writeFile(temp, "hello" + i);
}
cache.commitTempFiles("test2", temps);
assertEquals(cache.size(), 72);
fins = cache.get("test2", temps.length);
StreamUtility.closeQuietly(fins);
try {
fins = cache.get("test", temps.length);
fail();
} catch (IOException e) {
}
}
Aggregations