Skip to content

Commit

Permalink
Add cloffeine-based cache
Browse files Browse the repository at this point in the history
  • Loading branch information
janosmeszaros committed Dec 12, 2021
1 parent af34eab commit e3910c8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{:paths ["src"]
:deps {org.postgresql/postgresql {:mvn/version "42.3.1"}
org.clojure/core.cache {:mvn/version "1.0.225"}
com.appsflyer/cloffeine {:mvn/version "1.0.0"}
io.vertx/vertx-pg-client {:mvn/version "4.2.1"}}
:aliases {:test {:extra-paths ["test"]
:extra-deps {org.clojure/clojure {:mvn/version "1.10.3"}
Expand Down
2 changes: 1 addition & 1 deletion src/porsas/async.clj
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
| `:cache` | Optional [[porsas.cache/Cache]] instance to hold the compiled rowmappers"
([] (context {}))
([{:keys [row cache]}]
(let [cache (or cache (cache/create-cache))
(let [cache (or cache (cache/create-caffeine-cache))
->row (fn [_sql ^RowSet rs]
(let [cols (col-map rs)]
(cond
Expand Down
22 changes: 17 additions & 5 deletions src/porsas/cache.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns porsas.cache
(:require [clojure.core.cache.wrapped :as c]))
(:require [clojure.core.cache.wrapped :as c]
[cloffeine.cache :as cache]))

(defprotocol Cache
(lookup-or-set [this k value-fn] "Lookup a value in the cache based on `k` and if not found set its value based on `value-fn` and returns it.")
Expand All @@ -8,11 +9,22 @@
(defprotocol Cached
(cache [this]))

(defrecord CoreCache [a]
(defrecord CoreCache [c]
Cache
(lookup-or-set [this k value-fn]
(c/lookup-or-miss (:a this) k value-fn))
(elements [this] (into {} @(:a this))))
(c/lookup-or-miss (:c this) k value-fn))
(elements [this] (into {} @(:c this))))

(defn create-cache []
(->CoreCache (c/basic-cache-factory {})))
(->CoreCache (c/lru-cache-factory {})))


(defrecord CaffeineCache [c]
Cache
(lookup-or-set [this k value-fn]
(cache/get (:c this) k value-fn))
(elements [this] (into {} (cache/as-map (:c this)))))


(defn create-caffeine-cache []
(->CaffeineCache (cache/make-cache)))
2 changes: 1 addition & 1 deletion src/porsas/jdbc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
| `:cache` | Optional [[porsas.cache/Cache]] instance to hold the compiled rowmappers"
([] (context {}))
([{:keys [row key cache] :or {key (unqualified-key)}}]
(let [c (or cache (cache/create-cache))
(let [c (or cache (cache/create-caffeine-cache))
->row (fn [_sql rs]
(let [cols (col-map rs key)]
(cond
Expand Down
2 changes: 1 addition & 1 deletion src/porsas/next.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
([]
(caching-row-builder (pj/qualified-key)))
([key]
(let [cache (cache/create-cache)]
(let [cache (cache/create-caffeine-cache)]
(fn [^ResultSet rs opts]
(let [sql (:next.jdbc/sql-string opts)
->row (cache/lookup-or-set cache sql (fn [_] (p/rs-> 1 nil (map last (pj/col-map rs key)))))]
Expand Down

0 comments on commit e3910c8

Please sign in to comment.