scoop:scoop.http.client:0.1.0
src/cache/memory.scoop
Selected production source · src/cache/memory.scoop
1
package scoop.http.client3
import scoop.http.*5
/**6
* A bounded in-memory cache with least-recently-used eviction. Entries become visible only after a7
* successful commit.8
*/9
public class MemoryHttpCache public constructor(10
/**11
* Maximum total accounted storage bytes.12
*/13
public val maxBytes: Long = 33554432L,14
/**15
* Maximum body bytes accepted for one entry.16
*/17
public val maxEntryBytes: Long = 8388608L,18
/**19
* Maximum committed entry count.20
*/21
public val maxEntries: Long = 1024L22
) : HttpCache {23
private val lock: ClientLock = ClientLock()24
private val entries: ArrayList<ClientMemoryCacheEntry> = ArrayList<ClientMemoryCacheEntry>()25
private val writers: ArrayList<ClientMemoryCacheWriter> = ArrayList<ClientMemoryCacheWriter>()26
private var used: Long = 0L27
init {28
if (maxBytes <= 0L || maxEntryBytes <= 0L || maxEntries <= 0L)29
throw IllegalArgumentException(Some("HTTP cache limits must be positive"))30
}31
/**32
* Current accounted committed storage bytes.33
*/34
public val sizeBytes: Long get() = lock.withLock { used }35
/**36
* Current committed entry count.37
*/38
public val entryCount: Long get() = lock.withLock { entries.size }40
/**41
* Returns an owned matching representation snapshot, or None. The caller closes a returned42
* snapshot.43
*/44
public override suspend fun lookup(request: HttpCacheRequest): HttpCacheSnapshot? = lock.withLock {45
var index = entries.size46
while (index > 0L) {47
index -= 1L48
val entry = entries[index]49
if (!entry.record.matches(request)) continue50
entries.removeAt(index); entries.add(entry)51
return@withLock Some<HttpCacheSnapshot>(ClientMemoryCacheSnapshot(entry))52
}53
None54
}56
/**57
* Starts a tentative body write or returns None when storage declines it; only commit58
* publishes the entry.59
*/60
public override suspend fun begin(record: HttpCacheRecord): HttpCacheWriter? {61
val limit = if (maxEntryBytes < maxBytes) maxEntryBytes else maxBytes62
val length = parseHttpContentLength(record.head.fields)63
if (clientPresent(length) && length!! > limit) return None64
val writer = ClientMemoryCacheWriter(this, record, limit)65
lock.withLock { writers.add(writer) }66
return Some<HttpCacheWriter>(writer)67
}69
/**70
* Invalidates all identities and Vary variants for the selected partition and URL.71
*/72
public override suspend fun invalidate(partition: String, url: String) {73
lock.withLock {74
var index = entries.size75
while (index > 0L) {76
index -= 1L77
val key = entries[index].record.key78
if (key.partition == partition && key.url == url) remove(index)79
}80
for (writer in writers) if (writer.record.key.partition == partition && writer.record.key.url == url)81
writer.discard.store(true)82
}83
}85
/**86
* Invalidates all entries and prevents existing tentative writes from publishing.87
*/88
public fun clear() { lock.withLock {89
entries.clear(); used = 0L90
for (writer in writers) writer.discard.store(true)91
} }93
internal fun publish(writer: ClientMemoryCacheWriter, bytes: Array<Byte>, trailers: Array<HttpField>) {94
lock.withLock {95
forget(writer)96
if (writer.discard.load()) return@withLock97
for (entry in entries) if (replacedClientCacheVariant(entry.record, writer.record) &&98
entry.record.responseDate > writer.record.responseDate) return@withLock99
var index = entries.size100
while (index > 0L) {101
index -= 1L102
if (!replacedClientCacheVariant(entries[index].record, writer.record)) continue103
remove(index)104
}105
while (entries.size >= maxEntries || used > maxBytes - bytes.size) remove(0L)106
entries.add(ClientMemoryCacheEntry(writer.record, bytes, trailers))107
used += bytes.size108
}109
}111
internal fun abandon(writer: ClientMemoryCacheWriter) { lock.withLock { forget(writer) } }112
private fun forget(writer: ClientMemoryCacheWriter) {113
for (index in 0L..<writers.size) if (writers[index] === writer) { writers.removeAt(index); return }114
}115
private fun remove(index: Long) { used -= entries[index].bytes.size; entries.removeAt(index) }116
}118
internal class ClientMemoryCacheEntry(val record: HttpCacheRecord, val bytes: Array<Byte>, val trailers: Array<HttpField>)