Store spider info file in cache
This commit is contained in:
1 parent
e74b49fff7
commit
4aa21bab09
6 files changed
+60
-34
No files matched your search
@@ -37,11 +37,6 @@ public class AppConfig {
|
||||
sContext = context.getApplicationContext();
|
||||
}
|
||||
|
||||
public static File getSpiderInfoCacheDir() {
|
||||
File dir = new File(sContext.getCacheDir(), SPIDER_INFO);
|
||||
return FileUtils.ensureDirectory(dir) ? dir : null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getExternalAppDir() {
|
||||
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
|
||||
|
||||
@@ -25,6 +25,7 @@ import android.util.Log;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import com.hippo.beerbelly.LruCacheEx;
|
||||
import com.hippo.beerbelly.SimpleDiskCache;
|
||||
import com.hippo.conaco.Conaco;
|
||||
import com.hippo.ehviewer.client.EhClient;
|
||||
import com.hippo.ehviewer.client.EhCookieStore;
|
||||
@@ -63,6 +64,7 @@ public class EhApplication extends SceneApplication {
|
||||
private LruCacheEx<Long, GalleryDetail> mGalleryDetailCache;
|
||||
private LruCacheEx<String, LargePreviewSet> mLargePreviewSetCache;
|
||||
private LruCacheEx<Long, Integer> mPreviewPagesCache;
|
||||
private SimpleDiskCache mSpiderInfoCache;
|
||||
private DownloadManager mDownloadManager;
|
||||
|
||||
@Override
|
||||
@@ -233,6 +235,16 @@ public class EhApplication extends SceneApplication {
|
||||
return application.mPreviewPagesCache;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static SimpleDiskCache getSpiderInfoCache(@NonNull Context context) {
|
||||
EhApplication application = ((EhApplication) context.getApplicationContext());
|
||||
if (null == application.mSpiderInfoCache) {
|
||||
application.mSpiderInfoCache = new SimpleDiskCache(
|
||||
new File(context.getCacheDir(), "spider_info"), 5 * 1024 * 1024); // 5M
|
||||
}
|
||||
return application.mSpiderInfoCache;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static DownloadManager getDownloadManager(@NonNull Context context) {
|
||||
EhApplication application = ((EhApplication) context.getApplicationContext());
|
||||
|
||||
@@ -52,11 +52,7 @@ public final class SpiderDen {
|
||||
private static SimpleDiskCache sCache;
|
||||
|
||||
public static void initialize(Context context) {
|
||||
try {
|
||||
sCache = new SimpleDiskCache(new File(context.getCacheDir(), "image"), 160 * 1024 * 1024);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
sCache = new SimpleDiskCache(new File(context.getCacheDir(), "image"), 160 * 1024 * 1024);
|
||||
}
|
||||
|
||||
public static UniFile getGalleryDownloadDir(GalleryInfo galleryInfo) {
|
||||
|
||||
@@ -44,16 +44,31 @@ public class SpiderInfo {
|
||||
int previewPerPage = -1;
|
||||
SparseArray<String> pTokenMap = null;
|
||||
|
||||
@SuppressWarnings("InfiniteLoopStatement")
|
||||
public static SpiderInfo readFromUniFile(@Nullable UniFile file) {
|
||||
|
||||
public static SpiderInfo read(@Nullable UniFile file) {
|
||||
if (file == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
InputStream is = null;
|
||||
SpiderInfo spiderInfo = null;
|
||||
try {
|
||||
is = file.openInputStream();
|
||||
return read(is);
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
} finally {
|
||||
IOUtils.closeQuietly(is);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("InfiniteLoopStatement")
|
||||
public static SpiderInfo read(@Nullable InputStream is) {
|
||||
if (null == is) {
|
||||
return null;
|
||||
}
|
||||
|
||||
SpiderInfo spiderInfo = null;
|
||||
try {
|
||||
spiderInfo = new SpiderInfo();
|
||||
// Start page
|
||||
spiderInfo.startPage = Integer.parseInt(IOUtils.readAsciiLine(is), 16);
|
||||
@@ -87,8 +102,6 @@ public class SpiderInfo {
|
||||
}
|
||||
} catch (IOException | NumberFormatException e) {
|
||||
// Ignore
|
||||
} finally {
|
||||
IOUtils.closeQuietly(is);
|
||||
}
|
||||
|
||||
if (spiderInfo == null || spiderInfo.gid == -1 || spiderInfo.token == null ||
|
||||
|
||||
@@ -27,7 +27,7 @@ import android.util.Log;
|
||||
import android.util.SparseArray;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import com.hippo.ehviewer.AppConfig;
|
||||
import com.hippo.beerbelly.SimpleDiskCache;
|
||||
import com.hippo.ehviewer.EhApplication;
|
||||
import com.hippo.ehviewer.GetText;
|
||||
import com.hippo.ehviewer.R;
|
||||
@@ -55,8 +55,6 @@ import com.hippo.yorozuya.io.InputStreamPipe;
|
||||
import com.hippo.yorozuya.io.OutputStreamPipe;
|
||||
import com.hippo.yorozuya.sparse.SparseJLArray;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -116,6 +114,8 @@ public class SpiderQueen implements Runnable {
|
||||
@NonNull
|
||||
private final OkHttpClient mHttpClient;
|
||||
@NonNull
|
||||
private final SimpleDiskCache mSpiderInfoCache;
|
||||
@NonNull
|
||||
private final GalleryInfo mGalleryInfo;
|
||||
@NonNull
|
||||
private final SpiderDen mSpiderDen;
|
||||
@@ -170,6 +170,7 @@ public class SpiderQueen implements Runnable {
|
||||
|
||||
private SpiderQueen(EhApplication application, @NonNull GalleryInfo galleryInfo) {
|
||||
mHttpClient = EhApplication.getOkHttpClient(application);
|
||||
mSpiderInfoCache = EhApplication.getSpiderInfoCache(application);
|
||||
mGalleryInfo = galleryInfo;
|
||||
mSpiderDen = new SpiderDen(mGalleryInfo);
|
||||
}
|
||||
@@ -581,7 +582,7 @@ public class SpiderQueen implements Runnable {
|
||||
UniFile downloadDir = mSpiderDen.getDownloadDir();
|
||||
if (downloadDir != null) {
|
||||
UniFile file = downloadDir.findFile(SPIDER_INFO_FILENAME);
|
||||
SpiderInfo spiderInfo = SpiderInfo.readFromUniFile(file);
|
||||
SpiderInfo spiderInfo = SpiderInfo.read(file);
|
||||
if (spiderInfo != null && spiderInfo.gid == mGalleryInfo.gid &&
|
||||
spiderInfo.token.equals(mGalleryInfo.token)) {
|
||||
return spiderInfo;
|
||||
@@ -589,13 +590,20 @@ public class SpiderQueen implements Runnable {
|
||||
}
|
||||
|
||||
// Read from cache
|
||||
File dir = AppConfig.getSpiderInfoCacheDir();
|
||||
if (dir != null) {
|
||||
UniFile file = UniFile.fromFile(new File(dir, Long.toString(mGalleryInfo.gid)));
|
||||
SpiderInfo spiderInfo = SpiderInfo.readFromUniFile(file);
|
||||
if (spiderInfo != null && spiderInfo.gid == mGalleryInfo.gid &&
|
||||
spiderInfo.token.equals(mGalleryInfo.token)) {
|
||||
return spiderInfo;
|
||||
InputStreamPipe pipe = mSpiderInfoCache.getInputStreamPipe(Long.toString(mGalleryInfo.gid));
|
||||
if (null != pipe) {
|
||||
try {
|
||||
pipe.obtain();
|
||||
SpiderInfo spiderInfo = SpiderInfo.read(pipe.open());
|
||||
if (spiderInfo != null && spiderInfo.gid == mGalleryInfo.gid &&
|
||||
spiderInfo.token.equals(mGalleryInfo.token)) {
|
||||
return spiderInfo;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// Ignore
|
||||
} finally {
|
||||
pipe.close();
|
||||
pipe.release();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -683,13 +691,15 @@ public class SpiderQueen implements Runnable {
|
||||
}
|
||||
|
||||
// Read from cache
|
||||
File dir = AppConfig.getSpiderInfoCacheDir();
|
||||
if (dir != null) {
|
||||
try {
|
||||
spiderInfo.write(new FileOutputStream(new File(dir, Long.toString(mGalleryInfo.gid))));
|
||||
} catch (Exception e) {
|
||||
// Ignore
|
||||
}
|
||||
OutputStreamPipe pipe = mSpiderInfoCache.getOutputStreamPipe(Long.toString(mGalleryInfo.gid));
|
||||
try {
|
||||
pipe.obtain();
|
||||
spiderInfo.write(pipe.open());
|
||||
} catch (IOException e) {
|
||||
// Ignore
|
||||
} finally {
|
||||
pipe.close();
|
||||
pipe.release();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
Submodule beerbelly updated: 8ca51ee073...266f6590a9.
Reference in new issue
Block a user