• Stars
    star
    442
  • Rank 98,624 (Top 2 %)
  • Language
    Clojure
  • License
    Eclipse Public Li...
  • Created almost 13 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A caching library for Clojure implementing various cache strategies

clojure.core.cache

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

    • First-in-first-out (FIFOCache)
    • Least-recently-used (LRUCache)
    • Least-used (LUCache -- sometimes called Least Frequently Used)
    • Time-to-live (TTLCacheQ)
    • Naive cache (BasicCache)
    • Naive cache backed with soft references (SoftCache)
  • Implementation of an efficient buffer replacement policy based on the low inter-reference recency set algorithm (LIRSCache) described in the LIRS paper

  • Factory functions for each existing cache type

  • Caches are generally immutable and should be used in conjunction with Clojure's state management, such as atom. SoftCache is the exception here, 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 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 by Dan Sutton for why it is important to use the API correctly!

Releases and Dependency Information

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.

Latest stable release: 1.0.225

CLI/deps.edn dependency information:

org.clojure/core.cache {:mvn/version "1.0.225"}

Leiningen dependency information:

[org.clojure/core.cache "1.0.225"]

Maven dependency information:

<dependency>
  <groupId>org.clojure</groupId>
  <artifactId>core.cache</artifactId>
  <version>1.0.225</version>
</dependency>

Example Usage

The clojure.core.cache namespace 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 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.

    (require '[clojure.core.cache :as cache])

    ;; 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}

    (cache/lookup C1' :c)

    ;=> 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)

    ;=> {:a 1, :d 13}

    (get @C2 :a)

    ;=> 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 for additional documentation.

Developer Information

Change Log

  • 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 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
  • Release 0.8.1 on 2019-08-24
    • 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 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 Remove unnecessary/additional .get call (Neil Prosser)
    • CCACHE-52 Fix NPE in SoftCache (Neil Prosser)
  • Release 0.7.1 on 2018.03.02
    • CCACHE-49 Fix TTLCacheQ seed function and expand tests on TTLCacheQ
  • Release 0.7.0 on 2018.03.01
    • CCACHE-46 Fix TTLCache when wrapped around another cache (Ivan Kryvoruchko)
    • CCACHE-43 Add through-cache to provide a version of through that plays nice with swap! etc
    • CCACHE-40 Fix FIFOCache stack overflow on large threshold (uses PersistentQueue now instead of concat and list)
    • CCACHE-39 Fix FIFOCache evict/miss queue handling
    • CCACHE-20 Updated README to clarify that caches are immutable and provide examples of use with atom etc
    • 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 Implement Iterable in defcache
    • CCACHE-44 Avoid equals comparison on cache miss
    • 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 bump tools.priority-map dependency to 0.0.4
    • CCACHE-28 concurrency bug in has? for SoftCache
    • CCACHE-29 fix conj implementation for caches
    • CCACHE-30 make-reference need not be dynamic
    • 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
    • Removed reflection warnings
    • Fixed eviction of items from LU, TTL and LRU caches with thresholds less than two
    • Fixed eviction of items from FIFO cache prior to threshold
  • Release 0.6.2 on 2012.07.13 more information
    • Added SoftCache
    • Fixed eviction of items from LU and LRU caches prior to threshold
    • Adjusted default thresholds in factory functions
  • Release 0.5.0 on 2011.12.13 more information
    • Added evict
    • Added cache factory functions
    • Added associatve operation support

Copyright and License

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.

More Repositories

1

clojure

The Clojure programming language
Java
10,334
star
2

clojurescript

Clojure to JS compiler
Clojure
9,191
star
3

core.async

Facilities for async programming and communication in Clojure
Clojure
1,935
star
4

clojure-clr

A port of Clojure to the CLR, part of the Clojure project
C#
1,541
star
5

core.logic

A logic programming library for Clojure & ClojureScript
Clojure
1,434
star
6

core.typed

An optional type system for Clojure
Clojure
1,285
star
7

core.match

An optimized pattern matching library for Clojure
Clojure
1,180
star
8

test.check

QuickCheck for Clojure
Clojure
1,112
star
9

java.jdbc

JDBC from Clojure (formerly clojure.contrib.sql)
Clojure
714
star
10

tools.cli

Command-line processing
Clojure
711
star
11

tools.nrepl

A Clojure network REPL that provides a server and client, along with some common APIs of use to IDEs and other tools that may need to evaluate Clojure code in remote environments.
Clojure
661
star
12

tools.namespace

Tools for managing namespaces in Clojure
Clojure
596
star
13

data.json

JSON in Clojure
Clojure
536
star
14

algo.monads

Macros for defining monads, and definition of the most common monads
Clojure
444
star
15

tools.deps.alpha

A functional API for transitive dependency graph expansion and the creation of classpaths
Clojure
435
star
16

tools.logging

Clojure logging API
Clojure
382
star
17

tools.trace

1.3 update of clojure.contrib.trace
Clojure
354
star
18

math.combinatorics

Efficient, functional algorithms for generating lazy sequences for common combinatorial functions
Clojure
343
star
19

spec-alpha2

Clojure library to describe the structure of data and functions
Clojure
297
star
20

data.csv

CSV reader/writer to/from Clojure data structures
Clojure
270
star
21

core.memoize

A manipulable, pluggable, memoization framework for Clojure
Clojure
263
star
22

tools.analyzer

An analyzer for Clojure code, written in Clojure and producing AST in EDN
Clojure
257
star
23

clojure-site

clojure.org site
HTML
249
star
24

data.xml

Clojure
220
star
25

