mirror of
https://github.com/square/okhttp.git
synced 2025-08-08 23:42:08 +03:00
Cache Events (#6015)
Provide EventListener methods to understand the impact of caching.
This commit is contained in:
@@ -204,4 +204,24 @@ sealed class CallEvent {
|
||||
override val call: Call,
|
||||
val ioe: IOException
|
||||
) : CallEvent()
|
||||
|
||||
data class SatisfactionFailure(
|
||||
override val timestampNs: Long,
|
||||
override val call: Call
|
||||
) : CallEvent()
|
||||
|
||||
data class CacheHit(
|
||||
override val timestampNs: Long,
|
||||
override val call: Call
|
||||
) : CallEvent()
|
||||
|
||||
data class CacheMiss(
|
||||
override val timestampNs: Long,
|
||||
override val call: Call
|
||||
) : CallEvent()
|
||||
|
||||
data class CacheConditionalHit(
|
||||
override val timestampNs: Long,
|
||||
override val call: Call
|
||||
) : CallEvent()
|
||||
}
|
||||
|
@@ -241,6 +241,30 @@ class ClientRuleEventListener(
|
||||
delegate.canceled(call)
|
||||
}
|
||||
|
||||
override fun satisfactionFailure(call: Call, response: Response) {
|
||||
logWithTime("satisfactionFailure")
|
||||
|
||||
delegate.satisfactionFailure(call, response)
|
||||
}
|
||||
|
||||
override fun cacheMiss(call: Call) {
|
||||
logWithTime("cacheMiss")
|
||||
|
||||
delegate.cacheMiss(call)
|
||||
}
|
||||
|
||||
override fun cacheHit(call: Call, response: Response) {
|
||||
logWithTime("cacheHit")
|
||||
|
||||
delegate.cacheHit(call, response)
|
||||
}
|
||||
|
||||
override fun cacheConditionalHit(call: Call, cachedResponse: Response) {
|
||||
logWithTime("cacheConditionalHit")
|
||||
|
||||
delegate.cacheConditionalHit(call, cachedResponse)
|
||||
}
|
||||
|
||||
private fun logWithTime(message: String) {
|
||||
val timeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs)
|
||||
logger.invoke("[$timeMs ms] $message")
|
||||
|
@@ -22,6 +22,9 @@ import java.net.Proxy
|
||||
import java.util.Deque
|
||||
import java.util.concurrent.ConcurrentLinkedDeque
|
||||
import java.util.concurrent.TimeUnit
|
||||
import okhttp3.CallEvent.CacheConditionalHit
|
||||
import okhttp3.CallEvent.CacheHit
|
||||
import okhttp3.CallEvent.CacheMiss
|
||||
import okhttp3.CallEvent.CallEnd
|
||||
import okhttp3.CallEvent.CallFailed
|
||||
import okhttp3.CallEvent.CallStart
|
||||
@@ -45,6 +48,7 @@ import okhttp3.CallEvent.ResponseBodyStart
|
||||
import okhttp3.CallEvent.ResponseFailed
|
||||
import okhttp3.CallEvent.ResponseHeadersEnd
|
||||
import okhttp3.CallEvent.ResponseHeadersStart
|
||||
import okhttp3.CallEvent.SatisfactionFailure
|
||||
import okhttp3.CallEvent.SecureConnectEnd
|
||||
import okhttp3.CallEvent.SecureConnectStart
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
@@ -260,4 +264,21 @@ open class RecordingEventListener : EventListener() {
|
||||
override fun canceled(
|
||||
call: Call
|
||||
) = logEvent(Canceled(System.nanoTime(), call))
|
||||
|
||||
override fun satisfactionFailure(
|
||||
call: Call,
|
||||
response: Response
|
||||
) = logEvent(SatisfactionFailure(System.nanoTime(), call))
|
||||
|
||||
override fun cacheMiss(
|
||||
call: Call
|
||||
) = logEvent(CacheMiss(System.nanoTime(), call))
|
||||
|
||||
override fun cacheHit(
|
||||
call: Call,
|
||||
response: Response
|
||||
) = logEvent(CacheHit(System.nanoTime(), call))
|
||||
|
||||
override fun cacheConditionalHit(call: Call, cachedResponse: Response) =
|
||||
logEvent(CacheConditionalHit(System.nanoTime(), call))
|
||||
}
|
||||
|
Reference in New Issue
Block a user