A clojure declarative cache library inspired by Spring Cache and JCache.
:dependencies
[sapphire "0.1.0-beta1"]
(ns sapphire.example
(:require [sapphire.core :refer :all]
[sapphire.cache :as cache]))
(cache/sapphire-init!
:cache-manager (cache/jcache-cache-manager-factory
:fully-qualified-class-name "org.ehcache.jsr107.EhcacheCachingProvider"
:config-file-path "ehcache3.xml"))
:cache-result
)(defcomponent find-user-by-id
"The result is cacheable."
{:cache-result {:cache-name "user"}}
[id]
(prn "Get from database.."))
;; => #'user/find-user-by-id
(find-user-by-id 666)
;; "Get from database.."
;; => nil
(find-user-by-id 666)
;; => nil
:cache-put
)(defcomponent put-user-into-cache
"Explicit put the data into cache."
{:cache-put {:cache-name "user", :key cache/first-param}}
[id user]
(prn "Put the result into cache.")
user)
;; => #'user/put-user-into-cache
(put-user-into-cache 777 {:username "777"})
;; "Put the result into cache."
;; => {:username "777"}
(find-user-by-id 777)
;; => {:username "777"}
:cache-remove
)(defcomponent remove-user-from-cache
"Remove data from cache."
{:cache-remove {:cache-name "user", :key cache/take-all-params}}
[id]
(prn "Remove data from cache by id."))
;; => #'user/remove-user-from-cache
(remove-user-from-cache 777)
;; "Remove data from cache by id."
;; => nil
(find-user-by-id 777)
;; "Get from database.."
;; => nil
:cache-remove-all
)(defcomponent remove-all-user-from-cache
"Remove data from cache."
{:cache-remove-all {:cache-name "user"}}
[]
(prn "Remove all data from cache."))
;; => #'user/remove-all-user-from-cache
(remove-all-user-from-cache)
;; "Remove all data from cache."
;; => nil
(find-user-by-id 666)
;; "Get from database.."
;; => nil
:cache-defaults
)(ns sapphire.example
"You can offer default cache config for all cache components in current namespace."
{:cache-defaults {:cache-name "user"}}
(:require [sapphire.core :refer :all]
[sapphire.cache :as cache]))
By default, sapphire's metadata will not be retained.
Unless you specify :keep-sapphire-meta
on the current namespace or function.
(ns sapphire.example
{:keep-sapphire-meta true}
(:require [sapphire.core :refer :all]
[sapphire.cache :as cache]))
(defcomponent keep-sapphire-metadata
{:keep-sapphire-meta true}
[]
(prn "Do something."))
Macro | Description |
---|---|
defcomponent | Based on clojure.core/defn and has the same syntax. Put the sapphire metadata into attr-map. |
Func | Description |
---|---|
cache/take-all-params | The default key func, take all params as key. |
cache/first-param | Take the first param as key. |
(cache/take-n-params n) | Take n params as key. |
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close