Skip to content

Commit

Permalink
Make it optional
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke committed Jan 11, 2025
1 parent 019560c commit 433f6f7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 15 deletions.
11 changes: 7 additions & 4 deletions okhttp/src/commonJvmAndroid/kotlin/okhttp3/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,19 @@ class Cache internal constructor(
maxSize: Long,
fileSystem: FileSystem,
taskRunner: TaskRunner,
useCacheLock: Boolean,
) : Closeable, Flushable {
/** Create a cache of at most [maxSize] bytes in [directory]. */
constructor(
fileSystem: FileSystem,
directory: Path,
maxSize: Long,
) : this(
directory,
maxSize,
fileSystem,
TaskRunner.INSTANCE,
directory = directory,
maxSize = maxSize,
fileSystem = fileSystem,
taskRunner = TaskRunner.INSTANCE,
useCacheLock = true,
)

/** Create a cache of at most [maxSize] bytes in [directory]. */
Expand All @@ -178,6 +180,7 @@ class Cache internal constructor(
valueCount = ENTRY_COUNT,
maxSize = maxSize,
taskRunner = taskRunner,
useCacheLock = useCacheLock,
)

// read and write statistics, all guarded by 'this'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class DiskLruCache(
maxSize: Long,
/** Used for asynchronous journal rebuilds. */
taskRunner: TaskRunner,
private val useCacheLock: Boolean,
) : Closeable, Flushable {
lateinit var cacheLock: Closeable

Expand Down Expand Up @@ -245,7 +246,7 @@ class DiskLruCache(

civilizedFileSystem = fileSystem.isCivilized(journalFileBackup)

cacheLock = openLock(fileSystem, directory)
cacheLock = if (useCacheLock) openLock(fileSystem, directory) else Closeable {}

try {
// Prefer to pick up where we left off.
Expand Down
100 changes: 90 additions & 10 deletions okhttp/src/jvmTest/kotlin/okhttp3/internal/cache/DiskLruCacheTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ class DiskLruCacheTest {

private fun createNewCacheWithSize(maxSize: Int) {
cache =
DiskLruCache(filesystem, cacheDir, appVersion, 2, maxSize.toLong(), taskRunner).also {
DiskLruCache(
fileSystem = filesystem,
directory = cacheDir,
appVersion = appVersion,
valueCount = 2,
maxSize = maxSize.toLong(),
taskRunner = taskRunner,
useCacheLock = true,
).also {
toClose.add(it)
}
synchronized(cache) { cache.initialize() }
Expand Down Expand Up @@ -145,7 +153,15 @@ class DiskLruCacheTest {
filesystem.setFaultyDelete(cacheDir / "k1.0.tmp", true)
filesystem.setFaultyDelete(cacheDir, true)
cache =
DiskLruCache(filesystem, cacheDir, appVersion, 2, Int.MAX_VALUE.toLong(), taskRunner).also {
DiskLruCache(
fileSystem = filesystem,
directory = cacheDir,
appVersion = appVersion,
valueCount = 2,
maxSize = Int.MAX_VALUE.toLong(),
taskRunner = taskRunner,
useCacheLock = true,
).also {
toClose.add(it)
}
assertFailsWith<IOException> {
Expand Down Expand Up @@ -816,7 +832,15 @@ class DiskLruCacheTest {
fun constructorDoesNotAllowZeroCacheSize(parameters: Pair<FileSystem, Boolean>) {
setUp(parameters.first, parameters.second)
assertFailsWith<IllegalArgumentException> {
DiskLruCache(filesystem, cacheDir, appVersion, 2, 0, taskRunner)
DiskLruCache(
fileSystem = filesystem,
directory = cacheDir,
appVersion = appVersion,
valueCount = 2,
maxSize = 0,
taskRunner = taskRunner,
useCacheLock = true,
)
}
}

Expand All @@ -825,7 +849,15 @@ class DiskLruCacheTest {
fun constructorDoesNotAllowZeroValuesPerEntry(parameters: Pair<FileSystem, Boolean>) {
setUp(parameters.first, parameters.second)
assertFailsWith<IllegalArgumentException> {
DiskLruCache(filesystem, cacheDir, appVersion, 0, 10, taskRunner)
DiskLruCache(
fileSystem = filesystem,
directory = cacheDir,
appVersion = appVersion,
valueCount = 0,
maxSize = 10,
taskRunner = taskRunner,
useCacheLock = true,
)
}
}

Expand Down Expand Up @@ -1163,7 +1195,15 @@ class DiskLruCacheTest {
cache.close()
val dir = (cacheDir / "testOpenCreatesDirectoryIfNecessary").also { filesystem.createDirectories(it) }
cache =
DiskLruCache(filesystem, dir, appVersion, 2, Int.MAX_VALUE.toLong(), taskRunner).also {
DiskLruCache(
fileSystem = filesystem,
directory = dir,
appVersion = appVersion,
valueCount = 2,
maxSize = Int.MAX_VALUE.toLong(),
taskRunner = taskRunner,
useCacheLock = true,
).also {
toClose.add(it)
}
set("a", "a", "a")
Expand Down Expand Up @@ -1557,7 +1597,15 @@ class DiskLruCacheTest {
setUp(parameters.first, parameters.second)
// Create an uninitialized cache.
cache =
DiskLruCache(filesystem, cacheDir, appVersion, 2, Int.MAX_VALUE.toLong(), taskRunner).also {
DiskLruCache(
fileSystem = filesystem,
directory = cacheDir,
appVersion = appVersion,
valueCount = 2,
maxSize = Int.MAX_VALUE.toLong(),
taskRunner = taskRunner,
useCacheLock = true,
).also {
toClose.add(it)
}
assertThat(cache.isClosed()).isFalse()
Expand All @@ -1583,7 +1631,15 @@ class DiskLruCacheTest {
// Confirm that the fault didn't corrupt entries stored before the fault was introduced.
cache.close()
cache =
DiskLruCache(filesystem, cacheDir, appVersion, 2, Int.MAX_VALUE.toLong(), taskRunner).also {
DiskLruCache(
fileSystem = filesystem,
directory = cacheDir,
appVersion = appVersion,
valueCount = 2,
maxSize = Int.MAX_VALUE.toLong(),
taskRunner = taskRunner,
useCacheLock = true,
).also {
toClose.add(it)
}
assertValue("a", "a", "a")
Expand Down Expand Up @@ -1617,7 +1673,15 @@ class DiskLruCacheTest {
// Confirm that the fault didn't corrupt entries stored before the fault was introduced.
cache.close()
cache =
DiskLruCache(filesystem, cacheDir, appVersion, 2, Int.MAX_VALUE.toLong(), taskRunner).also {
DiskLruCache(
fileSystem = filesystem,
directory = cacheDir,
appVersion = appVersion,
valueCount = 2,
maxSize = Int.MAX_VALUE.toLong(),
taskRunner = taskRunner,
useCacheLock = true,
).also {
toClose.add(it)
}
assertValue("a", "a", "a")
Expand Down Expand Up @@ -1647,7 +1711,15 @@ class DiskLruCacheTest {
// Confirm that the fault didn't corrupt entries stored before the fault was introduced.
cache.close()
cache =
DiskLruCache(filesystem, cacheDir, appVersion, 2, Int.MAX_VALUE.toLong(), taskRunner).also {
DiskLruCache(
fileSystem = filesystem,
directory = cacheDir,
appVersion = appVersion,
valueCount = 2,
maxSize = Int.MAX_VALUE.toLong(),
taskRunner = taskRunner,
useCacheLock = true,
).also {
toClose.add(it)
}
assertValue("a", "a", "a")
Expand All @@ -1671,7 +1743,15 @@ class DiskLruCacheTest {
filesystem.setFaultyWrite(journalFile, false)
cache.close()
cache =
DiskLruCache(filesystem, cacheDir, appVersion, 2, Int.MAX_VALUE.toLong(), taskRunner).also {
DiskLruCache(
fileSystem = filesystem,
directory = cacheDir,
appVersion = appVersion,
valueCount = 2,
maxSize = Int.MAX_VALUE.toLong(),
taskRunner = taskRunner,
useCacheLock = true,
).also {
toClose.add(it)
}
assertAbsent("a")
Expand Down

0 comments on commit 433f6f7

Please sign in to comment.