Public Vars

Back

memoize (clj)

(source)

function

(memoize f)
Returns a memoized version of a referentially transparent function. The memoized version of the function keeps a cache of the mapping from arguments to results and, when calls with the same arguments are repeated often, has higher performance at the expense of higher memory use.

Examples

metabase/metabase
(ns metabase.util.memoize
  (:require
   [clojure.core.memoize :as memoize]
   [metabase.shared.util.namespaces :as shared.ns]))

(comment
  memoize/keep-me)

(shared.ns/import-fns
 [memoize
  lru
  memoizer])
mhuebert/maria
(ns cells.cell
  (:refer-clojure :exclude [bound-fn get])
  (:require [clojure.core :as core]
            [cells.util :as util]
            [applied-science.js-interop :as j]))

(defmacro memoized-on [o k & body]
  `(let [o# ~o]
     (~'applied-science.js-interop/get o# ~k
      (let [v# (do ~@body)]
        (~'applied-science.js-interop/assoc! o# ~k)
        v#))))
immutant/immutant
(ns immutant.caching.core-memoize-test
  (:require [clojure.test :refer :all]
            [clojure.core.cache :refer [seed]]
            [immutant.caching :refer [cache stop]]
            [immutant.caching.core-memoize :refer [memo]]))

(deftest should-memoize
  (stop "test")
  (let [f (fn [] (Thread/sleep 500) "boo")
        m (memo f "test")]
    (is (> (timeit (m)) 0.48))
    (is (< (timeit (m)) 0.5))
    (is (= (m) "boo"))))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
  "Type annotations for the base Clojure distribution."
  #?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
  (:require [clojure.core :as cc]
            [typed.clojure :as t]
            #?(:clj [typed.ann-macros.clojure :as macros])
            #?(:clj typed.ann.clojure.jvm) ;; jvm annotations
            #?(:clj clojure.core.typed))
  #?(:clj
     (:import (clojure.lang PersistentHashSet PersistentList
                            APersistentMap #_IPersistentCollection
                            #_ITransientSet
                            IRef)
              (java.util Comparator Collection))))


cc/doall (t/All [[c :< (t/U nil t/AnySeqable)]]
                [(t/? t/AnyInteger) c :-> c])
cc/dorun [(t/? t/AnyInteger) t/AnySeqable :-> nil]
cc/iterate (t/All [x]
                  [[x :-> x] x :-> (t/NonEmptyASeq x)])
cc/memoize (t/All [x y :..]
                  [[y :.. y :-> x] :-> [y :.. y :-> x]])
footprintanalytics/footprint-web
(ns metabase.public-settings-test
  (:require [clj-http.fake :as http-fake]
            [clojure.core.memoize :as memoize]
            [clojure.test :refer :all]
            [metabase.models.setting :as setting]
            [metabase.public-settings :as public-settings]
            [metabase.public-settings.premium-features :as premium-features]
            [metabase.test :as mt]
            [metabase.test.fixtures :as fixtures]
            [metabase.util.i18n :as i18n :refer [tru]]))

(deftest cloud-gateway-ips-test
  (with-redefs [premium-features/is-hosted? (constantly true)]
    (testing "Setting calls Store URL to fetch IP addresses"
      (memoize/memo-clear! @#'public-settings/fetch-cloud-gateway-ips-fn)
      (http-fake/with-fake-routes-in-isolation
        {{:address (public-settings/cloud-gateway-ips-url)}
         (constantly {:status 200 :body "{\"ip_addresses\": [\"127.0.0.1\"]}"})}
        (is (= ["127.0.0.1"] (public-settings/cloud-gateway-ips))))

      (testing "Getter is memoized to avoid frequent HTTP calls"
        (http-fake/with-fake-routes-in-isolation
          {{:address (public-settings/cloud-gateway-ips-url)}
           (constantly {:status 200 :body "{\"ip_addresses\": [\"0.0.0.0\"]}"})}
          (is (= ["127.0.0.1"] (public-settings/cloud-gateway-ips))))))

    (testing "Setting returns nil if URL is unreachable"
      (memoize/memo-clear! @#'public-settings/fetch-cloud-gateway-ips-fn)
      (http-fake/with-fake-routes-in-isolation
        {{:address (public-settings/cloud-gateway-ips-url)}
         (constantly {:status 500})}
        (is (= nil (public-settings/cloud-gateway-ips))))))

  (testing "Setting returns nil in self-hosted environments"
    (with-redefs [premium-features/is-hosted? (constantly false)]
      (memoize/memo-clear! @#'public-settings/fetch-cloud-gateway-ips-fn)
      (http-fake/with-fake-routes-in-isolation
        {{:address (public-settings/cloud-gateway-ips-url)}
         (constantly {:status 200 :body "{\"ip_addresses\": [\"127.0.0.1\"]}"})}
        (is (= nil (public-settings/cloud-gateway-ips)))))))