Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion okhttp/src/commonJvmAndroid/kotlin/okhttp3/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class Cache internal constructor(
val snapshot = (cached.body as CacheResponseBody).snapshot
var editor: DiskLruCache.Editor? = null
try {
editor = snapshot.edit() ?: return // edit() returns null if snapshot is not current.
editor = snapshot.editMetadata() ?: return // editMetadata() returns null if snapshot is not current.
entry.writeTo(editor)
editor.commit()
} catch (_: IOException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,14 @@ class DiskLruCache(
fun edit(
key: String,
expectedSequenceNumber: Long = ANY_SEQUENCE_NUMBER,
): Editor? = edit(key, expectedSequenceNumber, allowSourceLocks = false)

@Synchronized
@Throws(IOException::class)
private fun edit(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is internal, I wonder if we should just add the parameter to the existing method?

key: String,
expectedSequenceNumber: Long,
allowSourceLocks: Boolean,
): Editor? {
initialize()

Expand All @@ -495,7 +503,7 @@ class DiskLruCache(
return null // Another edit is in progress.
}

if (entry != null && entry.lockingSourceCount != 0) {
if (!allowSourceLocks && entry != null && entry.lockingSourceCount != 0) {
return null // We can't write this file because a reader is still reading it.
}

Expand Down Expand Up @@ -855,6 +863,9 @@ class DiskLruCache(
@Throws(IOException::class)
fun edit(): Editor? = this@DiskLruCache.edit(key, sequenceNumber)

@Throws(IOException::class)
internal fun editMetadata(): Editor? = this@DiskLruCache.edit(key, sequenceNumber, allowSourceLocks = true)

/** Returns the unbuffered stream with the value for [index]. */
fun getSource(index: Int): Source = sources[index]

Expand Down
10 changes: 10 additions & 0 deletions okhttp/src/jvmTest/kotlin/okhttp3/CacheTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3138,6 +3138,16 @@ class CacheTest {

@Test
fun conditionalHitUpdatesCache() {
assertConditionalHitUpdatesCache()
}

@Test
fun conditionalHitUpdatesCacheOnWindowsFileSystem() {
fileSystem.emulateWindows()
assertConditionalHitUpdatesCache()
}

private fun assertConditionalHitUpdatesCache() {
server.enqueue(
MockResponse
.Builder()
Expand Down
Loading