diff --git a/deps.edn b/deps.edn index 3995132..627acac 100644 --- a/deps.edn +++ b/deps.edn @@ -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"} diff --git a/src/porsas/async.clj b/src/porsas/async.clj index ae568f4..6408f93 100644 --- a/src/porsas/async.clj +++ b/src/porsas/async.clj @@ -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 diff --git a/src/porsas/cache.clj b/src/porsas/cache.clj index e4fab6f..1eb3915 100644 --- a/src/porsas/cache.clj +++ b/src/porsas/cache.clj @@ -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.") @@ -8,11 +9,20 @@ (defprotocol Cached (cache [this])) -(defrecord CoreCache [a] - Cache - (lookup-or-set [this k value-fn] - (c/lookup-or-miss (:a this) k value-fn)) - (elements [this] (into {} @(:a this)))) +(defrecord CoreCache [c] + Cache + (lookup-or-set [this k value-fn] + (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))) diff --git a/src/porsas/jdbc.clj b/src/porsas/jdbc.clj index 62f6eea..f072566 100644 --- a/src/porsas/jdbc.clj +++ b/src/porsas/jdbc.clj @@ -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 diff --git a/src/porsas/next.clj b/src/porsas/next.clj index 286f204..8da8526 100644 --- a/src/porsas/next.clj +++ b/src/porsas/next.clj @@ -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)))))]