HttpClientCache

internal class HttpClientCache(maxCacheSize: Int = 100, maxAge: Duration = 30.minutes, maxIdleTime: Duration = 10.minutes, cleanupInterval: Duration = 5.minutes)(source)

LRU cache for Ktor HttpClient instances keyed by authentication credentials.

Caches HTTP clients to avoid creating a new client per scrape request. Entries are evicted when they exceed maxAge, sit idle longer than maxIdleTime, or when the cache exceeds maxCacheSize (least-recently-used eviction). A background coroutine periodically cleans up expired entries. Credential-based keys are masked in logs to prevent leaking secrets.

Parameters

maxCacheSize

maximum number of cached clients

maxAge

maximum lifetime of a cache entry

maxIdleTime

maximum idle time before an entry is considered expired

cleanupInterval

interval between background cleanup sweeps

See also

Constructors

Link copied to clipboard
constructor(maxCacheSize: Int = 100, maxAge: Duration = 30.minutes, maxIdleTime: Duration = 10.minutes, cleanupInterval: Duration = 5.minutes)

Types

Link copied to clipboard
class CacheEntry(val client: HttpClient, val createdAt: Long = System.currentTimeMillis(), var lastAccessedAt: Long = System.currentTimeMillis())
Link copied to clipboard
data class CacheStats(val totalEntries: Int, val validEntries: Int, val expiredEntries: Int)
Link copied to clipboard
data class ClientKey(val username: String?, val password: String?)
Link copied to clipboard
object Companion

Functions

Link copied to clipboard
suspend fun close()
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
suspend fun getOrCreateClient(key: HttpClientCache.ClientKey, clientFactory: () -> HttpClient): HttpClientCache.CacheEntry
Link copied to clipboard