Pages lru cache

This commit is contained in:
Admin
2020-02-17 20:12:39 +02:00
parent 2309d2465b
commit a552328888
3 changed files with 14 additions and 12 deletions

View File

@@ -1,6 +1,8 @@
package org.koitharu.kotatsu.core.local
import android.content.Context
import com.tomclaw.cache.DiskLruCache
import org.koitharu.kotatsu.utils.FileSizeUtils
import org.koitharu.kotatsu.utils.ext.longHashCode
import org.koitharu.kotatsu.utils.ext.sub
import org.koitharu.kotatsu.utils.ext.takeIfReadable
@@ -9,19 +11,18 @@ import java.io.OutputStream
class PagesCache(context: Context) {
private val cacheDir = File(context.externalCacheDir ?: context.cacheDir, "pages")
private val cacheDir = context.externalCacheDir ?: context.cacheDir
private val lruCache = DiskLruCache.create(cacheDir.sub("pages"), FileSizeUtils.mbToBytes(200))
init {
if (!cacheDir.exists()) {
cacheDir.mkdir()
}
operator fun get(url: String): File? {
return lruCache.get(url)?.takeIfReadable()
}
operator fun get(url: String) = cacheDir.sub(url.longHashCode().toString()).takeIfReadable()
fun put(url: String, writer: (OutputStream) -> Unit): File {
val file = cacheDir.sub(url.longHashCode().toString())
file.outputStream().use(writer)
return file
val res = lruCache.put(url, file)
file.delete()
return res
}
}