Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use .use() writing metadata to the cache #5793

Merged
merged 1 commit into from
Feb 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 38 additions & 40 deletions okhttp/src/main/java/okhttp3/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,9 @@ class Cache internal constructor(
val snapshot = (cached.body as CacheResponseBody).snapshot
var editor: DiskLruCache.Editor? = null
try {
editor = snapshot.edit() // Returns null if snapshot is not current.
if (editor != null) {
entry.writeTo(editor)
editor.commit()
}
editor = snapshot.edit() ?: return // edit() returns null if snapshot is not current.
entry.writeTo(editor)
editor.commit()
} catch (_: IOException) {
abortQuietly(editor)
}
Expand Down Expand Up @@ -299,7 +297,7 @@ class Cache internal constructor(
return object : MutableIterator<String> {
val delegate: MutableIterator<DiskLruCache.Snapshot> = cache.snapshots()
var nextUrl: String? = null
var canRemove: Boolean = false
var canRemove = false

override fun hasNext(): Boolean {
if (nextUrl != null) return true
Expand Down Expand Up @@ -393,7 +391,7 @@ class Cache internal constructor(
) : CacheRequest {
private val cacheOut: Sink = editor.newSink(ENTRY_BODY)
private val body: Sink
internal var done: Boolean = false
internal var done = false

init {
this.body = object : ForwardingSink(cacheOut) {
Expand Down Expand Up @@ -559,42 +557,42 @@ class Cache internal constructor(

@Throws(IOException::class)
fun writeTo(editor: DiskLruCache.Editor) {
val sink = editor.newSink(ENTRY_METADATA).buffer()
sink.writeUtf8(url).writeByte('\n'.toInt())
sink.writeUtf8(requestMethod).writeByte('\n'.toInt())
sink.writeDecimalLong(varyHeaders.size.toLong()).writeByte('\n'.toInt())
for (i in 0 until varyHeaders.size) {
sink.writeUtf8(varyHeaders.name(i))
editor.newSink(ENTRY_METADATA).buffer().use { sink ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat

sink.writeUtf8(url).writeByte('\n'.toInt())
sink.writeUtf8(requestMethod).writeByte('\n'.toInt())
sink.writeDecimalLong(varyHeaders.size.toLong()).writeByte('\n'.toInt())
for (i in 0 until varyHeaders.size) {
sink.writeUtf8(varyHeaders.name(i))
.writeUtf8(": ")
.writeUtf8(varyHeaders.value(i))
.writeByte('\n'.toInt())
}

sink.writeUtf8(StatusLine(protocol, code, message).toString()).writeByte('\n'.toInt())
sink.writeDecimalLong((responseHeaders.size + 2).toLong()).writeByte('\n'.toInt())
for (i in 0 until responseHeaders.size) {
sink.writeUtf8(responseHeaders.name(i))
.writeUtf8(": ")
.writeUtf8(responseHeaders.value(i))
.writeByte('\n'.toInt())
}
sink.writeUtf8(SENT_MILLIS)
.writeUtf8(": ")
.writeUtf8(varyHeaders.value(i))
.writeDecimalLong(sentRequestMillis)
.writeByte('\n'.toInt())
}

sink.writeUtf8(StatusLine(protocol, code, message).toString()).writeByte('\n'.toInt())
sink.writeDecimalLong((responseHeaders.size + 2).toLong()).writeByte('\n'.toInt())
for (i in 0 until responseHeaders.size) {
sink.writeUtf8(responseHeaders.name(i))
sink.writeUtf8(RECEIVED_MILLIS)
.writeUtf8(": ")
.writeUtf8(responseHeaders.value(i))
.writeDecimalLong(receivedResponseMillis)
.writeByte('\n'.toInt())

if (isHttps) {
sink.writeByte('\n'.toInt())
sink.writeUtf8(handshake!!.cipherSuite.javaName).writeByte('\n'.toInt())
writeCertList(sink, handshake.peerCertificates)
writeCertList(sink, handshake.localCertificates)
sink.writeUtf8(handshake.tlsVersion.javaName).writeByte('\n'.toInt())
}
}
sink.writeUtf8(SENT_MILLIS)
.writeUtf8(": ")
.writeDecimalLong(sentRequestMillis)
.writeByte('\n'.toInt())
sink.writeUtf8(RECEIVED_MILLIS)
.writeUtf8(": ")
.writeDecimalLong(receivedResponseMillis)
.writeByte('\n'.toInt())

if (isHttps) {
sink.writeByte('\n'.toInt())
sink.writeUtf8(handshake!!.cipherSuite.javaName).writeByte('\n'.toInt())
writeCertList(sink, handshake.peerCertificates)
writeCertList(sink, handshake.localCertificates)
sink.writeUtf8(handshake.tlsVersion.javaName).writeByte('\n'.toInt())
}
sink.close()
}

@Throws(IOException::class)
Expand Down Expand Up @@ -660,10 +658,10 @@ class Cache internal constructor(

companion object {
/** Synthetic response header: the local time when the request was sent. */
private val SENT_MILLIS = Platform.get().getPrefix() + "-Sent-Millis"
private val SENT_MILLIS = "${Platform.get().getPrefix()}-Sent-Millis"

/** Synthetic response header: the local time when the response was received. */
private val RECEIVED_MILLIS = Platform.get().getPrefix() + "-Received-Millis"
private val RECEIVED_MILLIS = "${Platform.get().getPrefix()}-Received-Millis"
}
}

Expand Down