CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-squareup-okhttp3--okhttp

Square's meticulous HTTP client for Java and Kotlin

Pending
Overview
Eval results
Files

caching.mddocs/

Caching

HTTP response caching to improve performance and reduce network usage.

Cache

Stores HTTP responses for reuse to avoid redundant network requests.

class Cache(directory: File, maxSize: Long) : Closeable, Flushable {
    val directory: File
    val directoryPath: Path
    
    fun delete()
    fun evictAll()
    fun urls(): MutableIterator<String>
    fun size(): Long
    fun maxSize(): Long
    fun flush()
    fun close()
    fun writeAbortCount(): Int
    fun writeSuccessCount(): Int
    fun networkCount(): Int
    fun hitCount(): Int
    fun requestCount(): Int
}

Cache Usage

// Create cache directory
val cacheDirectory = File(context.cacheDir, "http_cache")
val cacheSize = 10L * 1024 * 1024 // 10 MiB

// Configure client with cache
val client = OkHttpClient.Builder()
    .cache(Cache(cacheDirectory, cacheSize))
    .build()

CacheControl

Directives that control HTTP caching behavior.

class CacheControl private constructor() {
    val noCache: Boolean
    val noStore: Boolean
    val maxAgeSeconds: Int
    val sMaxAgeSeconds: Int
    val isPrivate: Boolean
    val isPublic: Boolean
    val mustRevalidate: Boolean
    val maxStaleSeconds: Int
    val minFreshSeconds: Int
    val onlyIfCached: Boolean
    val noTransform: Boolean
    val immutable: Boolean
    
    override fun toString(): String
    
    companion object {
        val FORCE_NETWORK: CacheControl
        val FORCE_CACHE: CacheControl
        
        fun parse(headers: Headers): CacheControl
    }
}

CacheControl Builder

class CacheControl.Builder {
    fun noCache(): Builder
    fun noStore(): Builder
    fun maxAge(maxAge: Int, timeUnit: TimeUnit): Builder
    fun maxAge(maxAge: Duration): Builder
    fun maxStale(maxStale: Int, timeUnit: TimeUnit): Builder
    fun maxStale(maxStale: Duration): Builder
    fun minFresh(minFresh: Int, timeUnit: TimeUnit): Builder
    fun minFresh(minFresh: Duration): Builder
    fun onlyIfCached(): Builder
    fun noTransform(): Builder
    fun immutable(): Builder
    fun build(): CacheControl
}

Cache Control Examples

// Force network request (skip cache)
val request = Request.Builder()
    .url("https://api.example.com/data")
    .cacheControl(CacheControl.FORCE_NETWORK)
    .build()

// Only use cached response (don't make network request)
val cachedRequest = Request.Builder()
    .url("https://api.example.com/data")
    .cacheControl(CacheControl.FORCE_CACHE)
    .build()

// Custom cache control
val cacheControl = CacheControl.Builder()
    .maxAge(5, TimeUnit.MINUTES)
    .maxStale(1, TimeUnit.HOURS)
    .build()

val request = Request.Builder()
    .url("https://api.example.com/data")
    .cacheControl(cacheControl)
    .build()

Install with Tessl CLI

npx tessl i tessl/maven-com-squareup-okhttp3--okhttp

docs

caching.md

cookies.md

forms-multipart.md

http-client.md

index.md

interceptors.md

networking.md

requests-responses.md

security.md

urls.md

websockets.md

tile.json