diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn new file mode 100644 index 0000000..f40cd26 --- /dev/null +++ b/.clj-kondo/config.edn @@ -0,0 +1 @@ +{:lint-as {clojure.core.cache/defcache clojure.core/defrecord}} diff --git a/.github/PULL_REQUEST_TEMPLATE b/.github/PULL_REQUEST_TEMPLATE new file mode 100644 index 0000000..686625a --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE @@ -0,0 +1,14 @@ +Hi! Thanks for your interest in contributing to this project. + +Clojure contrib projects do not use GitHub issues or pull requests, and +require a signed Contributor Agreement. If you would like to contribute, +please read more about the CA and sign that first (this can be done online). + +Then go to this project's issue tracker in JIRA to create tickets, update +tickets, or submit patches. For help in creating tickets and patches, +please see: + +- Signing the CA: https://clojure.org/community/contributing +- Creating Tickets: https://clojure.org/community/creating_tickets +- Developing Patches: https://clojure.org/community/developing_patches +- Contributing FAQ: https://clojure.org/community/contributing diff --git a/.github/workflows/doc-build.yml b/.github/workflows/doc-build.yml new file mode 100644 index 0000000..65beb66 --- /dev/null +++ b/.github/workflows/doc-build.yml @@ -0,0 +1,13 @@ +name: Build API Docs + +permissions: + contents: write + +on: + workflow_dispatch: + +jobs: + call-doc-build-workflow: + uses: clojure/build.ci/.github/workflows/doc-build.yml@master + with: + project: clojure/core.cache diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..286cf95 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,22 @@ +name: Release on demand + +permissions: + contents: write + +on: + workflow_dispatch: + inputs: + releaseVersion: + description: "Version to release" + required: true + snapshotVersion: + description: "Snapshot version after release" + required: true + +jobs: + call-release: + uses: clojure/build.ci/.github/workflows/release.yml@master + with: + releaseVersion: ${{ github.event.inputs.releaseVersion }} + snapshotVersion: ${{ github.event.inputs.snapshotVersion }} + secrets: inherit \ No newline at end of file diff --git a/.github/workflows/snapshot.yml b/.github/workflows/snapshot.yml new file mode 100644 index 0000000..9fdad8c --- /dev/null +++ b/.github/workflows/snapshot.yml @@ -0,0 +1,11 @@ +name: Snapshot on demand + +permissions: + contents: read + +on: [workflow_dispatch] + +jobs: + call-snapshot: + uses: clojure/build.ci/.github/workflows/snapshot.yml@master + secrets: inherit diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..2cc441a --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,10 @@ +name: Test + +permissions: + contents: read + +on: [push] + +jobs: + call-test: + uses: clojure/build.ci/.github/workflows/test.yml@master diff --git a/.gitignore b/.gitignore index b3bd55c..42b8275 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,18 @@ -target +.classpath +.clj-kondo/.cache +.cpcache/ +.idea .lein* +.lsp/.cache +.nrepl-port +.portal/vs-code.edn +.project +.settings +.vscode +/build.boot lib multi-lib -.nrepl-port +target +.calva/repl.calva-repl +.calva/mcp-server/port +.rebel_readline_history diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8cadea7..17e4617 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -3,12 +3,10 @@ This is a [Clojure contrib] project. Under the Clojure contrib [guidelines], this project cannot accept pull requests. All patches must be submitted via [JIRA]. -See [Contributing] and the [FAQ] on the Clojure development [wiki] for +See [Contributing] on the Clojure website for more information on how to contribute. -[Clojure contrib]: http://dev.clojure.org/display/doc/Clojure+Contrib -[Contributing]: http://dev.clojure.org/display/community/Contributing -[FAQ]: http://dev.clojure.org/display/community/Contributing+FAQ -[JIRA]: http://dev.clojure.org/jira/browse/CCACHE -[guidelines]: http://dev.clojure.org/display/community/Guidelines+for+Clojure+Contrib+committers -[wiki]: http://dev.clojure.org/ +[Clojure contrib]: https://clojure.org/community/contrib_libs +[Contributing]: https://clojure.org/community/contributing +[JIRA]: http://clojure.atlassian.net/browse/CCACHE +[guidelines]: https://clojure.org/community/contrib_howto diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e246f6a --- /dev/null +++ b/LICENSE @@ -0,0 +1,205 @@ +Eclipse Public License - v 1.0 + +THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC +LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM +CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + +a) in the case of the initial Contributor, the initial code and documentation + distributed under this Agreement, and +b) in the case of each subsequent Contributor: + i) changes to the Program, and + ii) additions to the Program; + + where such changes and/or additions to the Program originate from and are + distributed by that particular Contributor. A Contribution 'originates' + from a Contributor if it was added to the Program by such Contributor + itself or anyone acting on such Contributor's behalf. Contributions do not + include additions to the Program which: (i) are separate modules of + software distributed in conjunction with the Program under their own + license agreement, and (ii) are not derivative works of the Program. + +"Contributor" means any person or entity that distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which are +necessarily infringed by the use or sale of its Contribution alone or when +combined with the Program. + +"Program" means the Contributions distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement, +including all Contributors. + +2. GRANT OF RIGHTS + a) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free copyright license to + reproduce, prepare derivative works of, publicly display, publicly + perform, distribute and sublicense the Contribution of such Contributor, + if any, and such derivative works, in source code and object code form. + b) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free patent license under + Licensed Patents to make, use, sell, offer to sell, import and otherwise + transfer the Contribution of such Contributor, if any, in source code and + object code form. This patent license shall apply to the combination of + the Contribution and the Program if, at the time the Contribution is + added by the Contributor, such addition of the Contribution causes such + combination to be covered by the Licensed Patents. The patent license + shall not apply to any other combinations which include the Contribution. + No hardware per se is licensed hereunder. + c) Recipient understands that although each Contributor grants the licenses + to its Contributions set forth herein, no assurances are provided by any + Contributor that the Program does not infringe the patent or other + intellectual property rights of any other entity. Each Contributor + disclaims any liability to Recipient for claims brought by any other + entity based on infringement of intellectual property rights or + otherwise. As a condition to exercising the rights and licenses granted + hereunder, each Recipient hereby assumes sole responsibility to secure + any other intellectual property rights needed, if any. For example, if a + third party patent license is required to allow Recipient to distribute + the Program, it is Recipient's responsibility to acquire that license + before distributing the Program. + d) Each Contributor represents that to its knowledge it has sufficient + copyright rights in its Contribution, if any, to grant the copyright + license set forth in this Agreement. + +3. REQUIREMENTS + +A Contributor may choose to distribute the Program in object code form under +its own license agreement, provided that: + + a) it complies with the terms and conditions of this Agreement; and + b) its license agreement: + i) effectively disclaims on behalf of all Contributors all warranties + and conditions, express and implied, including warranties or + conditions of title and non-infringement, and implied warranties or + conditions of merchantability and fitness for a particular purpose; + ii) effectively excludes on behalf of all Contributors all liability for + damages, including direct, indirect, special, incidental and + consequential damages, such as lost profits; + iii) states that any provisions which differ from this Agreement are + offered by that Contributor alone and not by any other party; and + iv) states that source code for the Program is available from such + Contributor, and informs licensees how to obtain it in a reasonable + manner on or through a medium customarily used for software exchange. + +When the Program is made available in source code form: + + a) it must be made available under this Agreement; and + b) a copy of this Agreement must be included with each copy of the Program. + Contributors may not remove or alter any copyright notices contained + within the Program. + +Each Contributor must identify itself as the originator of its Contribution, +if +any, in a manner that reasonably allows subsequent Recipients to identify the +originator of the Contribution. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities with +respect to end users, business partners and the like. While this license is +intended to facilitate the commercial use of the Program, the Contributor who +includes the Program in a commercial product offering should do so in a manner +which does not create potential liability for other Contributors. Therefore, +if a Contributor includes the Program in a commercial product offering, such +Contributor ("Commercial Contributor") hereby agrees to defend and indemnify +every other Contributor ("Indemnified Contributor") against any losses, +damages and costs (collectively "Losses") arising from claims, lawsuits and +other legal actions brought by a third party against the Indemnified +Contributor to the extent caused by the acts or omissions of such Commercial +Contributor in connection with its distribution of the Program in a commercial +product offering. The obligations in this section do not apply to any claims +or Losses relating to any actual or alleged intellectual property +infringement. In order to qualify, an Indemnified Contributor must: +a) promptly notify the Commercial Contributor in writing of such claim, and +b) allow the Commercial Contributor to control, and cooperate with the +Commercial Contributor in, the defense and any related settlement +negotiations. The Indemnified Contributor may participate in any such claim at +its own expense. + +For example, a Contributor might include the Program in a commercial product +offering, Product X. That Contributor is then a Commercial Contributor. If +that Commercial Contributor then makes performance claims, or offers +warranties related to Product X, those performance claims and warranties are +such Commercial Contributor's responsibility alone. Under this section, the +Commercial Contributor would have to defend claims against the other +Contributors related to those performance claims and warranties, and if a +court requires any other Contributor to pay any damages as a result, the +Commercial Contributor must pay those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR +IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, +NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each +Recipient is solely responsible for determining the appropriateness of using +and distributing the Program and assumes all risks associated with its +exercise of rights under this Agreement , including but not limited to the +risks and costs of program errors, compliance with applicable laws, damage to +or loss of data, programs or equipment, and unavailability or interruption of +operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY +CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION +LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of the +remainder of the terms of this Agreement, and without further action by the +parties hereto, such provision shall be reformed to the minimum extent +necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Program itself +(excluding combinations of the Program with other software or hardware) +infringes such Recipient's patent(s), then such Recipient's rights granted +under Section 2(b) shall terminate as of the date such litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it fails to +comply with any of the material terms or conditions of this Agreement and does +not cure such failure in a reasonable period of time after becoming aware of +such noncompliance. If all Recipient's rights under this Agreement terminate, +Recipient agrees to cease use and distribution of the Program as soon as +reasonably practicable. However, Recipient's obligations under this Agreement +and any licenses granted by Recipient relating to the Program shall continue +and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, but in +order to avoid inconsistency the Agreement is copyrighted and may only be +modified in the following manner. The Agreement Steward reserves the right to +publish new versions (including revisions) of this Agreement from time to +time. No one other than the Agreement Steward has the right to modify this +Agreement. The Eclipse Foundation is the initial Agreement Steward. The +Eclipse Foundation may assign the responsibility to serve as the Agreement +Steward to a suitable separate entity. Each new version of the Agreement will +be given a distinguishing version number. The Program (including +Contributions) may always be distributed subject to the version of the +Agreement under which it was received. In addition, after a new version of the +Agreement is published, Contributor may elect to distribute the Program +(including its Contributions) under the new version. Except as expressly +stated in Sections 2(a) and 2(b) above, Recipient receives no rights or +licenses to the intellectual property of any Contributor under this Agreement, +whether expressly, by implication, estoppel or otherwise. All rights in the +Program not expressly granted under this Agreement are reserved. + +This Agreement is governed by the laws of the State of New York and the +intellectual property laws of the United States of America. No party to this +Agreement will bring a legal action under this Agreement more than one year +after the cause of action arose. Each party waives its rights to a jury trial in +any resulting litigation. + + diff --git a/README.md b/README.md index 8ccd38e..8184ee6 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,17 @@ clojure.core.cache ======================================== -core.cache is a new Clojure contrib library providing the following features: +core.cache is a Clojure contrib library providing the following features: * An underlying `CacheProtocol` used as the base abstraction for implementing new synchronous caches * A `defcache` macro for hooking your `CacheProtocol` implementations into the Clojure associative data capabilities. -* Implementations of some basic caching strategies +* Implementations of some basic in-memory caching strategies - First-in-first-out (FIFOCache) - Least-recently-used (LRUCache) - Least-used (LUCache -- sometimes called Least Frequently Used) - - Time-to-live (TTLCache) + - Time-to-live (TTLCacheQ) - Naive cache (BasicCache) - Naive cache backed with soft references (SoftCache) @@ -19,28 +19,41 @@ core.cache is a new Clojure contrib library providing the following features: * Factory functions for each existing cache type -core.cache is based on an old library named Clache that has been thouroughly deprecated. +* Caches are generally in-memory and immutable and should be used in conjunction with Clojure's state management, such as `atom`. SoftCache is the exception here, an in-memory cache built on top of mutable Java collections, but it can be treated as an immutable cache as well. +The `clojure.core.cache` namespace contains the immutable in-memory caches themselves. +The `clojure.core.cache.wrapped` namespace contains the same API operating on caches wrapped in atoms, which is the "normal" use in the wild (introduced in 0.8.0). + +core.cache is based on an old library named Clache that has been thoroughly deprecated. + +The `core.cache` API is hard to use correctly. That's why `clojure.core.cache.wrapped/lookup-or-miss` exists: it encapsulates all the best practices around using the API _and_ wraps it in an `atom` from the get-go. Read this article about [the `core.cache` API](https://dev.to/dpsutton/exploring-the-core-cache-api-57al) by Dan Sutton for why it is important to use the API correctly! Releases and Dependency Information ======================================== -Latest stable release: 0.6.3 +This project follows the version scheme MAJOR.MINOR.COMMITS where MAJOR and MINOR provide some relative indication of the size of the change, but do not follow semantic versioning. In general, all changes endeavor to be non-breaking (by moving to new names rather than by breaking existing names). COMMITS is an ever-increasing counter of commits since the beginning of this repository. -* [All Released Versions](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22core.cache%22) +Latest stable release: 1.2.263 +* [All Released Versions](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22core.cache%22) * [Development Snapshot Versions](https://oss.sonatype.org/index.html#nexus-search;gav~org.clojure~core.cache~~~) +[CLI/`deps.edn`](https://clojure.org/reference/deps_edn) dependency information: + +```clojure +org.clojure/core.cache {:mvn/version "1.2.263"} +``` + [Leiningen](https://github.com/technomancy/leiningen) dependency information: - [org.clojure/core.cache "0.6.3"] + [org.clojure/core.cache "1.2.263"] [Maven](http://maven.apache.org/) dependency information: org.clojure core.cache - 0.6.3 + 1.2.263 @@ -48,42 +61,165 @@ Latest stable release: 0.6.3 Example Usage ======================================== +The [`clojure.core.cache` namespace](https://cljdoc.org/d/org.clojure/core.cache/CURRENT/api/clojure.core.cache) +provides an API for immutable caches where +it is expected that you would manage storage of these data structures. + +The [`clojure.core.cache.wrapped` namespace](https://cljdoc.org/d/org.clojure/core.cache/CURRENT/api/clojure.core.cache.wrapped) +provides the same API but over +immutable caches already wrapped in an atom, which is generally a more +intuitive API for straightforward use cases. In particular, this namespace +adds a `lookup-or-miss` function which encapsulates the `has?`/`hit`/`miss` +logic for the underlying cache as well as edge cases such as `lookup` +returning `nil` if a cache item expires on fetch (e.g., TTL), and guaranteeing +the value computing function is only called at most once. + +The expectation is that you use _either_ `clojure.core.cache` _or_ +`clojure.core.cache.wrapped` and you do not try to mix them. + ```clojure (require '[clojure.core.cache :as cache]) - - (def C (cache/fifo-cache-factory {:a 1, :b 2}) - - (if (cache/has? C :c) - (cache/hit C :c) - (cache/miss C :c 42)) - - ;=> {:a 1, :b 2, :c 42} - - (cache/evict C :b) - - ;=> {:a 1} -``` -Refer to docstrings in the `clojure.core.cache` namespace, or the [autogenerated API documentation](http://clojure.github.com/core.cache/) for additional documentation + ;; C1 is an immutable cache: + (def C1 (cache/fifo-cache-factory {:a 1, :b 2})) + (def C1' (if (cache/has? C1 :c) + (cache/hit C1 :c) + (cache/miss C1 :c 42))) + ;=> {:a 1, :b 2, :c 42} -Developer Information -======================================== + (cache/lookup C1' :c) -* [GitHub project](https://github.com/clojure/core.cache) + ;=> 42 + + (get C1' :c) ; cache/lookup is implemented as get + + ;=> 42 + + ;; a shorthand for the above conditional... + (def C1' (cache/through-cache C1 :c (constantly 42))) + ;; ...which uses a value to compute the result from the key... + (cache/through-cache C1 my-key (partial jdbc/get-by-id db-spec :storage)) + ;; ...so you could fetch values from a database if they're not in cache... + + (cache/evict C1 :b) + + ;=> {:a 1} + + ;; since the caches are immutable, you would normally wrap them in an atom + (def C2 (atom (cache/fifo-cache-factory {:a 1, :b 2}))) + + (swap! C2 cache/through-cache :d (constantly 13)) + + ;=> {:a 1, :b 3, :d 13} + + (swap! C2 cache/evict :b) -* [Bug Tracker](http://dev.clojure.org/jira/browse/CCACHE) + ;=> {:a 1, :d 13} -* [Continuous Integration](http://build.clojure.org/job/core.cache/) + (get @C2 :a) -* [Compatibility Test Matrix](http://build.clojure.org/job/core.cache-test-matrix/) + ;=> 1 + + ;; or use the wrapped API instead: + (require '[clojure.core.cache.wrapped :as w]) + + ;; in this case C3 is an atom containing a cache: + (def C3 (w/fifo-cache-factory {:a 1, :b 2})) + + ;; operations modify the atom and return the updated cache: + (w/through-cache C3 :d (constantly 13)) + + ;=> {:a 1, :b 3, :d 13} + + ;; modifies the atom and returns the updated cache: + (w/evict C3 :b) + + ;=> {:a 1, :d 13} + + ;; for some caches this is a mutating operation (e.g., TTL): + (w/lookup C3 :a) ; or (get @C3 :a) + + ;=> 1 + + ;; unique to the wrapped API, this combines through-cache and lookup + ;; to return the looked up value, possibly after calling the passed in + ;; function with the key to populate the cache if the key wasn't present: + (w/lookup-or-miss C3 :b (constantly 42)) + + ;=> 42 + + @C3 + + ;=> {:a 1, :d 13, :b 42} + + ;; calls (jdbc/get-by-id :storage my-key) in the event of a cache miss: + (w/lookup-or-miss C3 my-key (partial jdbc/get-by-id db-spec :storage)) +``` + +Refer to docstrings in the `clojure.core.cache` or `clojure.core.cache.wrapped` namespaces, or the [autogenerated API documentation](http://clojure.github.io/core.cache/) for additional documentation. + + +Developer Information +======================================== + +* [GitHub project](https://github.com/clojure/core.cache) +* [Bug Tracker](https://clojure.atlassian.net/browse/CCACHE) +* [Continuous Integration](https://github.com/clojure/core.cache/actions/workflows/test.yml) Change Log ==================== +* Release 1.2.263 on 2026-02-25 + * [CCACHE-65](http://clojure.atlassian.net/browse/CCACHE-65) again! This removes `locking` and reverts to regular `delay`/`force` but now has custom evict-on-exception logic. Credit to Claude Opus 4.6 (several iterations). +* Release 1.2.259 on 2026-02-23 + * [CCACHE-65](http://clojure.atlassian.net/browse/CCACHE-65) again! Avoids caching exceptions but currently relies on `locking` to avoid an edge case race condition if the `value-fn` throws an exception under heavy load on `lookup-or-miss`. This is a temporary solution until we can implement a more robust solution that doesn't rely on `locking`. +* Release 1.2.254 on 2026-02-21 + * [CCACHE-67](http://clojure.atlassian.net/browse/CCACHE-67) Add `clojure.core.cache.wrapped/size` function to get the size of the cache without dereferencing the atom. (via [@dharrigan](https://github.com/dharrigan)). + * [CCACHE-65](http://clojure.atlassian.net/browse/CCACHE-65) Use `delay` in `lookup-or-miss` to avoid cache-stampede. +* Release 1.1.234 on 2024-02-19 + * Update parent pom and `data.priority-map` versions +* Release 1.0.225 on 2021-12-06 + * Update `data.priority-map` to 1.1.0 +* Release 1.0.217 on 2021-08-02 + * [CCACHE-63](http://clojure.atlassian.net/browse/CCACHE-63) Improve cache initialization for LU/LRU; fix LU miss logic when not full. +* Release 1.0.207 on 2020-04-10 + * Switch to 1.0.x versioning. + * Update `data.priority-map` to 1.0.0 +* Release 0.8.2 on 2019-09-30 + * [CCACHE-57](http://clojure.atlassian.net/browse/CCACHE-57) Fix wrapped cache `miss` function +* Release 0.8.1 on 2019-08-24 + * [CCACHE-56](http://clojure.atlassian.net/browse/CCACHE-56) Fix wrapped TTL cache and fix `clojure.core.cache.wrapped/lookup-or-miss` for caches that can invalidate on `lookup` +* Release 0.8.0 on 2019-08-24 + * [CCACHE-50](http://clojure.atlassian.net/browse/CCACHE-50) Add `clojure.core.cache.wrapped` namespace with atom-wrapped caches for a more convenient API that adds `lookup-or-miss` which avoids the possibility of cache stampede +* Release 0.7.2 on 2019-01-06 + * [CCACHE-53](http://clojure.atlassian.net/browse/CCACHE-53) Remove unnecessary/additional `.get` call (Neil Prosser) + * [CCACHE-52](http://clojure.atlassian.net/browse/CCACHE-52) Fix NPE in SoftCache (Neil Prosser) +* Release 0.7.1 on 2018.03.02 + * [CCACHE-49](http://clojure.atlassian.net/browse/CCACHE-49) Fix TTLCacheQ `seed` function and expand tests on TTLCacheQ +* Release 0.7.0 on 2018.03.01 + * [CCACHE-46](http://clojure.atlassian.net/browse/CCACHE-46) Fix TTLCache when wrapped around another cache (Ivan Kryvoruchko) + * [CCACHE-43](http://clojure.atlassian.net/browse/CCACHE-43) Add `through-cache` to provide a version of `through` that plays nice with `swap!` etc + * [CCACHE-40](http://clojure.atlassian.net/browse/CCACHE-40) Fix FIFOCache stack overflow on large threshold (uses PersistentQueue now instead of concat and list) + * [CCACHE-39](http://clojure.atlassian.net/browse/CCACHE-39) Fix FIFOCache evict/miss queue handling + * [CCACHE-20](http://clojure.atlassian.net/browse/CCACHE-20) Updated README to clarify that caches are immutable and provide examples of use with `atom` etc + * [CCACHE-15](http://clojure.atlassian.net/browse/CCACHE-15) Added queue and generation logic to reduce `miss` cost and make `evict` O(1); rename TTLCache -> TTLCacheQ (Kevin Downey) + * Drop support for Clojure 1.3/1.4/1.5 +* Release 0.6.5 on 2016.03.28 + * Bump tools.priority-map dependency to 0.0.7 + * [CCACHE-41](http://clojure.atlassian.net/browse/CCACHE-41) Implement Iterable in defcache + * [CCACHE-44](http://clojure.atlassian.net/browse/CCACHE-44) Avoid equals comparison on cache miss + * [CCACHE-37](http://clojure.atlassian.net/browse/CCACHE-37) Fix typo in docstring +* Release 0.6.4 on 2014.08.06 + * Thanks to Paul Stadig and Nicola Mometto who contributed patches for this release + * [CCACHE-34](http://clojure.atlassian.net/browse/CCACHE-34) bump tools.priority-map dependency to 0.0.4 + * [CCACHE-28](http://clojure.atlassian.net/browse/CCACHE-28) concurrency bug in has? for SoftCache + * [CCACHE-29](http://clojure.atlassian.net/browse/CCACHE-29) fix conj implementation for caches + * [CCACHE-30](http://clojure.atlassian.net/browse/CCACHE-30) make-reference need not be dynamic + * [CCACHE-26](http://clojure.atlassian.net/browse/CCACHE-26) hit function in LRU cache can give funny results * Release 0.6.3 on 2013.03.15 * Added through to encapsulate check logic * Release 0.6.2 on 2012.08.07 [more information](http://blog.fogus.me/?p=4527) @@ -103,4 +239,4 @@ Change Log Copyright and License ======================================== -Copyright (c) Rich Hickey, Michael Fogus and contributors, 2012. All rights reserved. The use and distribution terms for this software are covered by the Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html at the root of this distribution. By using this software in any fashion, you are agreeing to be bound bythe terms of this license. You must not remove this notice, or any other, from this software. +Copyright (c) Rich Hickey, Michael Fogus and contributors, 2012-2023. All rights reserved. The use and distribution terms for this software are covered by the Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html at the root of this distribution. By using this software in any fashion, you are agreeing to be bound bythe terms of this license. You must not remove this notice, or any other, from this software. diff --git a/deps.edn b/deps.edn new file mode 100644 index 0000000..5eb3590 --- /dev/null +++ b/deps.edn @@ -0,0 +1,17 @@ +;; You can run clojure.core.cache tests with: clj -A:test:runner +;; You can also specify an alias to select which version of Clojure to test +;; against: :1.9 :1.10 :1.11 :1.12 + +{:paths ["src/main/clojure"] + :deps {org.clojure/data.priority-map {:mvn/version "1.2.1"}} + :aliases {:1.9 {:override-deps {org.clojure/clojure {:mvn/version "1.9.0"}}} + :1.10 {:override-deps {org.clojure/clojure {:mvn/version "1.10.3"}}} + :1.11 {:override-deps {org.clojure/clojure {:mvn/version "1.11.4"}}} + :1.12 {:override-deps {org.clojure/clojure {:mvn/version "1.12.4"}}} + :test + {:extra-paths ["src/test/clojure"] + :extra-deps {org.clojure/test.check {:mvn/version "1.1.3"} + io.github.cognitect-labs/test-runner + {:git/tag "v0.5.1" :git/sha "dfb30dd"}} + :main-opts ["-m" "cognitect.test-runner" + "-d" "src/test/clojure"]}}} diff --git a/docs/Composing.md b/docs/Composing.md index 91975da..98bbe65 100644 --- a/docs/Composing.md +++ b/docs/Composing.md @@ -8,14 +8,14 @@ Put in simpler terms, to create a cache instance composed of the seed data `{:a (ttl-cache-factory :ttl 5000))) ;; used right away - + (assoc C :c 42) -;;=> {:b 2, :c 42} - -;; used after 5 seconds +;;=> {:b 2, :c 42} + +;; used after 5 seconds (assoc C :d 138) ;;=> {:d 138} ``` -What the code above does is to simply use a `FIFOCache` instance, seeded with `{:a 1, :b 2}` as the seed for a `TTLCache` instance. As shown, within the TTL window, the cache evicts its elements using a FIFO policy. However, once a TTL window has expired the expired elements are also evicted. +What the code above does is to simply use a `FIFOCache` instance, seeded with `{:a 1, :b 2}` as the seed for a `TTLCacheQ` instance. As shown, within the TTL window, the cache evicts its elements using a FIFO policy. However, once a TTL window has expired the expired elements are also evicted. diff --git a/docs/Extending.md b/docs/Extending.md index 5855e26..6a1812a 100644 --- a/docs/Extending.md +++ b/docs/Extending.md @@ -100,7 +100,7 @@ The `hit` method in this case is trivial, it just returns the instance itself. threshold))))) ``` -The `miss` method is where all the action takes place. That is, if the size of the base `cache` is less than the `threshold` then the cached value is staored in the base `cache` and its key in the `ks` set. However, if the threshold was breached then the first key in the `ks` set is removed from the base `cache` and `ks` and a new `LameCache` instance is returned with the new key and value added to `cache` and `ks`. +The `miss` method is where all the action takes place. That is, if the size of the base `cache` is less than the `threshold` then the cached value is stored in the base `cache` and its key in the `ks` set. However, if the threshold was breached then the first key in the `ks` set is removed from the base `cache` and `ks` and a new `LameCache` instance is returned with the new key and value added to `cache` and `ks`. ```clojure (evict [_ key] diff --git a/docs/Including.md b/docs/Including.md index 9718bca..e1f1db4 100644 --- a/docs/Including.md +++ b/docs/Including.md @@ -10,7 +10,7 @@ The core.cache releases and snapshots are stored in the following repositories: You can use core.cache in your [Leiningen](https://github.com/technomancy/leiningen) projects with the following `:dependencies` directive in your `project.clj` file: - [org.clojure/core.cache "0.6.3"] + [org.clojure/core.cache "1.0.217"] ## Maven @@ -19,7 +19,7 @@ For Maven-driven projects, use the following slice of XML in your `pom.xml`'s `< org.clojure core.cache - 0.6.3 + 1.0.217 -Enjoy! \ No newline at end of file +Enjoy! diff --git a/docs/LRU.md b/docs/LRU.md index d72af62..f301ece 100644 --- a/docs/LRU.md +++ b/docs/LRU.md @@ -1,7 +1,7 @@ LRU cache ========== -The least-recently-used cache is one that evicts items that are accessed least frequently once its threshold has been exceeded. +The least-recently-used cache is one that evicts items that are accessed least recently once its threshold has been exceeded. > In simple terms, the LRU cache will remove the element in the cache that has not been accessed in the longest time. @@ -13,11 +13,11 @@ General usage To create a core.cache `LRUCache` instance you should *always* use its associated constructor function `lru-cache-factory` with an optional `:threshold` parameter: ```clojure - (ns your.lib + (ns your.lib (:require [clojure.core.cache :as cache])) - + (def C (cache/lru-cache-factory {} :threshold 2)) - + (-> C (assoc :a 1) (assoc :b 2)) ;=> {:a 1, :b 2} ``` @@ -34,11 +34,11 @@ At this point the cache has not yet crossed the set threshold of `2`, but if you At this point the operation of the LRU cache looks exactly the same at the FIFO cache. However, the difference becomes apparent when a given cache item is "touched": ```clojure - (-> C (assoc :a 1) - (assoc :b 2) + (-> C (assoc :a 1) + (assoc :b 2) (.hit :a) ;; ensure :a is used most recently (assoc :c 3)) - + ;=> {:a 1, :c 3} ``` @@ -64,8 +64,8 @@ There are a few reasons why you might want to use a LRU cache: ### Disadvantages to using an LRU cache - * Tends to perform poorly when elements files are accessed occassionally but consistently while other elements are accessed very frequently for a short duration and never accessed again + * Tends to perform poorly when elements files are accessed occasionally but consistently while other elements are accessed very frequently for a short duration and never accessed again * It requires more historical data to operate * It requires a larger cache to increase efficiency - + As always, you should measure your system's characteristics to determine the best eviction strategy for your purposes. diff --git a/docs/Plans.md b/docs/Plans.md index 45fe0b2..17d95e2 100644 --- a/docs/Plans.md +++ b/docs/Plans.md @@ -1,5 +1,7 @@ # Plans for core.cache +These were Fogus's plans for core.cache. See https://clojure.atlassian.net/projects/CCACHE/issues/?filter=allopenissues for what is actually being considered. + * Most Recently Used cache implementation * Function-backed cache implementation * Random eviction cache implementation @@ -10,4 +12,3 @@ * test.generative usage * Explore Arc impl. * More documentation and examples - diff --git a/docs/README.md b/docs/README.md index 3f931c2..1f81d19 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,8 @@ core.cache ========== +> Note: the documentation here is for the `clojure.core.cache` namespace. See also the documentation for the [`clojure.core.cache.wrapped` namespace](https://cljdoc.org/d/org.clojure/core.cache/CURRENT/api/clojure.core.cache.wrapped) on cljdoc.org. + ## Table of Topics * Overview (this page) @@ -42,6 +44,6 @@ core.cache is a Clojure contrib library providing the following features: ## Places * [Source code](https://github.com/clojure/core.cache) -* [Ticket system](http://dev.clojure.org/jira/browse/CCACHE) +* [Ticket system](http://clojure.atlassian.net/browse/CCACHE) * [Announcement](http://groups.google.com/group/clojure/browse_frm/thread/69d08572ab265dc7) * [Examples and documentation](http://github.com/clojure/core.cache/wiki) \ No newline at end of file diff --git a/docs/TTL.md b/docs/TTL.md index a8c7f64..b1981d0 100644 --- a/docs/TTL.md +++ b/docs/TTL.md @@ -8,14 +8,14 @@ The time-to-live cache is one that evicts items that are older than a time-to-li General usage ------------- -To create a core.cache `TTLCache` instance you should *always* use its associated constructor function `ttl-cache-factory` with an optional `:ttl` parameter: +To create a core.cache `TTLCacheQ` instance you should *always* use its associated constructor function `ttl-cache-factory` with an optional `:ttl` parameter: ```clojure - (ns your.lib + (ns your.lib (:require [clojure.core.cache :as cache])) - + (def C (cache/ttl-cache-factory {} :ttl 1000)) - + (-> C (assoc :a 1) (assoc :b 2)) ;=> {:a 1, :b 2} ``` @@ -26,8 +26,8 @@ At this point the cache is fresh and younger than one second (that is, depending ```clojure (def sleepy #(do (Thread/sleep %2) %)) - - (-> C (assoc :a 1) + + (-> C (assoc :a 1) (assoc :b 2) (sleepy 1500) (assoc :c 3)) @@ -55,5 +55,5 @@ There are a few reasons why you might want to use a TTL cache: * It requires more historical data to operate * Cache size does not generally help its efficiency - + As always, you should measure your system's characteristics to determine the best eviction strategy for your purposes. diff --git a/docs/Using.md b/docs/Using.md index ba2792e..2c56f10 100644 --- a/docs/Using.md +++ b/docs/Using.md @@ -1,3 +1,5 @@ +> Note: the documentation here is for the `clojure.core.cache` namespace. See also the documentation for the [`clojure.core.cache.wrapped` namespace](https://cljdoc.org/d/org.clojure/core.cache/CURRENT/api/clojure.core.cache.wrapped) on cljdoc.org. + # Using core.cache *note: see the page on [including core.cache](./Including.md) before you begin this section* @@ -13,7 +15,7 @@ To use the cache implementations or extend the core.cache protocols you first ne Next you should create an instance of a specific cache type, optionally seeded: ```clojure -(def C (cache/fifo-cache-factory {:a 1, :b 2}) +(def C (cache/fifo-cache-factory {:a 1, :b 2})) ``` To find a value in a map by its key you have a couple choices: @@ -32,22 +34,37 @@ To ensure the proper cache policies are followed for each specific type, the fol (if (cache/has? C :c) ;; has? checks that the cache contains an item (cache/hit C :c) ;; hit returns a cache with any relevant internal information updated (cache/miss C :c 42)) ;; miss returns a new cache with the new item and without evicted entries - + ;=> {:a 1, :b 2, :c 42} ``` -Using the `has?/hit/miss` pattern ensures that the thresholding and eviction logic for each implementation works properly. **Avoid this pattern at your own risk.** +Using the `has?/hit/miss` pattern ensures that the thresholding and eviction logic for each implementation works properly. It is built into the `through` and `through-cache` functions, as well as `clojure.core.cache.wrapped/lookup-or-miss`. **Avoid this pattern at your own risk.** Finally, to explicitly evict an element in a cache, use the `evict` function: ```clojure (cache/evict C :b) - + ;=> {:a 1} ``` For specific information about eviction policies and thresholds, view the specific documentation for each cache type listed in the next section. +Sometimes you may wish to cache a function except when it returns certain values, +such as a resource-loading function that may return `nil` if the resource is +(temporarily) unavailable. Because of the edge cases that can arise with doing +multiple lookups on a cache -- protections from which are baked into `through`, +`through-cache`, and `lookup-or-miss` -- it is better to write your code to +simply cache all results and then, post-retrieval, `evict` the key if the +result is something you don't want cached: + +```clojure +(let [result (wrapped/lookup-or-miss C :c load-my-resource)] + (when (nil? result) ; or seq or whatever condition should exclude this result + (wrapped/evict C :c)) + result) +``` + ## Builtin cache implementations core.cache comes with a number of builtin immutable cache implementations, including (*click through for specific information*): diff --git a/docs/release-notes/release-0.5.0.markdown b/docs/release-notes/release-0.5.0.markdown index 98c604e..b07d756 100644 --- a/docs/release-notes/release-0.5.0.markdown +++ b/docs/release-notes/release-0.5.0.markdown @@ -43,7 +43,7 @@ Places ------ * [Source code](https://github.com/clojure/core.cache) -* [Ticket system](http://dev.clojure.org/jira/browse/CCACHE) +* [Ticket system](http://clojure.atlassian.net/browse/CCACHE) * [Announcement](http://groups.google.com/group/clojure/browse_frm/thread/69d08572ab265dc7) * Examples and documentation -- in progress diff --git a/docs/release-notes/release-0.6.1.markdown b/docs/release-notes/release-0.6.1.markdown index 5fc65ea..14cd378 100644 --- a/docs/release-notes/release-0.6.1.markdown +++ b/docs/release-notes/release-0.6.1.markdown @@ -41,9 +41,9 @@ Places ------ * [Source code](https://github.com/clojure/core.cache) -* [Ticket system](http://dev.clojure.org/jira/browse/CCACHE) +* [Ticket system](http://clojure.atlassian.net/browse/CCACHE) * [Announcement](http://groups.google.com/group/clojure/browse_frm/thread/69d08572ab265dc7) -* [API Reference](https://clojure.github.com/core.cache) +* [API Reference](https://clojure.github.io/core.cache) * [Examples and documentation](https://github.com/clojure/core.cache/wiki) (work in progress) Changes from v0.5.0 diff --git a/docs/release-notes/release-0.6.2.markdown b/docs/release-notes/release-0.6.2.markdown index dc2d1c9..7835912 100644 --- a/docs/release-notes/release-0.6.2.markdown +++ b/docs/release-notes/release-0.6.2.markdown @@ -41,9 +41,9 @@ Places ------ * [Source code](https://github.com/clojure/core.cache) -* [Ticket system](http://dev.clojure.org/jira/browse/CCACHE) +* [Ticket system](http://clojure.atlassian.net/browse/CCACHE) * [Announcement](http://groups.google.com/group/clojure/browse_frm/thread/69d08572ab265dc7) -* [API Reference](https://clojure.github.com/core.cache) +* [API Reference](https://clojure.github.io/core.cache) * [Examples and documentation](https://github.com/clojure/core.cache/wiki) (work in progress) Changes from v0.6.1 @@ -72,4 +72,4 @@ The following capabilities are under design, development, or consideration for f * Deprecation of Clache * More documentation and examples -More planning is needed around capabilities not listed nor thought of. \ No newline at end of file +More planning is needed around capabilities not listed nor thought of. diff --git a/pom.xml b/pom.xml index 1c8f072..97042e4 100644 --- a/pom.xml +++ b/pom.xml @@ -1,16 +1,22 @@ - + 4.0.0 core.cache - 0.6.4-SNAPSHOT - ${artifactId} - A cache library for Clojure - jar + 1.2.999-SNAPSHOT + core.cache + Cache library for Clojure. + + + scm:git:git://github.com/clojure/core.cache.git + scm:git:ssh://git@github.com/clojure/core.cache.git + HEAD + https://github.com/clojure/core.cache + Eclipse Public License 1.0 - http://opensource.org/licenses/eclipse-1.0.php + https://opensource.org/license/epl-1-0/ repo @@ -18,47 +24,26 @@ org.clojure pom.contrib - 0.1.2 + 1.4.0 fogus - Fogus - http://fogus.me + Michael Fogus + https://www.fogus.me - - - org.clojure - data.priority-map - 0.0.2 - - - - - jira - http://dev.clojure.org/jira/browse/CCACHE - - - - scm:git:git://github.com/clojure/core.cache.git - http://github.com/clojure/core.cache - - - - - sonatype-oss-snapshots - https://oss.sonatype.org/content/repositories/snapshots - - - - - - - src/test/clojure - - - + + 1.11.4 + + + + + org.clojure + data.priority-map + 1.2.1 + + diff --git a/project.clj b/project.clj deleted file mode 100644 index ac03f0a..0000000 --- a/project.clj +++ /dev/null @@ -1,15 +0,0 @@ -(defproject core.cache "0.6.4-SNAPSHOT" - :description "Cache library for Clojure." - :dependencies [[org.clojure/clojure "1.6.0"] - [org.clojure/data.priority-map "0.0.2"]] - :profiles {:1.2 {:dependencies [[org.clojure/clojure "1.2.0"]]} - :1.2.1 {:dependencies [[org.clojure/clojure "1.2.1"]]} - :1.3 {:dependencies [[org.clojure/clojure "1.3.0"]]} - :1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]} - :1.5 {:dependencies [[org.clojure/clojure "1.5.0"]]} - :1.5.1 {:dependencies [[org.clojure/clojure "1.5.1"]]}} - :plugins [[lein-swank "1.4.4"] - [lein-marginalia "0.7.1"]] - :repositories {"sonatype-oss-public" "https://oss.sonatype.org/content/groups/public/"} - :source-paths ["src/main/clojure"] - :test-paths ["src/test/clojure"]) diff --git a/run-tests.sh b/run-tests.sh new file mode 100755 index 0000000..8d255fb --- /dev/null +++ b/run-tests.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +versions="1.9 1.10 1.11 1.12" +for v in $versions +do + time clojure -M:test:$v + if test $? != 0; then exit 1; fi +done diff --git a/src/main/clojure/clojure/core/cache.clj b/src/main/clojure/clojure/core/cache.clj index 700ad0f..7800345 100644 --- a/src/main/clojure/clojure/core/cache.clj +++ b/src/main/clojure/clojure/core/cache.clj @@ -50,13 +50,24 @@ ([cache item] (through default-wrapper-fn identity cache item)) ([value-fn cache item] (through default-wrapper-fn value-fn cache item)) ([wrap-fn value-fn cache item] - (if (clojure.core.cache/has? cache item) - (clojure.core.cache/hit cache item) - (clojure.core.cache/miss cache item (wrap-fn #(value-fn %) item))))) + (if (clojure.core.cache/has? cache item) + (clojure.core.cache/hit cache item) + (clojure.core.cache/miss cache item (wrap-fn #(value-fn %) item))))) + +(defn through-cache + "The basic hit/miss logic for the cache system. Like through but always has + the cache argument in the first position for easier use with swap! etc." + ([cache item] (through-cache cache item default-wrapper-fn identity)) + ([cache item value-fn] (through-cache cache item default-wrapper-fn value-fn)) + ([cache item wrap-fn value-fn] + (if (clojure.core.cache/has? cache item) + (clojure.core.cache/hit cache item) + (clojure.core.cache/miss cache item (wrap-fn #(value-fn %) item))))) (defmacro defcache [type-name fields & specifics] - (let [[base-field & _] fields] + (let [[base & _] fields + base-field (with-meta base {:tag 'clojure.lang.IPersistentMap})] `(deftype ~type-name [~@fields] ~@specifics @@ -68,6 +79,10 @@ (lookup this# key#) not-found#)) + java.lang.Iterable + (iterator [_#] + (.iterator ~base-field)) + clojure.lang.IPersistentMap (assoc [this# k# v#] (miss this# k# v#)) @@ -83,15 +98,15 @@ clojure.lang.Counted (count [this#] - (clojure.core/count ~base-field)) + (count ~base-field)) clojure.lang.IPersistentCollection - (cons [_# elem#] - (clojure.core/cons ~base-field elem#)) + (cons [this# elem#] + (seed this# (conj ~base-field elem#))) (empty [this#] (seed this# (empty ~base-field))) - (equiv [_# other#] - (clojure.lang.Util/equiv ~base-field other#)) + (equiv [this# other#] + (= other# ~base-field)) clojure.lang.Seqable (seq [_#] @@ -123,7 +138,7 @@ (f (get cache item))) (lookup [_ item not-found] (let [ret (get cache item not-found)] - (if (= ret not-found) + (if (= not-found ret) not-found (f ret)))) (has? [_ item] @@ -146,16 +161,14 @@ {:dropping dropping :keeping keeping :queue - (concat (repeat (- limit (count keeping)) ::free) - (take limit keeping))})) - -(defn- dissoc-keys [m ks] - (if ks - (recur (dissoc m (first ks)) (next ks)) - m)) + (-> clojure.lang.PersistentQueue/EMPTY + (into (repeat (- limit (count keeping)) ::free)) + (into (take limit keeping)))})) (defn- prune-queue [q k] - (remove #{k} q)) + (reduce (fn [q e] (if (#{k} e) q (conj q e))) + (conj clojure.lang.PersistentQueue/EMPTY ::free) + q)) (defcache FIFOCache [cache q limit] CacheProtocol @@ -168,20 +181,19 @@ (hit [this item] this) (miss [_ item result] - (let [[kache qq] (let [k (first q)] + (let [[kache qq] (let [k (peek q)] (if (>= (count cache) limit) - [(dissoc cache k) (rest q)] - [cache (rest q)]))] + [(dissoc cache k) (pop q)] + [cache (pop q)]))] (FIFOCache. (assoc kache item result) - (concat qq [item]) + (conj qq item) limit))) (evict [this key] - (let [v (get cache key ::miss)] - (if (= v ::miss) - this - (FIFOCache. (dissoc cache key) - (prune-queue q key) - limit)))) + (if (contains? cache key) + (FIFOCache. (dissoc cache key) + (prune-queue q key) + limit) + this)) (seed [_ base] (let [{dropping :dropping q :queue} (describe-layout base limit)] @@ -193,11 +205,8 @@ (str cache \, \space (pr-str q)))) (defn- build-leastness-queue - [base limit start-at] - (into (clojure.data.priority-map/priority-map) - (concat (take (- limit (count base)) (for [k (range (- limit) 0)] [k k])) - (for [[k _] base] [k start-at])))) - + [base start-at] + (into (clojure.data.priority-map/priority-map) (for [[k _] base] [k start-at]))) (defcache LRUCache [cache lru tick limit] CacheProtocol @@ -210,7 +219,9 @@ (hit [_ item] (let [tick+ (inc tick)] (LRUCache. cache - (assoc lru item tick+) + (if (contains? cache item) + (assoc lru item tick+) + lru) tick+ limit))) (miss [_ item result] @@ -227,16 +238,15 @@ tick+ limit)))) (evict [this key] - (let [v (get cache key ::miss)] - (if (= v ::miss) - this - (LRUCache. (dissoc cache key) - (dissoc lru key) - (inc tick) - limit)))) + (if (contains? cache key) + (LRUCache. (dissoc cache key) + (dissoc lru key) + (inc tick) + limit) + this)) (seed [_ base] (LRUCache. base - (build-leastness-queue base limit 0) + (build-leastness-queue base 0) 0 limit)) Object @@ -244,42 +254,57 @@ (str cache \, \space lru \, \space tick \, \space limit))) -(defn- key-killer - [ttl expiry now] - (let [ks (map key (filter #(> (- now (val %)) expiry) ttl))] - #(apply dissoc % ks))) +(defn- key-killer-q + [ttl q expiry now] + (let [[ks q'] (reduce (fn [[ks q] [k g t]] + (if (> (- now t) expiry) + (if (= g (first (get ttl k))) + [(conj ks k) (pop q)] + [ks (pop q)]) + (reduced [ks q]))) + [[] q] + q)] + [#(apply dissoc % ks) q'])) - -(defcache TTLCache [cache ttl ttl-ms] +(defcache TTLCacheQ [cache ttl q gen ttl-ms] CacheProtocol (lookup [this item] (let [ret (lookup this item ::nope)] - (when-not (= ret ::nope) ret))) + (when-not (= ::nope ret) ret))) (lookup [this item not-found] (if (has? this item) (get cache item) not-found)) (has? [_ item] - (let [t (get ttl item (- ttl-ms))] - (< (- (System/currentTimeMillis) - t) - ttl-ms))) + (and (let [[_ t] (get ttl item [0 (- ttl-ms)])] + (< (- (System/currentTimeMillis) + t) + ttl-ms)) + (contains? cache item))) (hit [this item] this) (miss [this item result] (let [now (System/currentTimeMillis) - kill-old (key-killer ttl ttl-ms now)] - (TTLCache. (assoc (kill-old cache) item result) - (assoc (kill-old ttl) item now) - ttl-ms))) + [kill-old q'] (key-killer-q ttl q ttl-ms now)] + (TTLCacheQ. (assoc (kill-old cache) item result) + (assoc (kill-old ttl) item [gen now]) + (conj q' [item gen now]) + (unchecked-inc gen) + ttl-ms))) (seed [_ base] (let [now (System/currentTimeMillis)] - (TTLCache. base - (into {} (for [x base] [(key x) now])) - ttl-ms))) + (TTLCacheQ. base + ;; we seed the cache all at gen, but subsequent entries + ;; will get gen+1, gen+2 etc + (into {} (for [x base] [(key x) [gen now]])) + (into q (for [x base] [(key x) gen now])) + (unchecked-inc gen) + ttl-ms))) (evict [_ key] - (TTLCache. (dissoc cache key) - (dissoc ttl key) - ttl-ms)) + (TTLCacheQ. (dissoc cache key) + (dissoc ttl key) + q + gen + ttl-ms)) Object (toString [_] (str cache \, \space ttl \, \space ttl-ms))) @@ -298,24 +323,23 @@ (miss [_ item result] (if (>= (count lu) limit) ;; need to evict? (let [min-key (if (contains? lu item) - ::nope - (first (peek lu))) ;; maybe evict case + ::nope + (first (peek lu))) ;; maybe evict case c (-> cache (dissoc min-key) (assoc item result)) l (-> lu (dissoc min-key) (update-in [item] (fnil inc 0)))] (LUCache. c l limit)) (LUCache. (assoc cache item result) ;; no change case - (assoc lu item 0) + (update-in lu [item] (fnil inc 0)) limit))) (evict [this key] - (let [v (get cache key ::miss)] - (if (= v ::miss) - this - (LUCache. (dissoc cache key) - (dissoc lu key) - limit)))) + (if (contains? this key) + (LUCache. (dissoc cache key) + (dissoc lu key) + limit) + this)) (seed [_ base] (LUCache. base - (build-leastness-queue base limit 0) + (build-leastness-queue base 0) limit)) Object (toString [_] @@ -482,11 +506,12 @@ (defn clear-soft-cache! [^java.util.Map cache ^java.util.Map rcache ^ReferenceQueue rq] (loop [r (.poll rq)] (when r - (.remove cache (get rcache r)) + (when-let [item (get rcache r)] + (.remove cache item)) (.remove rcache r) (recur (.poll rq))))) -(defn ^{:dynamic true} make-reference [v rq] +(defn make-reference [v rq] (if (nil? v) (SoftReference. ::nil rq) (SoftReference. v rq))) @@ -495,9 +520,10 @@ CacheProtocol (lookup [_ item] (when-let [^SoftReference r (get cache (or item ::nil))] - (if (= ::nil (.get r)) - nil - (.get r)))) + (let [v (.get r)] + (if (= ::nil v) + nil + v)))) (lookup [_ item not-found] (if-let [^SoftReference r (get cache (or item ::nil))] (if-let [v (.get r)] @@ -509,8 +535,9 @@ (has? [_ item] (let [item (or item ::nil) ^SoftReference cell (get cache item)] - (and (contains? cache item) - (not (nil? (.get cell)))))) + (boolean + (when cell + (not (nil? (.get cell))))))) (hit [this item] (clear-soft-cache! cache rcache rq) this) @@ -549,7 +576,7 @@ ;; Factories (defn basic-cache-factory - "Returns a pluggable basic cache initialied to `base`" + "Returns a pluggable basic cache initialized to `base`" [base] {:pre [(map? base)]} (BasicCache. base)) @@ -576,7 +603,7 @@ (clojure.core.cache/seed (FIFOCache. {} clojure.lang.PersistentQueue/EMPTY threshold) base)) (defn lru-cache-factory - "Returns an LRU cache with the cache and usage-table initialied to `base` -- + "Returns an LRU cache with the cache and usage-table initialized to `base` -- each entry is initialized with the same usage value. This function takes an optional `:threshold` argument that defines the maximum number @@ -587,7 +614,7 @@ (clojure.core.cache/seed (LRUCache. {} (clojure.data.priority-map/priority-map) 0 threshold) base)) (defn ttl-cache-factory - "Returns a TTL cache with the cache and expiration-table initialied to `base` -- + "Returns a TTL cache with the cache and expiration-table initialized to `base` -- each with the same time-to-live. This function also allows an optional `:ttl` argument that defines the default @@ -595,10 +622,10 @@ [base & {ttl :ttl :or {ttl 2000}}] {:pre [(number? ttl) (<= 0 ttl) (map? base)]} - (clojure.core.cache/seed (TTLCache. {} {} ttl) base)) + (clojure.core.cache/seed (TTLCacheQ. {} {} clojure.lang.PersistentQueue/EMPTY 0 ttl) base)) (defn lu-cache-factory - "Returns an LU cache with the cache and usage-table initialied to `base`. + "Returns an LU cache with the cache and usage-table initialized to `base`. This function takes an optional `:threshold` argument that defines the maximum number of elements in the cache before the LU semantics apply (default is 32)." @@ -616,7 +643,7 @@ {:pre [(number? s-history-limit) (< 0 s-history-limit) (number? q-history-limit) (< 0 q-history-limit) (map? base)]} - (seed (LIRSCache. {} {} {} 0 s-history-limit q-history-limit) base)) + (clojure.core.cache/seed (LIRSCache. {} {} {} 0 s-history-limit q-history-limit) base)) (defn soft-cache-factory "Returns a SoftReference cache. Cached values will be referred to with @@ -627,21 +654,5 @@ ConcurrentHashMap." [base] {:pre [(map? base)]} - (seed (SoftCache. (ConcurrentHashMap.) (ConcurrentHashMap.) (ReferenceQueue.)) + (clojure.core.cache/seed (SoftCache. (ConcurrentHashMap.) (ConcurrentHashMap.) (ReferenceQueue.)) base)) - -(comment - - (def C (-> {:a 1 :b 2} - (fifo-cache-factory :threshold 2) - (ttl-cache-factory :ttl 5000))) - - (assoc C :c 42) - ;;=> {:b 2, :c 42} - - ;; wait 5 seconds - - (assoc C :d 138) - ;;=> {:d 138} - -) diff --git a/src/main/clojure/clojure/core/cache/wrapped.clj b/src/main/clojure/clojure/core/cache/wrapped.clj new file mode 100644 index 0000000..e171cbc --- /dev/null +++ b/src/main/clojure/clojure/core/cache/wrapped.clj @@ -0,0 +1,228 @@ +; Copyright (c) Rich Hickey. All rights reserved. +; The use and distribution terms for this software are covered by the +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +; which can be found in the file epl-v10.html at the root of this distribution. +; By using this software in any fashion, you are agreeing to be bound by +; the terms of this license. +; You must not remove this notice, or any other, from this software. + +(ns clojure.core.cache.wrapped + "A higher level way to use clojure.core.cache that assumes the immutable + cache is wrapped in an atom. + + The API is (almost) the same as clojure.core.cache -- including the factory + functions -- but instead of accepting immutable caches, the functions + here accept atoms containing those caches. The factory functions return + new atoms containing the newly created cache. + + In addition, lookup-or-miss provides a safe, atomic way to retrieve a + value from a cache or compute it if it is missing, without risking a + cache stampede." + (:require [clojure.core.cache :as c])) + +(set! *warn-on-reflection* true) + +(def ^{:private true} default-wrapper-fn #(%1 %2)) + +(defn lookup + "Retrieve the value associated with `e` if it exists, else `nil` in + the 2-arg case. Retrieve the value associated with `e` if it exists, + else `not-found` in the 3-arg case. + + Reads from the current version of the atom." + ([cache-atom e] + (force (c/lookup @cache-atom e))) + ([cache-atom e not-found] + (force (c/lookup @cache-atom e not-found)))) + +(defn lookup-or-miss + "Retrieve the value associated with `e` if it exists, else compute the + value (using value-fn, and optionally wrap-fn), update the cache for `e` + and then perform the lookup again. + + value-fn (and wrap-fn) will only be called (at most) once even in the + case of retries, so there is no risk of cache stampede. + + Since lookup can cause invalidation in some caches (such as TTL), we + trap that case and retry (a maximum of ten times)." + ([cache-atom e value-fn] + (lookup-or-miss cache-atom e default-wrapper-fn value-fn)) + ([cache-atom e wrap-fn value-fn] + (let [my-delay (delay (wrap-fn value-fn e)) + attempt + (fn [] + (let [v (c/lookup (swap! cache-atom + c/through-cache + e + default-wrapper-fn + (fn [_] my-delay)) + e + ::expired)] + (cond + ;; TTL expiration race — retry + (= ::expired v) ::expired + ;; not a delay — return directly + (not (delay? v)) v + :else + (try + (force v) + (catch Throwable t + ;; only evict if the failed delay is still in the cache + ;; (another thread may have already replaced it) + (swap! cache-atom + (fn [cache] + (if (identical? v (c/lookup cache e)) + (c/evict cache e) + cache))) + ;; our own delay failed — rethrow + (when (realized? my-delay) + (throw t)) + ;; another thread's delay failed — retry + ::retry)))))] + (loop [n 0 v (attempt)] + (when (< n 10) + (case v + (::expired ::retry) (recur (inc n) (attempt)) + v)))))) + +(defn has? + "Checks if the cache contains a value associated with `e`. + + Reads from the current version of the atom." + [cache-atom e] + (c/has? @cache-atom e)) + +(defn size + "Obtain the approximate size/count of the wrapped cache. + + The value returned is a point-in-time approximate count/size as the nature + of certain caches don't reflect their true size until a 'lookup-or-miss' + operation is invoked - such as the TTL cache. + + Whilst it is an approximate value, the returned result can be useful for + metric tracking or decision making. + + Returns an approximate count/size of the wrapped cache. + " + [cache-atom] + (count @cache-atom)) + +(defn hit + "Is meant to be called if the cache is determined to contain a value + associated with `e`. + + Returns the updated cache from the atom. Provided for completeness." + [cache-atom e] + (swap! cache-atom c/hit e)) + +(defn miss + "Is meant to be called if the cache is determined to **not** contain a + value associated with `e`. + + Returns the updated cache from the atom. Provided for completeness." + [cache-atom e ret] + (swap! cache-atom c/miss e ret)) + +(defn evict + "Removes an entry from the cache. + + Returns the updated cache from the atom." + [cache-atom e] + (swap! cache-atom c/evict e)) + +(defn seed + "Is used to signal that the cache should be created with a seed. + The contract is that said cache should return an instance of its + own type. + + Returns the updated cache from the atom. Provided for completeness." + [cache-atom base] + (swap! cache-atom c/seed base)) + +(defn through + "The basic hit/miss logic for the cache system. Expects a wrap function and + value function. The wrap function takes the value function and the item in question + and is expected to run the value function with the item whenever a cache + miss occurs. The intent is to hide any cache-specific cells from leaking + into the cache logic itelf." + ([cache-atom item] (through default-wrapper-fn identity cache-atom item)) + ([value-fn cache-atom item] (through default-wrapper-fn value-fn cache-atom item)) + ([wrap-fn value-fn cache-atom item] + (swap! cache-atom c/through-cache item wrap-fn value-fn))) + +(defn through-cache + "The basic hit/miss logic for the cache system. Like through but always has + the cache argument in the first position." + ([cache-atom item] (through-cache cache-atom item default-wrapper-fn identity)) + ([cache-atom item value-fn] (through-cache cache-atom item default-wrapper-fn value-fn)) + ([cache-atom item wrap-fn value-fn] + (swap! cache-atom c/through-cache item wrap-fn value-fn))) + +(defn basic-cache-factory + "Returns a pluggable basic cache initialized to `base`" + [base] + (atom (c/basic-cache-factory base))) + +(defn fifo-cache-factory + "Returns a FIFO cache with the cache and FIFO queue initialized to `base` -- + the queue is filled as the values are pulled out of `base`. If the associative + structure can guarantee ordering, then the said ordering will define the + eventual eviction order. Otherwise, there are no guarantees for the eventual + eviction ordering. + + This function takes an optional `:threshold` argument that defines the maximum number + of elements in the cache before the FIFO semantics apply (default is 32). + + If the number of elements in `base` is greater than the limit then some items + in `base` will be dropped from the resulting cache. If the associative + structure used as `base` can guarantee sorting, then the last `limit` elements + will be used as the cache seed values. Otherwise, there are no guarantees about + the elements in the resulting cache." + [base & {threshold :threshold :or {threshold 32}}] + (atom (c/fifo-cache-factory base :threshold threshold))) + +(defn lru-cache-factory + "Returns an LRU cache with the cache and usage-table initialized to `base` -- + each entry is initialized with the same usage value. + + This function takes an optional `:threshold` argument that defines the maximum number + of elements in the cache before the LRU semantics apply (default is 32)." + [base & {threshold :threshold :or {threshold 32}}] + (atom (c/lru-cache-factory base :threshold threshold))) + +(defn ttl-cache-factory + "Returns a TTL cache with the cache and expiration-table initialized to `base` -- + each with the same time-to-live. + + This function also allows an optional `:ttl` argument that defines the default + time in milliseconds that entries are allowed to reside in the cache." + [base & {ttl :ttl :or {ttl 2000}}] + (atom (c/ttl-cache-factory base :ttl ttl))) + +(defn lu-cache-factory + "Returns an LU cache with the cache and usage-table initialized to `base`. + + This function takes an optional `:threshold` argument that defines the maximum number + of elements in the cache before the LU semantics apply (default is 32)." + [base & {threshold :threshold :or {threshold 32}}] + (atom (c/lu-cache-factory base :threshold threshold))) + +(defn lirs-cache-factory + "Returns an LIRS cache with the S & R LRU lists set to the indicated + limits." + [base & {:keys [s-history-limit q-history-limit] + :or {s-history-limit 32 + q-history-limit 32}}] + (atom (c/lirs-cache-factory base + :s-history-limit s-history-limit + :q-history-limit q-history-limit))) + +(defn soft-cache-factory + "Returns a SoftReference cache. Cached values will be referred to with + SoftReferences, allowing the values to be garbage collected when there is + memory pressure on the JVM. + + SoftCache is a mutable cache, since it is always based on a + ConcurrentHashMap." + [base] + (atom (c/soft-cache-factory base))) diff --git a/src/test/clojure/clojure/core/cache/wrapped_test.clj b/src/test/clojure/clojure/core/cache/wrapped_test.clj new file mode 100644 index 0000000..b66677c --- /dev/null +++ b/src/test/clojure/clojure/core/cache/wrapped_test.clj @@ -0,0 +1,112 @@ +; Copyright (c) Rich Hickey. All rights reserved. +; The use and distribution terms for this software are covered by the +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +; which can be found in the file epl-v10.html at the root of this distribution. +; By using this software in any fashion, you are agreeing to be bound by +; the terms of this license. +; You must not remove this notice, or any other, from this software. + +(ns clojure.core.cache.wrapped-test + (:require [clojure.core.cache.wrapped :as c] + [clojure.core.cache :as cache] + [clojure.test :refer [deftest is]])) + +(deftest basic-wrapped-test + (let [cache (c/basic-cache-factory {})] + (is (= nil (c/lookup cache :a))) + ;; the size of the cache is zero + (is (= 0 (c/size cache))) + (is (= ::missing (c/lookup cache :a ::missing))) + ;; mutating operation + (is (= 42 (c/lookup-or-miss cache :a (constantly 42)))) + ;; cache now contains :a + (is (= 42 (c/lookup cache :a))) + ;; and it now has a size of one + (is (= 1 (c/size cache))) + ;; :a is present, does not call value-fn + (is (= 42 (c/lookup-or-miss cache :a #(throw (ex-info "bad" {:key %}))))) + ;; cache still contains :a as 42 + (is (= 42 (c/lookup cache :a))) + ;; and it still has a size of one + (is (= 1 (c/size cache))) + (c/evict cache :a) + (is (= nil (c/lookup cache :a))) + ;; and the cache now has a size of zero + (is (= 0 (c/size cache))) + (is (= ::missing (c/lookup cache :a ::missing))))) + +(deftest wrapped-ttl-test + ;; TTL lookup-or-miss can expire on the lookup so this test verifies + ;; that bug (in 0.8.0) so I can fix it in 0.8.1! + (let [cache (c/ttl-cache-factory {} :ttl 1) + limit 2000000 + start (System/currentTimeMillis)] + (loop [n 0] + (if-not (c/lookup-or-miss cache :a (constantly 42)) + (is false (str "Failure on call " n)) + (when (< n limit) + (recur (inc n))))) + (println "ttl test completed" limit "calls in" + (- (System/currentTimeMillis) start) "ms"))) + +(deftest cache-stampede + (let [thread-count 1000 + cache-atom (-> {} + (cache/ttl-cache-factory :ttl 120000) + (cache/lu-cache-factory :threshold 100) + (atom)) + latch (java.util.concurrent.CountDownLatch. thread-count) + invocations-counter (atom 0) + freds (atom []) + values (atom []) + exes (atom 0)] + (dotimes [n thread-count] + (if (<= n (rand-int thread-count)) + (swap! freds conj + (Thread. + (fn [] + (try + (swap! values conj + (c/lookup-or-miss cache-atom "my-key" + (fn [_] + (throw (Exception. "Bad"))))) + (catch Exception e + (swap! exes inc) + (is (= "Bad" (.getMessage e))))) + (.countDown latch)))) + (swap! freds conj + (Thread. + (fn [] + (try + (swap! values conj + (c/lookup-or-miss cache-atom "my-key" + (fn [_] + (swap! invocations-counter inc) + (Thread/sleep 3000) + "some value"))) + (catch Exception _ + (is false "Unexpected cached exception"))) + (.countDown latch)))))) + + (run! #(.start ^Thread %) @freds) + + (.await latch) + (println "cache-stampede test completed with" @invocations-counter + "successful invocation(s)\n\tand" (count @values) + "successful lookups in" thread-count + "threads with" @exes "exceptions") + (is (= 1 (deref invocations-counter))) + (is (= (- thread-count @exes) (count @values))) + (doseq [v @values] + (is (= "some value" v))))) + +(deftest cache-thrown + (let [c (c/basic-cache-factory {})] + (is (thrown? Exception + (c/lookup-or-miss c :a + (fn [_] + (throw (Exception. "bad")))))) + ;; cache should not have :a in it + (is (= nil (c/lookup c :a))) + ;; and the cache should still be empty + (is (= 0 (c/size c))))) diff --git a/src/test/clojure/clojure/core/cache/tests.clj b/src/test/clojure/clojure/core/cache_test.clj similarity index 77% rename from src/test/clojure/clojure/core/cache/tests.clj rename to src/test/clojure/clojure/core/cache_test.clj index 52812f1..a67f614 100644 --- a/src/test/clojure/clojure/core/cache/tests.clj +++ b/src/test/clojure/clojure/core/cache_test.clj @@ -8,14 +8,16 @@ (ns ^{:doc "A caching library for Clojure." :author "Fogus"} - clojure.core.cache.tests + clojure.core.cache-test (:use [clojure.core.cache] :reload-all) (:use [clojure.test]) - (:import (clojure.core.cache BasicCache FIFOCache LRUCache TTLCache LUCache + (:import (clojure.core.cache BasicCache FIFOCache LRUCache TTLCacheQ LUCache LIRSCache) (java.lang.ref ReferenceQueue SoftReference) (java.util.concurrent ConcurrentHashMap))) +(println "\nTesting with Clojure" (clojure-version)) + (deftest test-basic-cache-lookup (testing "that the BasicCache can lookup as expected" (is (= :robot (lookup (miss (BasicCache. {}) '(servo) :robot) '(servo)))))) @@ -53,7 +55,7 @@ (get c :e 0) 0 (get c :b 0) 2 (get c :f 0) nil - + (get-in c [:c :e]) 4 (get-in c '(:c :e)) 4 (get-in c [:c :x]) nil @@ -62,7 +64,7 @@ (get-in c [:h]) nil (get-in c []) c (get-in c nil) c - + (get-in c [:c :e] 0) 4 (get-in c '(:c :e) 0) 4 (get-in c [:c :x] 0) 0 @@ -181,37 +183,76 @@ (assoc :a 1) (assoc :b 2) (assoc :b 3) - .cache))))) + .cache)))) + + (is (= {:d 4 :e 5} + (-> (lru-cache-factory {} :threshold 2) + (hit :x) + (hit :y) + (hit :z) + (assoc :a 1) + (assoc :b 2) + (assoc :c 3) + (assoc :d 4) + (assoc :e 5) + .cache)))) (defn sleepy [e t] (Thread/sleep t) e) (deftest test-ttl-cache-ilookup - (let [five-secs (+ 5000 (System/currentTimeMillis)) - big-time (into {} (for [[k _] big-map] [k five-secs])) - small-time (into {} (for [[k _] small-map] [k five-secs]))] - (testing "that the TTLCache can lookup via keywords" - (do-ilookup-tests (TTLCache. small-map small-time 2000))) - (testing "that the TTLCache can lookup via keywords" - (do-dot-lookup-tests (TTLCache. small-map small-time 2000))) - (testing "assoc and dissoc for TTLCache" - (do-assoc (TTLCache. {} {} 2000)) - (do-dissoc (TTLCache. {:a 1 :b 2} {:a five-secs :b five-secs} 2000))) - (testing "that get and cascading gets work for TTLCache" - (do-getting (TTLCache. big-map big-time 2000))) - (testing "that finding works for TTLCache" - (do-finding (TTLCache. small-map small-time 2000))) - (testing "that contains? works for TTLCache" - (do-contains (TTLCache. small-map small-time 2000))))) + (let [five-secs (+ 5000 (System/currentTimeMillis)) + big-time (into {} (for [[k _] big-map] [k [0 five-secs]])) + big-q (into clojure.lang.PersistentQueue/EMPTY + (for [[k _] big-map] [k 0 five-secs])) + small-time (into {} (for [[k _] small-map] [k [0 five-secs]])) + small-q (into clojure.lang.PersistentQueue/EMPTY + (for [[k _] small-map] [k 0 five-secs]))] + (testing "that the TTLCacheQ can lookup via keywords" + (do-ilookup-tests (TTLCacheQ. small-map small-time small-q 1 2000))) + (testing "that the TTLCacheQ can lookup via keywords" + (do-dot-lookup-tests (TTLCacheQ. small-map small-time small-q 1 2000))) + (testing "assoc and dissocQ for TTLCacheQ" + (do-assoc (TTLCacheQ. {} {} clojure.lang.PersistentQueue/EMPTY 1 2000)) + (do-dissoc (TTLCacheQ. {:a 1 :b 2} + {:a [0 five-secs] :b [0 five-secs]} + (into clojure.lang.PersistentQueue/EMPTY + [[:a 0 five-secs] [:b 0 five-secs]]) + 1 + 2000))) + (testing "that get and cascading gets work for TTLCacheQ" + (do-getting (TTLCacheQ. big-map big-time big-q 1 2000))) + (testing "that finding works for TTLCacheQ" + (do-finding (TTLCacheQ. small-map small-time small-q 1 2000))) + (testing "that contains? works for TTLCacheQ" + (do-contains (TTLCacheQ. small-map small-time small-q 1 2000))))) + +(defn- ttl-q-check + [start nap [k g t]] + [k g (<= start (+ start nap) t (+ start nap 100))]) (deftest test-ttl-cache (testing "TTL-ness with empty cache" - (let [C (ttl-cache-factory {} :ttl 500)] + (let [start (System/currentTimeMillis) + C (ttl-cache-factory {} :ttl 500) + C' (-> C (assoc :a 1) (assoc :b 2))] (are [x y] (= x y) - {:a 1, :b 2} (-> C (assoc :a 1) (assoc :b 2) .cache) - {:c 3} (-> C (assoc :a 1) (assoc :b 2) (sleepy 700) (assoc :c 3) .cache)))) + [[:a 1 true], [:b 2 true]] (map (partial ttl-q-check start 0) (.q C')) + {:a 1, :b 2} (.cache C') + 3 (.gen C'))) + (let [start (System/currentTimeMillis) + C (ttl-cache-factory {} :ttl 500) + C' (-> C (assoc :a 1) (assoc :b 2) (sleepy 700) (assoc :c 3))] + (are [x y] (= x y) + [[:c 3 true]] (map (partial ttl-q-check start 700) (.q C')) + {:c 3} (.cache C') + 4 (.gen C')))) (testing "TTL cache does not return a value that has expired." (let [C (ttl-cache-factory {} :ttl 500)] - (is (nil? (-> C (assoc :a 1) (sleepy 700) (lookup :a))))))) + (is (nil? (-> C (assoc :a 1) (sleepy 700) (lookup :a)))))) + (testing "TTL cache does not contain a value that was removed from underlying cache." + (let [underlying-cache (lru-cache-factory {} :threshold 1) + C (ttl-cache-factory underlying-cache :ttl 360000)] + (is (not (-> C (assoc :a 1) (assoc :b 2) (has? :a))))))) (deftest test-lu-cache-ilookup (testing "that the LUCache can lookup via keywords" @@ -235,7 +276,7 @@ {:a 1, :b 2, :c 3, :d 4} (-> C (assoc :c 3) (assoc :d 4) .cache) {:a 1, :c 3, :d 4, :e 5} (-> C (assoc :c 3) (assoc :d 4) (.hit :a) (assoc :e 5) .cache) {:b 2, :c 3, :d 4, :e 5} (-> C (assoc :c 3) (assoc :d 4) (.hit :b) (.hit :c) (.hit :d) (assoc :e 5) .cache)))) - (testing "regressions against LRU eviction before threshold met" + (testing "regressions against LU eviction before threshold met" (is (= (-> (clojure.core.cache/lu-cache-factory {} :threshold 2) (assoc :a 1) (assoc :b 2) @@ -260,6 +301,13 @@ (assoc :a 1) (assoc :b 2) (assoc :b 3) + .cache))) + + (is (= {:c 3 :d 4} + (-> (clojure.core.cache/lu-cache-factory {:a 1 :b 2} :threshold 2) + (dissoc :a) + (assoc :c 3) + (assoc :d 4) .cache))))) ;; # LIRS @@ -413,22 +461,22 @@ N non-resident HIR block (.put :foo ref)) rcache (doto (ConcurrentHashMap.) (.put ref :foo)) - soft-cache (clear-soft-cache! cache rcache rq)] + _ (clear-soft-cache! cache rcache rq)] (is (contains? cache :foo) (str cache)) (is (contains? rcache ref) (str rcache)) (.clear ref) (.enqueue ref) (is (not (.get ref))) - (let [soft-cache (clear-soft-cache! cache rcache rq)] + (let [_ (clear-soft-cache! cache rcache rq)] (is (not (contains? cache :foo))) (is (not (contains? rcache ref)))))) (deftest test-soft-cache (let [ref (atom nil) old-make-reference make-reference] - (binding [make-reference (fn [& args] - (reset! ref (apply old-make-reference args)) - @ref)] + (with-redefs [make-reference (fn [& args] + (reset! ref (apply old-make-reference args)) + @ref)] (let [old-soft-cache (soft-cache-factory {:foo1 :bar}) r @ref soft-cache (assoc old-soft-cache :foo2 :baz)] @@ -443,3 +491,41 @@ N non-resident HIR block (is (= :quux (.lookup old-soft-cache :foo3 :quux))) (is (not (.has? soft-cache :foo1))) (is (not (.has? old-soft-cache :foo1))))))) + +(deftest test-soft-cache-eviction-handling + (let [ref (atom nil) + old-make-reference make-reference] + (with-redefs [make-reference (fn [& args] + (reset! ref (apply old-make-reference args)) + @ref)] + (let [cache (soft-cache-factory {})] + (miss cache :foo "foo") + (.enqueue @ref) + (evict cache :foo))))) + +(deftest test-equiv + (is (= (fifo-cache-factory {:a 1 :c 3} :threshold 3) + (fifo-cache-factory {:a 1 :c 3} :threshold 3)))) + +(deftest test-persistent-cons + (is (let [starts-with-a (fifo-cache-factory {:a 1} :threshold 3)] + (= (fifo-cache-factory {:a 1 :c 3} :threshold 3) + (conj starts-with-a [:c 3]) + (conj starts-with-a {:c 3}))))) + +(deftest evict-with-object-exception + (let [thing (proxy [Object] [] + (equals [obj] (throw (new Exception "Boom!"))))] + (are [x y] (= x y) + {:b 2} (-> (lru-cache-factory {:a thing, :b 2}) (evict :a) (.cache)) + {:b 2} (-> (lu-cache-factory {:a thing, :b 2}) (evict :a) (.cache)) + {:b 2} (-> (fifo-cache-factory {:a thing, :b 2}) (evict :a) (.cache))))) + +(deftest test-cache-iterable + (let [c (fifo-cache-factory {:a 1 :b 2} :threshold 10)] + (is (= #{:a :b} (set (iterator-seq (.iterator (keys c)))))))) + +(deftest test-fifo-miss-does-not-drop-ccache-39 + (let [c (fifo-cache-factory {:a 1 :b 2} :threshold 2)] + (is (= #{:a :c} (set (-> c (evict :b) (miss :c 42) (.q))))) + (is (= #{:c :d} (set (-> c (evict :b) (miss :c 42) (miss :d 43) (.q)))))))