Handle cache internally

This commit is contained in:
tarsin committed 2023-01-29 12:38:44 +08:00
1 parent 1b9e00efe1
commit e17dc78aeb
3 files changed
+37 -33

No files matched your search

@@ -329,12 +329,6 @@ class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactor
}
}
@JvmStatic
val spiderInfoCache by lazy {
DiskCache.Builder().directory(File(application.cacheDir, "spider_info_v2_1"))
.maxSizeBytes(20 * 1024 * 1024).build()
}
@JvmStatic
val downloadManager by lazy { DownloadManager() }
@@ -2,6 +2,9 @@
package com.hippo.ehviewer.spider
import coil.annotation.ExperimentalCoilApi
import coil.disk.DiskCache
import com.hippo.ehviewer.EhApplication
import com.hippo.unifile.UniFile
import com.hippo.yorozuya.IOUtils
import kotlinx.serialization.ExperimentalSerializationApi
@@ -34,11 +37,39 @@ class SpiderInfo @JvmOverloads constructor(
}
}
fun write(file: File) {
file.writeBytes(Cbor.encodeToByteArray(this))
@OptIn(ExperimentalCoilApi::class)
fun saveToCache() {
val entry = spiderInfoCache.edit(gid.toString()) ?: return
runCatching {
entry.data.toFile().writeBytes(Cbor.encodeToByteArray(this))
}.onFailure {
it.printStackTrace()
entry.abort()
}.onSuccess {
entry.commit()
}
}
companion object {
private val spiderInfoCache by lazy {
DiskCache.Builder()
.directory(File(EhApplication.application.cacheDir, "spider_info_v2_1"))
.maxSizeBytes(20 * 1024 * 1024).build()
}
@JvmStatic
@OptIn(ExperimentalCoilApi::class)
fun readFromCache(gid: Long): SpiderInfo? {
val snapshot = spiderInfoCache[gid.toString()] ?: return null
return runCatching {
snapshot.use {
return Cbor.decodeFromByteArray(it.data.toFile().readBytes())
}
}.onFailure {
it.printStackTrace()
}.getOrNull()
}
@JvmStatic
fun read(file: UniFile): SpiderInfo? {
return runCatching {
@@ -50,12 +81,6 @@ class SpiderInfo @JvmOverloads constructor(
}.getOrNull()
}
// This path is for diskcache only, no need to compatible with old data
@JvmStatic
fun read(file: File): SpiderInfo? {
return Cbor.decodeFromByteArray(file.readBytes())
}
private fun readV1(inputStream: InputStream): SpiderInfo {
fun read(): String {
return IOUtils.readAsciiLine(inputStream)
@@ -77,7 +77,6 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import coil.disk.DiskCache;
import eu.kanade.tachiyomi.ui.reader.loader.PageLoader;
import okhttp3.Call;
import okhttp3.MediaType;
@@ -108,8 +107,6 @@ public final class SpiderQueen implements Runnable {
@NonNull
private final OkHttpClient mHttpClient;
@NonNull
private final DiskCache mSpiderInfoCache;
@NonNull
private final GalleryInfo mGalleryInfo;
@NonNull
private final SpiderDen mSpiderDen;
@@ -155,7 +152,6 @@ public final class SpiderQueen implements Runnable {
private SpiderQueen(@NonNull GalleryInfo galleryInfo) {
mHttpClient = EhApplication.getOkHttpClient();
mSpiderInfoCache = EhApplication.getSpiderInfoCache();
mGalleryInfo = galleryInfo;
mSpiderDen = new SpiderDen(mGalleryInfo);
@@ -703,18 +699,10 @@ public final class SpiderQueen implements Runnable {
}
// Read from cache
var entry = mSpiderInfoCache.get(Long.toString(mGalleryInfo.getGid()));
if (entry == null) return null;
try (entry) {
spiderInfo = SpiderInfo.read(entry.getData().toFile());
if (spiderInfo != null && spiderInfo.getGid() == mGalleryInfo.getGid() &&
spiderInfo.getToken().equals(mGalleryInfo.getToken())) {
return spiderInfo;
}
} catch (Throwable e) {
e.printStackTrace();
spiderInfo = SpiderInfo.readFromCache(mGalleryInfo.getGid());
if (spiderInfo != null && spiderInfo.getGid() == mGalleryInfo.getGid() && spiderInfo.getToken().equals(mGalleryInfo.getToken())) {
return spiderInfo;
}
return null;
}
@@ -851,10 +839,7 @@ public final class SpiderQueen implements Runnable {
}
// Write to cache
var editor = mSpiderInfoCache.edit(Long.toString(mGalleryInfo.getGid()));
if (editor == null) return;
spiderInfo.write(editor.getData().toFile());
editor.commit();
spiderInfo.saveToCache();
}
private void runInternal() {