1
0
mirror of https://github.com/square/okhttp.git synced 2025-08-06 01:35:50 +03:00

Change parameter order for Cache constructor (#8338)

* Change parameter order for Cache constructor

Put FileSystem before Path, as is convention.

Also put this new constructor in the public API.

* apiDump

* Track the change in RunSurvey

* Track signature change
This commit is contained in:
Jesse Wilson
2024-04-10 15:46:48 -04:00
committed by GitHub
parent 5f008f382d
commit d2e91f7cb4
9 changed files with 23 additions and 24 deletions

View File

@@ -51,7 +51,7 @@ class RobolectricOkHttpClientTest {
context = ApplicationProvider.getApplicationContext<Application>() context = ApplicationProvider.getApplicationContext<Application>()
client = client =
OkHttpClient.Builder() OkHttpClient.Builder()
.cache(Cache("/cache".toPath(), 10_000_000, FakeFileSystem())) .cache(Cache(FakeFileSystem(), "/cache".toPath(), 10_000_000))
.build() .build()
} }

View File

@@ -170,7 +170,7 @@ class DnsOverHttpsTest {
// 5. unsuccessful response // 5. unsuccessful response
@Test @Test
fun usesCache() { fun usesCache() {
val cache = Cache("cache".toPath(), (100 * 1024).toLong(), cacheFs) val cache = Cache(cacheFs, "cache".toPath(), (100 * 1024).toLong())
val cachedClient = bootstrapClient.newBuilder().cache(cache).build() val cachedClient = bootstrapClient.newBuilder().cache(cache).build()
val cachedDns = buildLocalhost(cachedClient, false) val cachedDns = buildLocalhost(cachedClient, false)
@@ -208,7 +208,7 @@ class DnsOverHttpsTest {
@Test @Test
fun usesCacheEvenForPost() { fun usesCacheEvenForPost() {
val cache = Cache("cache".toPath(), (100 * 1024).toLong(), cacheFs) val cache = Cache(cacheFs, "cache".toPath(), (100 * 1024).toLong())
val cachedClient = bootstrapClient.newBuilder().cache(cache).build() val cachedClient = bootstrapClient.newBuilder().cache(cache).build()
val cachedDns = buildLocalhost(cachedClient, false, post = true) val cachedDns = buildLocalhost(cachedClient, false, post = true)
repeat(2) { repeat(2) {

View File

@@ -68,7 +68,7 @@ public final class okhttp3/Cache : java/io/Closeable, java/io/Flushable {
public static final field Companion Lokhttp3/Cache$Companion; public static final field Companion Lokhttp3/Cache$Companion;
public final fun -deprecated_directory ()Ljava/io/File; public final fun -deprecated_directory ()Ljava/io/File;
public fun <init> (Ljava/io/File;J)V public fun <init> (Ljava/io/File;J)V
public fun <init> (Lokio/Path;JLokio/FileSystem;)V public fun <init> (Lokio/FileSystem;Lokio/Path;J)V
public fun close ()V public fun close ()V
public final fun delete ()V public final fun delete ()V
public final fun directory ()Ljava/io/File; public final fun directory ()Ljava/io/File;

View File

@@ -151,11 +151,11 @@ class Cache internal constructor(
fileSystem: FileSystem, fileSystem: FileSystem,
taskRunner: TaskRunner, taskRunner: TaskRunner,
) : Closeable, Flushable { ) : Closeable, Flushable {
@ExperimentalOkHttpApi /** Create a cache of at most [maxSize] bytes in [directory]. */
constructor( constructor(
fileSystem: FileSystem,
directory: Path, directory: Path,
maxSize: Long, maxSize: Long,
fileSystem: FileSystem,
) : this( ) : this(
directory, directory,
maxSize, maxSize,
@@ -163,6 +163,13 @@ class Cache internal constructor(
TaskRunner.INSTANCE, TaskRunner.INSTANCE,
) )
/** Create a cache of at most [maxSize] bytes in [directory]. */
constructor(directory: File, maxSize: Long) : this(
FileSystem.SYSTEM,
directory.toOkioPath(),
maxSize,
)
internal val cache = internal val cache =
DiskLruCache( DiskLruCache(
fileSystem = fileSystem, fileSystem = fileSystem,
@@ -183,13 +190,6 @@ class Cache internal constructor(
val isClosed: Boolean val isClosed: Boolean
get() = cache.isClosed() get() = cache.isClosed()
/** Create a cache of at most [maxSize] bytes in [directory]. */
constructor(directory: File, maxSize: Long) : this(
directory.toOkioPath(),
maxSize,
FileSystem.SYSTEM,
)
internal fun get(request: Request): Response? { internal fun get(request: Request): Response? {
val key = key(request.url) val key = key(request.url)
val snapshot: DiskLruCache.Snapshot = val snapshot: DiskLruCache.Snapshot =
@@ -389,7 +389,6 @@ class Cache internal constructor(
get() = cache.directory.toFile() get() = cache.directory.toFile()
@get:JvmName("directoryPath") @get:JvmName("directoryPath")
@ExperimentalOkHttpApi
val directoryPath: Path val directoryPath: Path
get() = cache.directory get() = cache.directory

View File

@@ -29,7 +29,7 @@ internal fun buildCache(
maxSize: Long, maxSize: Long,
fileSystem: FileSystem, fileSystem: FileSystem,
): Cache { ): Cache {
return Cache(file, maxSize, fileSystem) return Cache(fileSystem, file, maxSize)
} }
internal var RealConnection.idleAtNsAccessor: Long internal var RealConnection.idleAtNsAccessor: Long

View File

@@ -92,7 +92,7 @@ class CacheTest {
platform.assumeNotOpenJSSE() platform.assumeNotOpenJSSE()
server.protocolNegotiationEnabled = false server.protocolNegotiationEnabled = false
fileSystem.emulateUnix() fileSystem.emulateUnix()
cache = Cache("/cache/".toPath(), Long.MAX_VALUE, fileSystem) cache = Cache(fileSystem, "/cache/".toPath(), Long.MAX_VALUE)
client = client =
clientTestRule.newClientBuilder() clientTestRule.newClientBuilder()
.cache(cache) .cache(cache)
@@ -2758,7 +2758,7 @@ CLEAN $urlKey ${entryMetadata.length} ${entryBody.length}
writeFile(cache.directoryPath, "$urlKey.0", entryMetadata) writeFile(cache.directoryPath, "$urlKey.0", entryMetadata)
writeFile(cache.directoryPath, "$urlKey.1", entryBody) writeFile(cache.directoryPath, "$urlKey.1", entryBody)
writeFile(cache.directoryPath, "journal", journalBody) writeFile(cache.directoryPath, "journal", journalBody)
cache = Cache(cache.directory.path.toPath(), Int.MAX_VALUE.toLong(), fileSystem) cache = Cache(fileSystem, cache.directory.path.toPath(), Int.MAX_VALUE.toLong())
client = client =
client.newBuilder() client.newBuilder()
.cache(cache) .cache(cache)
@@ -2807,7 +2807,7 @@ CLEAN $urlKey ${entryMetadata.length} ${entryBody.length}
writeFile(cache.directoryPath, "$urlKey.1", entryBody) writeFile(cache.directoryPath, "$urlKey.1", entryBody)
writeFile(cache.directoryPath, "journal", journalBody) writeFile(cache.directoryPath, "journal", journalBody)
cache.close() cache.close()
cache = Cache(cache.directory.path.toPath(), Int.MAX_VALUE.toLong(), fileSystem) cache = Cache(fileSystem, cache.directory.path.toPath(), Int.MAX_VALUE.toLong())
client = client =
client.newBuilder() client.newBuilder()
.cache(cache) .cache(cache)
@@ -2860,7 +2860,7 @@ CLEAN $urlKey ${entryMetadata.length} ${entryBody.length}
writeFile(cache.directoryPath, "$urlKey.1", entryBody) writeFile(cache.directoryPath, "$urlKey.1", entryBody)
writeFile(cache.directoryPath, "journal", journalBody) writeFile(cache.directoryPath, "journal", journalBody)
cache.close() cache.close()
cache = Cache(cache.directory.path.toPath(), Int.MAX_VALUE.toLong(), fileSystem) cache = Cache(fileSystem, cache.directory.path.toPath(), Int.MAX_VALUE.toLong())
client = client =
client.newBuilder() client.newBuilder()
.cache(cache) .cache(cache)
@@ -2905,7 +2905,7 @@ CLEAN $urlKey ${entryMetadata.length} ${entryBody.length}
writeFile(cache.directoryPath, "$urlKey.1", entryBody) writeFile(cache.directoryPath, "$urlKey.1", entryBody)
writeFile(cache.directoryPath, "journal", journalBody) writeFile(cache.directoryPath, "journal", journalBody)
cache.close() cache.close()
cache = Cache(cache.directory.path.toPath(), Int.MAX_VALUE.toLong(), fileSystem) cache = Cache(fileSystem, cache.directory.path.toPath(), Int.MAX_VALUE.toLong())
client = client =
client.newBuilder() client.newBuilder()
.cache(cache) .cache(cache)
@@ -3496,7 +3496,7 @@ CLEAN $urlKey ${entryMetadata.length} ${entryBody.length}
} }
} }
val path: Path = "/cache".toPath() val path: Path = "/cache".toPath()
val c = Cache(path, 100000L, loggingFileSystem) val c = Cache(loggingFileSystem, path, 100000L)
assertThat(c.directoryPath).isEqualTo(path) assertThat(c.directoryPath).isEqualTo(path)
c.size() c.size()
assertThat(events).containsExactly( assertThat(events).containsExactly(

View File

@@ -146,9 +146,9 @@ open class CallTest {
private val callback = RecordingCallback() private val callback = RecordingCallback()
private val cache = private val cache =
Cache( Cache(
fileSystem = LoggingFilesystem(fileSystem),
directory = "/cache".toPath(), directory = "/cache".toPath(),
maxSize = Int.MAX_VALUE.toLong(), maxSize = Int.MAX_VALUE.toLong(),
fileSystem = LoggingFilesystem(fileSystem),
) )
@BeforeEach @BeforeEach

View File

@@ -125,7 +125,7 @@ class HttpOverHttp2Test {
private lateinit var protocol: Protocol private lateinit var protocol: Protocol
private lateinit var client: OkHttpClient private lateinit var client: OkHttpClient
private val fileSystem: FakeFileSystem = FakeFileSystem() private val fileSystem: FakeFileSystem = FakeFileSystem()
private val cache: Cache = Cache("/tmp/cache".toPath(), Long.MAX_VALUE, fileSystem) private val cache: Cache = Cache(fileSystem, "/tmp/cache".toPath(), Long.MAX_VALUE)
private lateinit var scheme: String private lateinit var scheme: String
private fun configureClientTestRule(): OkHttpClientTestRule { private fun configureClientTestRule(): OkHttpClientTestRule {

View File

@@ -31,7 +31,7 @@ suspend fun main() {
val client = val client =
OkHttpClient.Builder() OkHttpClient.Builder()
.cache(Cache("build/okhttp_cache".toPath(), 100_000_000, FileSystem.SYSTEM)) .cache(Cache(FileSystem.SYSTEM, "build/okhttp_cache".toPath(), 100_000_000))
.build() .build()
val sslLabsClients = SslLabsClient(client).clients() val sslLabsClients = SslLabsClient(client).clients()