我正在访问TestRail,该API有180 Req/min的速率限制,
@Bean
@Named("testRail")
fun createClient(appConfig: AppConfig): OkHttpClient = OkHttpClient
.Builder()
.cache(Cache(File("http_cache"),100*1024*1024))
.readTimeout(Duration.ofHours(1)).build()我尝试过这些方法来清除缓存,但是似乎没有什么效果,
println("Client Request original: ${httpClient.cache?.requestCount()}") //prints n
httpClient.cache?.evictAll()
println("Client Request after evict: ${httpClient.cache?.requestCount()}") //prints n
httpClient.cache?.flush()
println("Client Request after flush: ${httpClient.cache?.requestCount()}") //prints n
httpClient.cache?.delete()
httpClient.cache?.initialize()
println("Client Request after deleting and initializing: ${httpClient.cache?.requestCount()}") //prints n我也尝试过添加一个拦截器,但这也不起作用,否则我在这里做错了什么:
@Bean
@Named("testRail")
fun createClient(appConfig: AppConfig): OkHttpClient = OkHttpClient
.Builder()
.cache(Cache(File("http_cache"),100*1024*1024))
.readTimeout(Duration.ofHours(1)).build()
.addInterceptor(provideCacheInterceptor(1))
private fun provideCacheInterceptor(maxMinutes: Int): Interceptor {
return Interceptor { chain ->
val response: Response = chain.proceed(chain.request())
val cacheControl: CacheControl = CacheControl.Builder()
.maxAge(maxMinutes, TimeUnit.MINUTES)
.build()
response.newBuilder()
.header(CACHE_CONTROL, cacheControl.toString())
.build()
}
}请告诉我是否有办法解决这个问题。
发布于 2022-04-22 01:04:33
OkHttp的缓存没有用于重置统计信息的API。
刷新强制内存中的数据到磁盘。在程序退出之前使用这个。
EvictAll从缓存中删除所有元素。
如果要重置统计信息,请创建一个对象以记住当前状态,然后减去。
https://stackoverflow.com/questions/71959045
复制相似问题