Skip to content

Instantly share code, notes, and snippets.

@michaelklishin
Created April 11, 2012 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelklishin/48422a63cbbec00a1e0e to your computer and use it in GitHub Desktop.
Save michaelklishin/48422a63cbbec00a1e0e to your computer and use it in GitHub Desktop.
(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