The time-to-live cache is one that evicts items that are older than a time-to-live threshold (in milliseconds).
Before reading this page please make sure that you've read and understand the Basic Usage patterns.
To create a core.cache TTLCacheQ instance you should always use its associated constructor function ttl-cache-factory with an optional :ttl parameter:
(ns your.lib
(:require [clojure.core.cache :as cache]))
(def C (cache/ttl-cache-factory {} :ttl 1000))
(-> C (assoc :a 1) (assoc :b 2))
;=> {:a 1, :b 2}
note: the default :ttl value is 2 seconds
At this point the cache is fresh and younger than one second (that is, depending on how fast you read), but if you execute yet another call the story will change:
(def sleepy #(do (Thread/sleep %2) %))
(-> C (assoc :a 1)
(assoc :b 2)
(sleepy 1500)
(assoc :c 3))
;=> {:c 3}
At this point the operation of the TTL cache is exposed. As you see, sleeping in between adding the keys :a and :b causes them to be evicted on the next insertion.
All caveats apply regarding the proper usage patterns.
The TTL cache eviction policy is very simple to understand and works well for certain scenarios. However, because the policy requires "age" information, the implementation is somewhat subtle and moderately memory intensive.
There are a few reasons why you might want to use a TTL cache:
As always, you should measure your system's characteristics to determine the best eviction strategy for your purposes.
Can you improve this documentation? These fine people already did:
Sean Corfield, Fogus & fogusEdit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |