Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Increase verbosity in the caching middleware (#68121)

daaab9fc000abe
diff --git a/src/metabase/query_processor/middleware/cache.clj b/src/metabase/query_processor/middleware/cache.clj
index 6fe4f832e4d..1d922ab6dd5 100644
--- a/src/metabase/query_processor/middleware/cache.clj
+++ b/src/metabase/query_processor/middleware/cache.clj
@@ -9,6 +9,7 @@
about how the cache backends themselves."
(:refer-clojure :exclude [get-in])
(:require
+ [clojure.string :as str]
[java-time.api :as t]
[medley.core :as m]
[metabase.cache.core :as cache]
@@ -222,10 +223,38 @@
(fn [metadata]
(save-results-xform start-time-ns metadata query-hash cache-strategy (rff metadata)))))))))
-(defn- is-cacheable? [{:keys [cache-strategy], :as _query}]
- (and (cache/enable-query-caching)
- (some? cache-strategy)
- (not= (:type cache-strategy) :nocache)))
+(defn- caching-enabled? []
+ (cache/enable-query-caching))
+
+(defn- has-cache-strategy? [cache-strategy]
+ (some? cache-strategy))
+
+(defn- strategy-not-nocache? [cache-strategy]
+ (not= (:type cache-strategy) :nocache))
+
+(defn- is-cacheable?
+ "Returns true if query caching is enabled and the query has a valid cache strategy."
+ [{:keys [cache-strategy], :as _query}]
+ (let [enabled? (caching-enabled?)
+ has-strat? (has-cache-strategy? cache-strategy)
+ not-nocache? (strategy-not-nocache? cache-strategy)]
+ (and enabled? has-strat? not-nocache?)))
+
+(defn- get-cache-eligibility-description
+ "Returns a descriptive string explaining why a query is or isn't cacheable."
+ [{:keys [cache-strategy], :as _query}]
+ (let [enabled? (caching-enabled?)
+ has-strat? (has-cache-strategy? cache-strategy)
+ not-nocache? (strategy-not-nocache? cache-strategy)]
+ (if (and enabled? has-strat? not-nocache?)
+ (str "query caching is enabled; "
+ "cache strategy provided: " (pr-str cache-strategy) "; "
+ "cache strategy type is not :nocache")
+ (str/join ", "
+ (cond-> []
+ (not enabled?) (conj "query caching is disabled")
+ (not has-strat?) (conj "no cache strategy provided")
+ (not not-nocache?) (conj "cache strategy is :nocache"))))))
(mu/defn maybe-return-cached-results :- ::qp.schema/qp
"Middleware for caching results of a query if applicable.
@@ -241,8 +270,9 @@
* The result *rows* of the query must be less than `query-caching-max-kb` when serialized (before compression)."
[qp :- ::qp.schema/qp]
(fn maybe-return-cached-results* [query rff]
- (let [cacheable? (is-cacheable? query)]
- (log/tracef "Query is cacheable? %s" (boolean cacheable?))
+ (let [cacheable? (is-cacheable? query)
+ description (get-cache-eligibility-description query)]
+ (log/tracef "Query is %scacheable: %s" (if-not cacheable? "not " "") description)
(if cacheable?
(run-query-with-cache qp query rff)
(qp query rff)))))