-
Notifications
You must be signed in to change notification settings - Fork 63
puredanger edited this page May 22, 2012
·
10 revisions
The least-recently-used cache is one that evicts items that are used least frequently once its limit has been exceeded.
To create a core.cache LRUCache
instance you can do the following:
(ns your.lib
(:require [clojure.core.cache :as cache]))
(def C (cache/lru-cache-factory 2 {}))
(-> C (assoc :a 1) (assoc :b 2))
;=> {:a 1, :b 2}
At this point the cache has not yet crossed the set limit of 2
, but if you execute yet another call the story will change:
(-> C (assoc :a 1) (assoc :b 2) (assoc :c 3))
;=> {:b 2, :c 3}
At this point the operation of the LRU cache looks exactly the same at the FIFO cache. However, the difference becomes apparent when a given cache item is "touched":
(-> C (assoc :a 1)
(assoc :b 2)
(.hit :a) ;; ensure :a is used most recently
(assoc :c 3))
;=> {:a 1, :c 3}
As you see, hitting the key :a
will expose the LRU nature of the underlying cache. That is, when the threshold is passed, the cache will expel the Least Recently Used element in favor of the new. In this base case the items accessed most recetly were :c
and :a
.
TODO