I tried this: (defn hit-or-miss [a k v] (if (c/has? @a k) (c/hit @a k) (c/miss @a k v)))
(def acu (atom (c/ttl-cache-factory {} :ttl 20000))) (swap! acu hit-or-miss :e 55) But I got: ClassCastException clojure.core.cache.TTLCache cannot be cast to clojure.lang.IDeref clojure.core/deref (core.clj:2080) Any ideas? On Tuesday, October 23, 2012 1:03:10 AM UTC+3, Sean Corfield wrote: > > Just to clarify, hit does nothing for TTL (since items timeout based > solely on when they were added, not when they were last touched), so > swap! on the hit really makes no difference here. It would, however, > be required for some of the other types of cache. > > Also, exactly how you use has? / hit / miss is going to depend on how > you're interacting with the cache and what behavior you want. > > For example, at World Singles, we use TTL caches and have three > operations: > * evict - this uses swap! and cache/evict to remove a cache entry > * fetch - this uses get to return an entry if present > * store - this uses swap! and cache/miss to add/update a cache entry > > Since hit doesn't do anything on a TTL cache, we don't bother calling > it. If we switch to other types of cache, our fetch operation would > need to be updated to use cache/has?, swap! and cache/hit (as well as > get), or we'd need to change our API somewhat... > > It's my understanding that you can use assoc / dissoc on a cache as > synonyms for miss / evict (that seemed to be true with the version of > core.cache that I initially used - I'm fairly confident it's still > true of assoc but not so confident that dissoc still works that way... > maybe Fogus can help me out there?). > > Sean > > On Mon, Oct 22, 2012 at 2:31 PM, Sean Corfield > <seanco...@gmail.com<javascript:>> > wrote: > > On Mon, Oct 22, 2012 at 1:50 PM, Hussein B. > > <hubag...@gmail.com<javascript:>> > 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 > >>> {} > > > > -- > Sean A Corfield -- (904) 302-SEAN > An Architect's View -- http://corfield.org/ > World Singles, LLC. -- http://worldsingles.com/ > > "Perfection is the enemy of the good." > -- Gustave Flaubert, French realist novelist (1821-1880) > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en