On Mon, Oct 22, 2012 at 1:50 PM, Hussein B. <[email protected]> wrote:
> c3 holds a map containing {:a 1} that will lives for two minutes.
In the code I provided, c3 is an atom that holds a cache (which is the map).
> After two minutes, requesting :a is generating false since it reached its
> TTL but it will still live in map until it is removed explicitly by invoking
> evict.
Because the cache itself is immutable. That's why you need to store
the cache in an atom (so the atom can be updated to contain the
modified cache):
(defn hit-or-miss
"Given an atom containing a cache, a key, and a value, update the
cache and return..."
[a k v]
(if (cache/has? @a k)
(cache/hit @a k)
(cache/miss @a k v)))
... (swap! c3 hit-or-miss :c 42) ...
> On Monday, October 22, 2012 11:15:06 PM UTC+3, Sean Corfield wrote:
>> In other words you need something like this:
>>
>> (def c3 (atom (cache/ttl-cache-factory {:a 1} :ttl 20000)))
>> user=> @c3
>> {:a 1}
>> user=> (cache/has? @c3 :a)
>> true
>> user=> (cache/has? @c3 :a)
>> false
>> user=> @c3
>> {:a 1}
>> user=> (swap! c3 cache/evict :a)
>> {}
>> user=> @c3
>> {}
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en