data.finger-tree

Finger Tree data structure
Clojure
213
star
26

spec.alpha

Clojure library to describe the structure of data and functions
Clojure
212
star
27

tools.reader

Clojure reader in Clojure
Clojure
203
star
28

tools.build

Clojure builds as Clojure programs
Clojure
200
star
29

core.rrb-vector

RRB-Trees in Clojure
Clojure
191
star
30

data.priority-map

Clojure priority map data structure
Clojure
186
star
31

math.numeric-tower

Math functions that deal intelligently with the various types in Clojure's numeric tower
Clojure
175
star
32

test.generative

Generative test runner
Clojure
161
star
33

core.unify

Unification library
Clojure
137
star
34

core.contracts

Contracts programming
Clojure
127
star
35

data.fressian

Read and write Fressian data from Clojure
Clojure
127
star
36

data.avl

Persistent sorted maps and sets with log-time rank queries
Clojure
125
star
37

data.int-map

A map optimized for integer keys
Java
124
star
38

core.incubator

Proving ground for proposed new core fns
Clojure
116
star
39

java.data

Functions for recursively converting Java beans to Clojure and vice versa
Clojure
114
star
40

tools.analyzer.jvm

Additional jvm-specific passes for tools.analyzer
Clojure
113
star
41

tools.macro

Utilities for macro writers
Clojure
113
star
42

clojurescript-site

website for ClojureScript
Shell
106
star
43

tools.deps.graph

Dependency graphs for deps.edn projects
Clojure
106
star
44

java.jmx

Produce and consume JMX beans from Clojure
Clojure
94
star
45

algo.generic

Generic versions of commonly used functions, implemented as multimethods that can be implemented for any data type
Clojure
92
star
46

tools.emitter.jvm

A JVM bytecode generator for ASTs compatible with tools.analyzer(.jvm)
Clojure
86
star
47

data.generators

Random data generators
Clojure
85
star
48

data.zip

Utilities for clojure.zip
Clojure
83
star
49

brew-install

Clojure CLI installer
Shell
81
star
50

data.codec

Native codec implementations
Clojure
74
star
51

tools.gitlibs

API for retrieving, caching, and programatically accessing git libraries
Clojure
62
star
52

java.classpath

Examine the Java classpath from Clojure programs
Clojure
59
star
53

jvm.tools.analyzer

Clojure
53
star
54

core.specs.alpha

specs to describe Clojure core macros and functions
Clojure
47
star
55

tools.tools

Clojure CLI tool for managing Clojure CLI tools
Clojure
42
star
56

homebrew-tools

Clojure homebrew tap providing Clojure formulae
Ruby
41
star
57

data.alpha.replicant-server

A Clojure library providing remote implementations of the Clojure data structures and a remote REPL server.
Clojure
37
star
58

test.benchmark

Benchmark and Regression Suite for Clojure
Roff
37
star
59

clr.tools.nrepl

Clojure
25
star
60

build.ci

Support scripts for continuous integration
Clojure
23
star
61

tools.analyzer.js

Provides js-specific passes for tools.analyzer
Clojure
21
star
62

algo.graph

Basic graph theory algorithms
Clojure
16
star
63

clojure-install

Java
16
star
64

data.alpha.replicant-client

A Clojure library providing client-side implementations of Clojure datastructures served by replicant-server.
Clojure
13
star
65

clojure.github.com

Documentation repos
HTML
8
star
66

build.poms

Parent POMs
8
star
67

core.typed.analyzer.jvm

Clojure
7
star
68

clr.tools.namespace

Clojure
7
star
69

core.typed.runtime.jvm

Clojure
7
star
70

clr.data.json

JSON in Clojure on the CLR
Clojure
6
star
71

clr.tools.reader

Clojure
5
star
72

clr.test.generative

Clojure
5
star
73

clojure-api-doc

Clojure API doc build
Clojure
5
star
74

contrib-api-doc

Clojure contrib API doc build
Clojure
5
star
75

core.typed.annotator.jvm

Clojure
5
star
76

core.typed.checker.jvm

Clojure
4
star
77

core.typed.checker.js

Clojure
4
star
78

io.incubator

Proving ground for proposed new io fns
4
star
79

clr.data.generators

Random data generators for Clojure on the CLR
Clojure
4
star
80

clr.core.async

Port of Clojure core.async to the CLR
Clojure
3
star
81

clr.spec.alpha

spec on the CLR
Clojure
3
star
82

clr.tools.analyzer

Clojure
3
star
83

test.regression

Regression tests for Clojure
Clojure
3
star
84

tools.deps.cli

Deps functions
Clojure
2
star
85

clr.core.specs.alpha

core specs on CLR
HTML
2
star
86

java.internal.invoke

2
star
87

clr.tools.gitlibs

An API for retrieving, caching, and programatically accessing git libraries
HTML
2
star
88

clr.core.logic

Clojure
2
star
89

clr.tools.trace

1
star
90

clr.core.cli

Clojure
1
star
91

clr.data.priority-map

ClojureCLR port of data.priority-map
Clojure
1
star
92

cljs.tools.closure

ClojureScript build of Google Closure
Shell
1
star
93

tools.analyzer.clr

additional clr-specific passes for tools.analyzer
Clojure
1
star
94

clr.test.check

Clojure
1
star
95

clr.core.cache

ClojureCLR port of core.cache
Clojure
1
star
96

clr.tools.logging

1
star
97

build.test

Dummy project for testing contrib build and deploy
Clojure
1
star
98

clr.core.memoize

ClojureCLR port of core.memoize
Clojure
1
star