我在应用程序的不同点提出相同的请求
Document doc = Jsoup.connect("https://www.sampleURL.com")
.get();
有没有办法缓存get请求?
您可以在 header 中传递 Cache-Control=cache 来缓存 GET 请求
从Context
获取
缓存目录。通过 Hilt,您可以使用应用程序上下文。要以字符串形式获取页面的 html,请使用
doc.outerHtml()
。
@Module
@InstallIn(SingletonComponent::class)
object ApiModule {
@Singleton
@Provides
fun provideHtmlRepository(
app: Application
) = HtmlRepository(app)
}
class HtmlRepository(
context: Context
) {
private val cacheDir = context.cacheDir
init {
val storageManager = context.getSystemService(Context.STORAGE_SERVICE) as StorageManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val cacheDirUuid = storageManager.getUuidForPath(cacheDir)
val quota = storageManager.getCacheQuotaBytes(cacheDirUuid)
Log.d("CACHE", "quota: $quota $cacheDirUuid")
}
Log.d("CACHE", "cache directory: ${context.cacheDir}")
}
suspend fun loadHtml(url: String): Document = withContext(Dispatchers.IO) {
val cacheFile = cacheFile(url)
if (cacheFile.exists()) {
Jsoup.parse(cacheFile, "UTF-8", url)
} else {
Jsoup.connect(url).get()
.also { doc -> cache(doc.outerHtml(), url)}
}
}
private suspend fun cache(html: String, url: String) = withContext(Dispatchers.IO) {
// launch new coroutine to avoid waiting
launch {
val cacheFile = cacheFile(url)
FileOutputStream(cacheFile).use { stream ->
stream.write(html.toByteArray())
}
}
}
private fun cacheFile(url: String): File {
val encodedUrl = URLEncoder.encode(url, Charsets.UTF_8.name()).htmlEncode()
return File(cacheDir, "$encodedUrl.html")
}
}