Decrease Volley startup time:

Set a max to the files
Use bufferedinput/outputstream for reading/writing files.
captchafix
Florens Douwes 11 years ago
parent a93fcb8be4
commit 125c9d8d9b
  1. 16
      Clover/app/src/main/java/com/android/volley/toolbox/DiskBasedCache.java
  2. 2
      Clover/app/src/main/java/org/floens/chan/ChanApplication.java

@ -21,6 +21,8 @@ import android.os.SystemClock;
import com.android.volley.Cache;
import com.android.volley.VolleyLog;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
@ -63,6 +65,8 @@ public class DiskBasedCache implements Cache {
/** Magic number for current version of cache file format. */
private static final int CACHE_MAGIC = 0x20140623;
private static final int DEFAULT_DISK_FILES_MAX = 250;
/**
* Constructs an instance of the DiskBasedCache at the specified directory.
* @param rootDirectory The root directory of the cache.
@ -112,7 +116,7 @@ public class DiskBasedCache implements Cache {
File file = getFileForKey(key);
CountingInputStream cis = null;
try {
cis = new CountingInputStream(new FileInputStream(file));
cis = new CountingInputStream(new BufferedInputStream(new FileInputStream(file)));
CacheHeader.readHeader(cis); // eat header
byte[] data = streamToBytes(cis, (int) (file.length() - cis.bytesRead));
return entry.toCacheEntry(data);
@ -149,9 +153,9 @@ public class DiskBasedCache implements Cache {
return;
}
for (File file : files) {
FileInputStream fis = null;
BufferedInputStream fis = null;
try {
fis = new FileInputStream(file);
fis = new BufferedInputStream(new FileInputStream(file));
CacheHeader entry = CacheHeader.readHeader(fis);
entry.size = file.length();
putEntry(entry.key, entry);
@ -195,7 +199,7 @@ public class DiskBasedCache implements Cache {
pruneIfNeeded(entry.data.length);
File file = getFileForKey(key);
try {
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(file));
CacheHeader e = new CacheHeader(key, entry);
boolean success = e.writeHeader(fos);
if (!success) {
@ -252,7 +256,7 @@ public class DiskBasedCache implements Cache {
* @param neededSpace The amount of bytes we are trying to fit into the cache.
*/
private void pruneIfNeeded(int neededSpace) {
if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
if (mEntries.size() <= DEFAULT_DISK_FILES_MAX && (mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
return;
}
if (VolleyLog.DEBUG) {
@ -277,7 +281,7 @@ public class DiskBasedCache implements Cache {
iterator.remove();
prunedFiles++;
if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
if (mEntries.size() < DEFAULT_DISK_FILES_MAX * HYSTERESIS_FACTOR && (mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
break;
}
}

@ -47,7 +47,7 @@ public class ChanApplication extends Application {
private static final long FILE_CACHE_DISK_SIZE = 50 * 1024 * 1024; // 50mb
private static final String FILE_CACHE_NAME = "filecache";
private static final int VOLLEY_LRU_CACHE_SIZE = 8 * 1024 * 1024; // 8mb
private static final int VOLLEY_CACHE_SIZE = 20 * 1024 * 1024; // 8mb
private static final int VOLLEY_CACHE_SIZE = 10 * 1024 * 1024; // 8mb
private static ChanApplication instance;
private static RequestQueue volleyRequestQueue;

Loading…
Cancel
Save