valcapability=10valcache=new simplelru.mutable.LRU[String, Int](10)
for (elem <-Range.inclusive(0, 200)) {
cache.add(s"key$elem", elem)
}
assert(cache.length == capability)
assert(cache.headOption.contains(200))
// accessing an item should move it to the front of the list
cache.get("key195")
assert(cache.headOption.contains(195))
LRU cache example (mutable and thread-safe)
valcapability=10valcache=new simplelru.mutable.LRU.blocking[String, Int](10)
for (elem <-Range.inclusive(0, 200)) {
cache.add(s"key$elem", elem)
}
assert(cache.length == capability)
assert(cache.headOption.contains(200))
// accessing an item should move it to the front of the list
cache.get("key195")
assert(cache.headOption.contains(195))
LRU cache example (immutable)
valemptyCache= simplelru.immutable.LRU.empty[String, Int](10)
valcapability=10valcache=Range.inclusive(0, 200)
.foldLeft(emptyCache) {
case (cache, elem) => cache.add(s"key$elem", elem)._1
}
assert(cache.length == capability)
assert(cache.headOption.contains(200))
// accessing an item should move it to the front of the listval (updateCache, _) = cache.get("key195")
assert(updateCache.headOption.contains(195))