https://github.com/lsevero/memoize-ttl
A simple caching TTL strategy
https://github.com/lsevero/memoize-ttl
cache clojure ttl-cache
Last synced: 8 months ago
JSON representation
A simple caching TTL strategy
- Host: GitHub
- URL: https://github.com/lsevero/memoize-ttl
- Owner: lsevero
- License: epl-2.0
- Created: 2021-03-03T22:27:25.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-05-04T15:41:09.000Z (about 5 years ago)
- Last Synced: 2025-10-06T13:56:32.406Z (8 months ago)
- Topics: cache, clojure, ttl-cache
- Language: Clojure
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# memoize-ttl
[](https://clojars.org/lsevero/memoize-ttl)
A `clojure.core/memoize` version with a time-to-live strategy.
It mimics the interface of memoize and it is much more simplistic than [core.cache](https://github.com/clojure/core.cache).
If you need more control on the cache, consider switching to core.cache instead.
This library has no dependencies (besides clojure).
## Usage
Just pass the function you want to memoize to `memoize-ttl`, this function also receives a :ttl optional argument in seconds (defaults to 1 hour).
```clojure
(require '[memoize-ttl.core :refer [memoize-ttl]])
(let [f (memoize-ttl (fn []
(let [now (java.time.Instant/now)]
(println "executing function f at: " now)
now)) :ttl 10)
changes (atom 0)
last-ret (atom nil)]
(doseq [i (range 20)]
(let [ret (f)]
(println "f returns: " ret)
(when (not= ret @last-ret)
(do (swap! changes inc)
(reset! last-ret ret)))
(Thread/sleep 1000))))
```
We also provides the `memoize-ttl-clean` function which returns a vector of the memoized function and a function to wipe the entire cache.
## License
Copyright © 2021 Lucas Severo.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License 2.0 which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the Eclipse
Public License, v. 2.0 are satisfied: GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or (at your
option) any later version, with the GNU Classpath Exception which is available
at https://www.gnu.org/software/classpath/license.html.