-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCache.kt
More file actions
31 lines (26 loc) · 1.42 KB
/
Copy pathSimpleCache.kt
File metadata and controls
31 lines (26 loc) · 1.42 KB
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
import java.util.concurrent.ConcurrentHashMap
//TODO: There is no garbage collection implemented and no size bound, so the cache will grow forever keeping expired and obsolete values indefinitely, which eventually will cause a memory leak
class SimpleCache<K, V> {
private val cache = ConcurrentHashMap<K, CacheEntry<V>>()
///TODO: Hardcoded TTL could be replaced with configurable value for more flexibility
private val ttlMs = 60000 // 1 minute
data class CacheEntry<V>(val value: V, val timestamp: Long)
fun put(key: K, value: V) {
///TODO: Use nanoTime instead of currentTimeMillis to avoid breaking the expiration logic in case the clock changes for whatever reason
cache[key] = CacheEntry(value, System.currentTimeMillis())
}
fun get(key: K): V? {
val entry = cache[key]
if (entry != null) {
///TODO: Use nanoTime instead of currentTimeMillis to avoid breaking the expiration logic in case the clock changes for whatever reason
if (System.currentTimeMillis() - entry.timestamp < ttlMs) {
return entry.value
}
}
return null
}
///TODO: Even though this function will work, the result is misleading as it will also include expired entries. A more accurate size will be returned if a garbage collector is implemented to remove expired entries
fun size(): Int {
return cache.size
}
}