Skip to content

Instantly share code, notes, and snippets.

@michaelklishin michaelklishin/cache.clj Secret
Created Apr 11, 2012

Embed
What would you like to do?
(ns ^{:doc "clojure.core.cache implementation(s) on top of MongoDB."
:author "Michael S. Klishin"}
monger.cache
(:require [monger.collection :as mc]
[clojure.core.cache :as cache])
(:use monger.conversion)
(:import [clojure.core.cache CacheProtocol]))
;;
;; Implementation
;;
(def ^{:const true}
default-cache-collection "cache_entries")
;;
;; API
;;
(defrecord BasicMongerCache [collection])
(extend-protocol clojure.core.cache.CacheProtocol
BasicMongerCache
(lookup [c k]
(mc/find-map-by-id (get c :collection) k))
(lookup [c k not-found]
(if-let [v (mc/find-map-by-id (get c :collection) k)]
(:value v)
not-found))
(has? [c k]
(mc/any? (get c :collection) {:_id key}))
(hit [this k]
this)
(miss [c k v]
(mc/insert (get c :collection) {:_id k :value v}))
(evict [c k]
(mc/remove-by-id (get c :collection) k))
(seed [c m]
(mc/insert-batch (get c :collection) (map (fn [[k v]]
{:_id k :value v}) m))))
(defn basic-monger-cache-factory
[coll base]
(cache/seed (BasicMongerCache. collection) base))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.