Skip to content

Commit 5933616

Browse files
sebhossSebastian Hoß
andauthored
fix #192 add support for cache2k (#200)
* fix #192 add support for cache2k Co-authored-by: Sebastian Hoß <seb@hoß.de>
1 parent 5aaca05 commit 5933616

File tree

21 files changed

+5991
-1635
lines changed

21 files changed

+5991
-1635
lines changed

README.md

Lines changed: 79 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,30 @@ Java [memoization](https://en.wikipedia.org/wiki/Memoization) library - trade sp
1010
## Features
1111

1212
* Memoize calls to `Consumer`, `Function`, `Predicate`, `Supplier` and other functional interfaces in `java.util.function`
13-
* Cache values using [Caffeine](https://github.com/ben-manes/caffeine), [Guava](https://github.com/google/guava/wiki/CachesExplained), or any [`ConcurrentMap`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/ConcurrentMap.html).
13+
* Cache values using [Caffeine](https://github.com/ben-manes/caffeine), [Guava](https://github.com/google/guava/wiki/CachesExplained), [cache2k](https://cache2k.org/), or any [`ConcurrentMap`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/ConcurrentMap.html).
1414
* Customize caches and the cache keys
1515

1616
## Usage
1717

1818
Memoize any of the supported types by using the static factory methods supplied by:
1919

20+
* `Cache2kMemoize` if you want to use [cache2k](https://cache2k.org/) caches.
2021
* `CaffeineMemoize` if you want to use [Caffeine](https://github.com/ben-manes/caffeine) caches.
2122
* `GuavaMemoize` if you want to use [Guava](https://github.com/google/guava/wiki/CachesExplained) caches.
2223
* `MapMemoize` if you want to use any [`ConcurrentMap`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/ConcurrentMap.html) as cache.
2324

2425
### Default cache with default cache keys
2526

2627
```java
28+
wtf.metio.memoization.cache2k.Cache2kMemoize;
2729
wtf.metio.memoization.caffeine.CaffeineMemoize;
2830
wtf.metio.memoization.guava.GuavaMemoize;
2931
wtf.metio.memoization.map.MapMemoize;
3032

33+
// memoize in cache2k cache
34+
Consumer<INPUT> consumer = ...;
35+
Consumer<INPUT> memoizedConsumer = Cache2kMemoize.consumer(consumer);
36+
3137
// memoize in Caffeine cache
3238
Consumer<INPUT> consumer = ...;
3339
Consumer<INPUT> memoizedConsumer = CaffeineMemoize.consumer(consumer);
@@ -44,10 +50,16 @@ Supplier<OUTPUT> memoizedSupplier = MapMemoize.supplier(supplier);
4450
### Default cache with custom cache keys
4551

4652
```java
53+
wtf.metio.memoization.cache2k.Cache2kMemoize;
4754
wtf.metio.memoization.caffeine.CaffeineMemoize;
4855
wtf.metio.memoization.guava.GuavaMemoize;
4956
wtf.metio.memoization.map.MapMemoize;
5057

58+
// memoize in cache2k cache
59+
Consumer<INPUT> consumer = ...;
60+
Function<INPUT, KEY> keyFunction = ...;
61+
Consumer<INPUT> memoizedConsumer = Cache2kMemoize.consumer(consumer, keyFunction);
62+
5163
// memoize in Caffeine cache
5264
Consumer<INPUT> consumer = ...;
5365
Function<INPUT, KEY> keyFunction = ...;
@@ -67,10 +79,16 @@ Supplier<OUTPUT> memoizedSupplier = MapMemoize.supplier(supplier, keySupp
6779
### Custom cache with default cache keys
6880

6981
```java
82+
wtf.metio.memoization.cache2k.Cache2kMemoize;
7083
wtf.metio.memoization.caffeine.CaffeineMemoize;
7184
wtf.metio.memoization.guava.GuavaMemoize;
7285
wtf.metio.memoization.map.MapMemoize;
7386

87+
// memoize in cache2k cache
88+
Consumer<INPUT> consumer = ...;
89+
Cache<INPUT, INPUT> cache = ...; // org.cache2k.Cache
90+
Consumer<INPUT> memoizedConsumer = Cache2kMemoize.consumer(consumer, cache);
91+
7492
// memoize in Caffeine cache
7593
Consumer<INPUT> consumer = ...;
7694
Cache<INPUT, INPUT> cache = ...; // com.github.benmanes.caffeine.cache.Cache
@@ -90,10 +108,17 @@ Supplier<OUTPUT> memoizedSupplier = MapMemoize.supplier(supplier, cache);
90108
### Custom cache with custom cache keys
91109

92110
```java
111+
wtf.metio.memoization.cache2k.Cache2kMemoize;
93112
wtf.metio.memoization.caffeine.CaffeineMemoize;
94113
wtf.metio.memoization.guava.GuavaMemoize;
95114
wtf.metio.memoization.map.MapMemoize;
96115

116+
// memoize in cache2k cache
117+
Consumer<INPUT> consumer = ...;
118+
Function<INPUT, KEY> keyFunction = ...;
119+
Cache<KEY, INPUT> cache = ...; // org.cache2k.Cache
120+
Consumer<INPUT> memoizedConsumer = CaffeineMemoize.consumer(consumer, keyFunction, cache);
121+
97122
// memoize in Caffeine cache
98123
Consumer<INPUT> consumer = ...;
99124
Function<INPUT, KEY> keyFunction = ...;
@@ -121,44 +146,63 @@ In order to use this project, declare the following dependencies in your project
121146

122147
```xml
123148
<dependencies>
124-
<!-- ConcurrentMap ONLY -->
125-
<dependency>
126-
<groupId>wtf.metio.memoization</groupId>
127-
<artifactId>memoization-map</artifactId>
128-
<version>${version.memoization}</version>
129-
</dependency>
130-
<!-- ConcurrentMap ONLY -->
131-
132-
<!-- Caffeine ONLY -->
133-
<dependency>
134-
<groupId>wtf.metio.memoization</groupId>
135-
<artifactId>memoization-caffeine</artifactId>
136-
<version>${version.memoization}</version>
137-
</dependency>
138-
<dependency>
139-
<groupId>com.github.ben-manes.caffeine</groupId>
140-
<artifactId>caffeine</artifactId>
141-
<version>${version.caffeine}</version>
142-
</dependency>
143-
<!-- Caffeine ONLY -->
144-
145-
<!-- Guava ONLY -->
146-
<dependency>
147-
<groupId>wtf.metio.memoization</groupId>
148-
<artifactId>memoization-guava</artifactId>
149-
<version>${version.memoization}</version>
150-
</dependency>
151-
<dependency>
152-
<groupId>com.google.guava</groupId>
153-
<artifactId>guava</artifactId>
154-
<version>${version.guava}</version>
155-
</dependency>
156-
<!-- Guava ONLY -->
149+
<!-- ConcurrentMap ONLY -->
150+
<dependency>
151+
<groupId>wtf.metio.memoization</groupId>
152+
<artifactId>memoization-map</artifactId>
153+
<version>${version.memoization}</version>
154+
</dependency>
155+
<!-- ConcurrentMap ONLY -->
156+
157+
<!-- cache2k ONLY -->
158+
<dependency>
159+
<groupId>wtf.metio.memoization</groupId>
160+
<artifactId>memoization-cache2k</artifactId>
161+
<version>${version.memoization}</version>
162+
</dependency>
163+
<dependency>
164+
<groupId>org.cache2k</groupId>
165+
<artifactId>cache2k-api</artifactId>
166+
<version>${version.cache2k}</version>
167+
</dependency>
168+
<dependency>
169+
<groupId>org.cache2k</groupId>
170+
<artifactId>cache2k-core</artifactId>
171+
<version>${version.cache2k}</version>
172+
<scope>runtime</scope>
173+
</dependency>
174+
<!-- cache2k ONLY -->
175+
176+
<!-- Caffeine ONLY -->
177+
<dependency>
178+
<groupId>wtf.metio.memoization</groupId>
179+
<artifactId>memoization-caffeine</artifactId>
180+
<version>${version.memoization}</version>
181+
</dependency>
182+
<dependency>
183+
<groupId>com.github.ben-manes.caffeine</groupId>
184+
<artifactId>caffeine</artifactId>
185+
<version>${version.caffeine}</version>
186+
</dependency>
187+
<!-- Caffeine ONLY -->
188+
189+
<!-- Guava ONLY -->
190+
<dependency>
191+
<groupId>wtf.metio.memoization</groupId>
192+
<artifactId>memoization-guava</artifactId>
193+
<version>${version.memoization}</version>
194+
</dependency>
195+
<dependency>
196+
<groupId>com.google.guava</groupId>
197+
<artifactId>guava</artifactId>
198+
<version>${version.guava}</version>
199+
</dependency>
200+
<!-- Guava ONLY -->
157201

158202
</dependencies>
159203
```
160204

161-
Replace `${version.memoization}` with the latest release. Use [ExpiringMap](https://github.com/jhalterman/expiringmap), [ConcurrentLinkedHashMap](https://github.com/ben-manes/concurrentlinkedhashmap), [Chronicle-Map](https://github.com/OpenHFT/Chronicle-Map), [Cacheonix](http://www.cacheonix.org/) or other `ConcurrentMap` implementations as alternatives to the default `ConcurrentHashMap` used in the `MapMemoize` factory. Caches like [cache2k](http://cache2k.org/) can be used together with `MapMemoize` by calling `cache.asMap()`.
205+
Replace `${version.memoization}` with the latest release. Use [ExpiringMap](https://github.com/jhalterman/expiringmap), [ConcurrentLinkedHashMap](https://github.com/ben-manes/concurrentlinkedhashmap), [Chronicle-Map](https://github.com/OpenHFT/Chronicle-Map), [Cacheonix](http://www.cacheonix.org/) or other `ConcurrentMap` implementations as alternatives to the default `ConcurrentHashMap` used in the `MapMemoize` factory.
162206

163207
## Alternatives
164208

memoization-cache2k/pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ SPDX-FileCopyrightText: The memoization.java Authors
4+
~ SPDX-License-Identifier: 0BSD
5+
-->
6+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
7+
<modelVersion>4.0.0</modelVersion>
8+
9+
<!-- ordering follows https://maven.apache.org/developers/conventions/code.html#POM_Code_Convention -->
10+
11+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
12+
<!-- PARENT -->
13+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
14+
<!-- https://maven.apache.org/pom.html#Inheritance -->
15+
<parent>
16+
<groupId>wtf.metio.memoization</groupId>
17+
<artifactId>memoization.java</artifactId>
18+
<version>9999.99.99-SNAPSHOT</version>
19+
</parent>
20+
21+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
22+
<!-- COORDINATES -->
23+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
24+
<!-- https://maven.apache.org/pom.html#Maven_Coordinates -->
25+
<artifactId>memoization-cache2k</artifactId>
26+
27+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
28+
<!-- INFORMATIONS -->
29+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
30+
<!-- https://maven.apache.org/pom.html#More_Project_Information -->
31+
<name>memoization.java :: cache2k</name>
32+
33+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
34+
<!-- DEPENDENCIES -->
35+
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
36+
<!-- https://maven.apache.org/pom.html#Dependencies -->
37+
<dependencies>
38+
<dependency>
39+
<groupId>wtf.metio.memoization</groupId>
40+
<artifactId>memoization-core</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>wtf.metio.memoization</groupId>
44+
<artifactId>memoization-map</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>wtf.metio.memoization</groupId>
48+
<artifactId>memoization-tck</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.cache2k</groupId>
53+
<artifactId>cache2k-api</artifactId>
54+
<version>2.6.1.Final</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.cache2k</groupId>
58+
<artifactId>cache2k-core</artifactId>
59+
<version>2.6.1.Final</version>
60+
<scope>test</scope>
61+
</dependency>
62+
</dependencies>
63+
64+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* SPDX-FileCopyrightText: The memoization.java Authors
3+
* SPDX-License-Identifier: 0BSD
4+
*/
5+
/**
6+
* The cache2k module contains the cache2k based implementation of memoization.java
7+
*/
8+
module wtf.metio.memoization.cache2k {
9+
10+
requires org.cache2k.api;
11+
requires wtf.metio.memoization.map;
12+
requires wtf.metio.memoization.core;
13+
14+
exports wtf.metio.memoization.cache2k;
15+
16+
}

0 commit comments

Comments
 (0)