/
CacheControl.kt
219 lines (178 loc) · 7.86 KB
/
CacheControl.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright (C) 2019 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:OptIn(ExperimentalTime::class)
package okhttp3
import java.util.concurrent.TimeUnit
import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
import okhttp3.internal.commonBuild
import okhttp3.internal.commonClampToInt
import okhttp3.internal.commonForceCache
import okhttp3.internal.commonForceNetwork
import okhttp3.internal.commonImmutable
import okhttp3.internal.commonNoCache
import okhttp3.internal.commonNoStore
import okhttp3.internal.commonNoTransform
import okhttp3.internal.commonOnlyIfCached
import okhttp3.internal.commonParse
import okhttp3.internal.commonToString
actual class CacheControl internal actual constructor(
@get:JvmName("noCache") actual val noCache: Boolean,
@get:JvmName("noStore") actual val noStore: Boolean,
@get:JvmName("maxAgeSeconds") actual val maxAgeSeconds: Int,
@get:JvmName("sMaxAgeSeconds") actual val sMaxAgeSeconds: Int,
actual val isPrivate: Boolean,
actual val isPublic: Boolean,
@get:JvmName("mustRevalidate") actual val mustRevalidate: Boolean,
@get:JvmName("maxStaleSeconds") actual val maxStaleSeconds: Int,
@get:JvmName("minFreshSeconds") actual val minFreshSeconds: Int,
@get:JvmName("onlyIfCached") actual val onlyIfCached: Boolean,
@get:JvmName("noTransform") actual val noTransform: Boolean,
@get:JvmName("immutable") actual val immutable: Boolean,
internal actual var headerValue: String?
) {
@JvmName("-deprecated_noCache")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "noCache"),
level = DeprecationLevel.ERROR)
fun noCache(): Boolean = noCache
@JvmName("-deprecated_noStore")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "noStore"),
level = DeprecationLevel.ERROR)
fun noStore(): Boolean = noStore
@JvmName("-deprecated_maxAgeSeconds")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "maxAgeSeconds"),
level = DeprecationLevel.ERROR)
fun maxAgeSeconds(): Int = maxAgeSeconds
@JvmName("-deprecated_sMaxAgeSeconds")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "sMaxAgeSeconds"),
level = DeprecationLevel.ERROR)
fun sMaxAgeSeconds(): Int = sMaxAgeSeconds
@JvmName("-deprecated_mustRevalidate")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "mustRevalidate"),
level = DeprecationLevel.ERROR)
fun mustRevalidate(): Boolean = mustRevalidate
@JvmName("-deprecated_maxStaleSeconds")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "maxStaleSeconds"),
level = DeprecationLevel.ERROR)
fun maxStaleSeconds(): Int = maxStaleSeconds
@JvmName("-deprecated_minFreshSeconds")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "minFreshSeconds"),
level = DeprecationLevel.ERROR)
fun minFreshSeconds(): Int = minFreshSeconds
@JvmName("-deprecated_onlyIfCached")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "onlyIfCached"),
level = DeprecationLevel.ERROR)
fun onlyIfCached(): Boolean = onlyIfCached
@JvmName("-deprecated_noTransform")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "noTransform"),
level = DeprecationLevel.ERROR)
fun noTransform(): Boolean = noTransform
@JvmName("-deprecated_immutable")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "immutable"),
level = DeprecationLevel.ERROR)
fun immutable(): Boolean = immutable
actual override fun toString(): String = commonToString()
actual class Builder {
internal actual var noCache: Boolean = false
internal actual var noStore: Boolean = false
internal actual var maxAgeSeconds = -1
internal actual var maxStaleSeconds = -1
internal actual var minFreshSeconds = -1
internal actual var onlyIfCached: Boolean = false
internal actual var noTransform: Boolean = false
internal actual var immutable: Boolean = false
actual fun noCache() = commonNoCache()
actual fun noStore() = commonNoStore()
actual fun onlyIfCached() = commonOnlyIfCached()
actual fun noTransform() = commonNoTransform()
actual fun immutable() = commonImmutable()
// We are compiling with kotlin 1.6 but need to run with older versions at runtime. For
// maximum compat we therefore need to handle both the case where DurationUnit is typealiased
// to TimeUnit (as it was pre-1.6) and where it is a distinct wrapper enum (in 1.6). We also
// can't use durationUnit.toTimeUnit() because that doesn't exist in the typealiased case.
// This function should work in either case.
internal fun toJavaTimeUnit(durationUnit: DurationUnit) = TimeUnit.valueOf(durationUnit.name)
actual fun maxAge(maxAge: Int, timeUnit: DurationUnit) = maxAge(maxAge, toJavaTimeUnit(timeUnit))
actual fun maxStale(maxStale: Int, timeUnit: DurationUnit) = maxStale(maxStale, toJavaTimeUnit(timeUnit))
actual fun minFresh(minFresh: Int, timeUnit: DurationUnit) = minFresh(minFresh, toJavaTimeUnit(timeUnit))
/**
* Sets the maximum age of a cached response. If the cache response's age exceeds [maxAge], it
* will not be used and a network request will be made.
*
* @param maxAge a non-negative integer. This is stored and transmitted with [TimeUnit.SECONDS]
* precision; finer precision will be lost.
*/
fun maxAge(maxAge: Int, timeUnit: TimeUnit) = apply {
require(maxAge >= 0) { "maxAge < 0: $maxAge" }
val maxAgeSecondsLong = timeUnit.toSeconds(maxAge.toLong())
this.maxAgeSeconds = maxAgeSecondsLong.commonClampToInt()
}
/**
* Accept cached responses that have exceeded their freshness lifetime by up to `maxStale`. If
* unspecified, stale cache responses will not be used.
*
* @param maxStale a non-negative integer. This is stored and transmitted with
* [TimeUnit.SECONDS] precision; finer precision will be lost.
*/
fun maxStale(maxStale: Int, timeUnit: TimeUnit) = apply {
require(maxStale >= 0) { "maxStale < 0: $maxStale" }
val maxStaleSecondsLong = timeUnit.toSeconds(maxStale.toLong())
this.maxStaleSeconds = maxStaleSecondsLong.commonClampToInt()
}
/**
* Sets the minimum number of seconds that a response will continue to be fresh for. If the
* response will be stale when [minFresh] have elapsed, the cached response will not be used and
* a network request will be made.
*
* @param minFresh a non-negative integer. This is stored and transmitted with
* [TimeUnit.SECONDS] precision; finer precision will be lost.
*/
fun minFresh(minFresh: Int, timeUnit: TimeUnit) = apply {
require(minFresh >= 0) { "minFresh < 0: $minFresh" }
val minFreshSecondsLong = timeUnit.toSeconds(minFresh.toLong())
this.minFreshSeconds = minFreshSecondsLong.commonClampToInt()
}
actual fun build(): CacheControl = commonBuild()
}
actual companion object {
@JvmField
actual val FORCE_NETWORK = commonForceNetwork()
@JvmField
actual val FORCE_CACHE = commonForceCache()
@JvmStatic
actual fun parse(headers: Headers): CacheControl = commonParse(headers)
}
}