Scoop API

scoop:scoop.http.client:0.1.0

src/cache/memory.scoop

Selected production source · src/cache/memory.scoop

1package scoop.http.client
3import scoop.http.*
5/**
6 * A bounded in-memory cache with least-recently-used eviction. Entries become visible only after a
7 * successful commit.
8 */
9public 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 = 1024L
22) : 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 = 0L
27 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 returned
42 * snapshot.
43 */
44 public override suspend fun lookup(request: HttpCacheRequest): HttpCacheSnapshot? = lock.withLock {
45 var index = entries.size
46 while (index > 0L) {
47 index -= 1L
48 val entry = entries[index]
49 if (!entry.record.matches(request)) continue
50 entries.removeAt(index); entries.add(entry)
51 return@withLock Some<HttpCacheSnapshot>(ClientMemoryCacheSnapshot(entry))
52 }
53 None
54 }
56 /**
57 * Starts a tentative body write or returns None when storage declines it; only commit
58 * publishes the entry.
59 */
60 public override suspend fun begin(record: HttpCacheRecord): HttpCacheWriter? {
61 val limit = if (maxEntryBytes < maxBytes) maxEntryBytes else maxBytes
62 val length = parseHttpContentLength(record.head.fields)
63 if (clientPresent(length) && length!! > limit) return None
64 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.size
75 while (index > 0L) {
76 index -= 1L
77 val key = entries[index].record.key
78 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 = 0L
90 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@withLock
97 for (entry in entries) if (replacedClientCacheVariant(entry.record, writer.record) &&
98 entry.record.responseDate > writer.record.responseDate) return@withLock
99 var index = entries.size
100 while (index > 0L) {
101 index -= 1L
102 if (!replacedClientCacheVariant(entries[index].record, writer.record)) continue
103 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.size
108 }
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) }
118internal class ClientMemoryCacheEntry(val record: HttpCacheRecord, val bytes: Array<Byte>, val trailers: Array<HttpField>)