我有一个基于 ring/compojure 的 Web API,我需要能够根据启动标志或将参数传递给请求来选择性地打开和关闭缓存(或任何标志)。
我尝试将标志设置为动态变量:
(def ^:dynamic *cache* true)
(defmacro cache [source record options & body]
`(let [cachekey# (gen-cachekey ~source ~record ~options)]
(if-let [cacheval# (if (and (:ttl ~source) ~*cache*) (mc/generic-get cachekey#) nil)]
cacheval#
(let [ret# (do ~@body)]
(if (and (:ttl ~source) ~*cache*) (mc/generic-set cachekey# ret# :ttl (:ttl ~source)))
ret#))))
...但这仅允许我更新绑定块中的标志,这对于包装每个数据获取功能并不理想,并且不允许我在启动时选择性地设置标志
然后我尝试在原子中设置标志,这允许我在启动时设置标志,如果某个参数被传递给请求,则可以轻松更新标志,但更新将更改所有线程的标志和不仅仅是特定请求的标志。
在 Clojure 中做这样的事情最惯用的方法是什么?