{"id":23070161,"url":"https://github.com/strojure/memoize-one","last_synced_at":"2025-04-03T10:14:11.307Z","repository":{"id":80284966,"uuid":"498751594","full_name":"strojure/memoize-one","owner":"strojure","description":"Clojure library for memoization of the single value.","archived":false,"fork":false,"pushed_at":"2023-03-09T14:47:42.000Z","size":23,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"default","last_synced_at":"2025-02-08T23:27:38.045Z","etag":null,"topics":["cache","clojure","memoization"],"latest_commit_sha":null,"homepage":"","language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/strojure.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-01T13:39:06.000Z","updated_at":"2023-07-25T14:57:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"3d974301-5894-4245-8291-f767522ff431","html_url":"https://github.com/strojure/memoize-one","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strojure%2Fmemoize-one","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strojure%2Fmemoize-one/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strojure%2Fmemoize-one/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strojure%2Fmemoize-one/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/strojure","download_url":"https://codeload.github.com/strojure/memoize-one/tar.gz/refs/heads/default","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246981166,"owners_count":20863828,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","clojure","memoization"],"created_at":"2024-12-16T06:19:52.029Z","updated_at":"2025-04-03T10:14:11.256Z","avatar_url":"https://github.com/strojure.png","language":"Clojure","funding_links":[],"categories":[],"sub_categories":[],"readme":"# memoize-one\n\nClojure library for memoization of the single value.\n\n[![Clojars Project](https://img.shields.io/clojars/v/com.github.strojure/memoize-one.svg)](https://clojars.org/com.github.strojure/memoize-one)\n\n## Design goals\n\n* Simple utility for caching single value accessed from concurrent threads.\n* Lazy initialization of the cached value on access.\n* Performant access to the cached value.\n* Optional cache expiration.\n* Native implementation without external dependencies.\n\n## Basic usage\n\n```clojure\n(ns readme.manual\n  \"Example of using of the manual cache.\"\n  (:require [strojure.memoize-one.core :as memoize]\n            [strojure.memoize-one.manual-cache :as cache]))\n\n(defn- test-cache\n  [side-effect]\n  (cache/memoize (fn [] (let [v (System/currentTimeMillis)]\n                          (println (str \"Create val \" v \" \" (rand-int 100)))\n                          (side-effect)\n                          v))))\n\n(comment\n  (let [c (test-cache #(Thread/sleep 100))]\n    (dotimes [i 10]\n      (future\n        (Thread/sleep (* i 50))\n        (let [v (memoize/get-ref c)]\n          @v\n          (memoize/evict v)))))\n\n  (def c (test-cache #(Thread/sleep 100)))\n  (def c (test-cache #(when (pos? (rand-int 2)) (throw (Exception. \"Oops\")))))\n\n  (memoize/get-ref c)\n\n  (deref (memoize/get-ref c))                     ; Execution time mean : 7.299272 ns\n  #_1654093121258\n\n  (.deref (memoize/get-ref c))                    ; Execution time mean : 6.074256 ns\n\n  (memoize/get-value c)                           ; Execution time mean : 6.343759 ns\n\n  (doto (memoize/get-ref c) (deref) (memoize/evict))\n  )\n\n```\n\n```clojure\n(ns readme.expiring\n  \"Example of using of the expiring cache.\"\n  (:require [strojure.memoize-one.core :as memoize]\n            [strojure.memoize-one.expiring-cache :as cache]))\n\n(def ^:private test-fn\n  (fn []\n    (let [v (System/currentTimeMillis)]\n      (println (str \"Create val \" v \" \" (rand-int 100)))\n      (Thread/sleep 100)\n      #_(when (pos? (rand-int 2))\n          (throw (Exception. \"Oops\")))\n      v)))\n\n(def ttl-cache\n  \"TTL cache for 20 seconds.\"\n  (cache/memoize-ttl 20000 test-fn))\n\n(comment\n  (deref (memoize/get-ref ttl-cache))             ; Execution time mean : 17,081336 ns\n  ;Create val 1654094164999 12\n  #_1654094164999\n  (memoize/evict (memoize/get-ref ttl-cache))\n  #_nil\n  )\n\n(defn- odd-test-fn\n  [x]\n  (fn [] (odd? x)))\n\n(def odd-cache\n  \"Cache expiring using `odd-test-fn`.\"\n  (cache/memoize-with odd-test-fn test-fn))\n\n(comment\n  (deref (memoize/get-ref odd-cache))             ; Execution time mean : 16,548488 ns\n  ;Create val 1654094271591 0\n  #_1654094271591\n  (memoize/evict (memoize/get-ref odd-cache))\n  #_nil\n  )\n\n(defn- combined-test-fn\n  \"Expiration test for TTL or `odd`.\"\n  [ttl]\n  (fn [x] (let [p1 ((cache/ttl-test-fn ttl) x)\n                p2 (odd-test-fn x)]\n            (fn []\n              (or (p1) (p2))))))\n\n(def combined-cache\n  \"Cache expiring using `combined-test-fn`.\"\n  (cache/memoize-with (combined-test-fn 20000) test-fn))\n\n(comment\n  (deref (memoize/get-ref combined-cache))        ; Execution time mean : 26,168754 ns\n  ;Create val 1654094360990 6\n  #_1654094360990\n  (memoize/evict (memoize/get-ref combined-cache))\n  #_nil\n  )\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrojure%2Fmemoize-one","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstrojure%2Fmemoize-one","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrojure%2Fmemoize-one/lists"}