Skip to content

Commit aa612b7

Browse files
Ahmed Tawilamaibin
authored andcommitted
BAEL-1109 Introduction to JCache (eugenp#2594)
* Evaluation article: Different Types of Bean Injection in Spring * added tests & changed configuration to Java-based config * removed xml config files * rename unit tests * BAEL-972 - Apache Commons Text * remove code from evaluation article * remove code from evaluation article * BAEL-972 - Apache Commons Text - added another example * BAEL-972 - Apache Commons Text - just indentation * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java - fix problems * BAEL-1033 Introduction to StreamUtils * BAEL-1033 Introduction to StreamUtils * BAEL-1033 Introduction to StreamUtils * fix formatting * BAEL-1033 minor refactor * BAEL-1035 Introduction to Eclipse Collections * format * BAEL-1035 Introduction to Eclipse Collections * BAEL-1035 Introduction to Eclipse Collections * BAEL-1035 Introduction to Eclipse Collections * cleanup * cleanup * BAEL-1109 Introduction to JCache * BAEL-1109 Introduction to JCache * remove unneeded property in pom.xml * fix formatting
1 parent 2b5a6c1 commit aa612b7

8 files changed

Lines changed: 255 additions & 20 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -555,25 +555,35 @@
555555
<version>${protonpack.version}</version>
556556
</dependency>
557557
<dependency>
558-
<groupId>org.functionaljava</groupId>
559-
<artifactId>functionaljava</artifactId>
560-
<version>4.7</version>
561-
</dependency>
562-
<dependency>
563-
<groupId>org.functionaljava</groupId>
564-
<artifactId>functionaljava-java8</artifactId>
565-
<version>4.7</version>
566-
</dependency>
567-
<dependency>
568-
<groupId>org.functionaljava</groupId>
569-
<artifactId>functionaljava-quickcheck</artifactId>
570-
<version>4.7</version>
571-
</dependency>
572-
<dependency>
573-
<groupId>org.functionaljava</groupId>
574-
<artifactId>functionaljava-java-core</artifactId>
575-
<version>4.7</version>
576-
</dependency>
558+
<groupId>org.functionaljava</groupId>
559+
<artifactId>functionaljava</artifactId>
560+
<version>4.7</version>
561+
</dependency>
562+
<dependency>
563+
<groupId>org.functionaljava</groupId>
564+
<artifactId>functionaljava-java8</artifactId>
565+
<version>4.7</version>
566+
</dependency>
567+
<dependency>
568+
<groupId>org.functionaljava</groupId>
569+
<artifactId>functionaljava-quickcheck</artifactId>
570+
<version>4.7</version>
571+
</dependency>
572+
<dependency>
573+
<groupId>org.functionaljava</groupId>
574+
<artifactId>functionaljava-java-core</artifactId>
575+
<version>4.7</version>
576+
</dependency>
577+
<dependency>
578+
<groupId>javax.cache</groupId>
579+
<artifactId>cache-api</artifactId>
580+
<version>${cache.version}</version>
581+
</dependency>
582+
<dependency>
583+
<groupId>com.hazelcast</groupId>
584+
<artifactId>hazelcast</artifactId>
585+
<version>${hazelcast.version}</version>
586+
</dependency>
577587
</dependencies>
578588
<repositories>
579589
<repository>
@@ -653,5 +663,7 @@
653663
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
654664
<protonpack.version>1.14</protonpack.version>
655665
<unit-ri.version>1.0.3</unit-ri.version>
666+
<cache.version>1.0.0</cache.version>
667+
<hazelcast.version>3.8.4</hazelcast.version>
656668
</properties>
657-
</project>
669+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.jcache;
2+
3+
import java.io.Serializable;
4+
5+
import javax.cache.event.CacheEntryCreatedListener;
6+
import javax.cache.event.CacheEntryEvent;
7+
import javax.cache.event.CacheEntryListenerException;
8+
import javax.cache.event.CacheEntryUpdatedListener;
9+
10+
public class SimpleCacheEntryListener implements CacheEntryCreatedListener<String, String>, CacheEntryUpdatedListener<String, String>, Serializable {
11+
12+
/**
13+
*
14+
*/
15+
private static final long serialVersionUID = -712657810462878763L;
16+
private boolean updated;
17+
private boolean created;
18+
19+
public boolean getUpdated() {
20+
return this.updated;
21+
}
22+
23+
public boolean getCreated() {
24+
return this.created;
25+
}
26+
27+
public void onUpdated(Iterable<CacheEntryEvent<? extends String, ? extends String>> events) throws CacheEntryListenerException {
28+
this.updated = true;
29+
}
30+
31+
public void onCreated(Iterable<CacheEntryEvent<? extends String, ? extends String>> events) throws CacheEntryListenerException {
32+
this.created = true;
33+
}
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.jcache;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import javax.cache.integration.CacheLoader;
7+
import javax.cache.integration.CacheLoaderException;
8+
9+
public class SimpleCacheLoader implements CacheLoader<Integer, String> {
10+
11+
@Override
12+
public String load(Integer key) throws CacheLoaderException {
13+
return "fromCache" + key;
14+
}
15+
16+
@Override
17+
public Map<Integer, String> loadAll(Iterable<? extends Integer> keys) throws CacheLoaderException {
18+
Map<Integer, String> data = new HashMap<>();
19+
for (int key : keys) {
20+
data.put(key, load(key));
21+
}
22+
return data;
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.jcache;
2+
3+
import java.io.Serializable;
4+
5+
import javax.cache.processor.EntryProcessor;
6+
import javax.cache.processor.EntryProcessorException;
7+
import javax.cache.processor.MutableEntry;
8+
9+
public class SimpleEntryProcessor implements EntryProcessor<String, String, String>, Serializable {
10+
11+
/**
12+
*
13+
*/
14+
private static final long serialVersionUID = -5616476363722945132L;
15+
16+
public String process(MutableEntry<String, String> entry, Object... args) throws EntryProcessorException {
17+
18+
if (entry.exists()) {
19+
String current = entry.getValue();
20+
entry.setValue(current + " - modified");
21+
return current;
22+
}
23+
return null;
24+
}
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.jcache;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import javax.cache.Cache;
6+
import javax.cache.CacheManager;
7+
import javax.cache.Caching;
8+
import javax.cache.configuration.FactoryBuilder;
9+
import javax.cache.configuration.MutableConfiguration;
10+
import javax.cache.spi.CachingProvider;
11+
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
15+
public class CacheLoaderTest {
16+
17+
private Cache<Integer, String> cache;
18+
19+
@Before
20+
public void setup() {
21+
CachingProvider cachingProvider = Caching.getCachingProvider();
22+
CacheManager cacheManager = cachingProvider.getCacheManager();
23+
MutableConfiguration<Integer, String> config = new MutableConfiguration<Integer, String>().setReadThrough(true)
24+
.setCacheLoaderFactory(new FactoryBuilder.SingletonFactory<>(new SimpleCacheLoader()));
25+
this.cache = cacheManager.createCache("SimpleCache", config);
26+
}
27+
28+
@Test
29+
public void whenReadingFromStorage_thenCorrect() {
30+
for (int i = 1; i < 4; i++) {
31+
String value = cache.get(i);
32+
assertEquals("fromCache" + i, value);
33+
}
34+
}
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.jcache;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import javax.cache.Cache;
6+
import javax.cache.CacheManager;
7+
import javax.cache.Caching;
8+
import javax.cache.configuration.MutableConfiguration;
9+
import javax.cache.spi.CachingProvider;
10+
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
14+
public class EntryProcessorTest {
15+
16+
private Cache<String, String> cache;
17+
18+
@Before
19+
public void instantiateCache() {
20+
CachingProvider cachingProvider = Caching.getCachingProvider();
21+
CacheManager cacheManager = cachingProvider.getCacheManager();
22+
MutableConfiguration<String, String> config = new MutableConfiguration<String, String>();
23+
this.cache = cacheManager.createCache("MyCache", config);
24+
this.cache.put("key", "value");
25+
}
26+
27+
@Test
28+
public void whenModifyValue_thenCorrect() {
29+
this.cache.invoke("key", new SimpleEntryProcessor());
30+
assertEquals("value - modified", cache.get("key"));
31+
}
32+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.jcache;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import javax.cache.Cache;
6+
import javax.cache.CacheManager;
7+
import javax.cache.Caching;
8+
import javax.cache.configuration.FactoryBuilder;
9+
import javax.cache.configuration.MutableCacheEntryListenerConfiguration;
10+
import javax.cache.configuration.MutableConfiguration;
11+
import javax.cache.spi.CachingProvider;
12+
13+
import org.junit.Before;
14+
import org.junit.Test;
15+
16+
public class EventListenerTest {
17+
18+
private Cache<String, String> cache;
19+
private SimpleCacheEntryListener listener;
20+
MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration;
21+
22+
@Before
23+
public void setup() {
24+
CachingProvider cachingProvider = Caching.getCachingProvider();
25+
CacheManager cacheManager = cachingProvider.getCacheManager();
26+
MutableConfiguration<String, String> config = new MutableConfiguration<String, String>();
27+
this.cache = cacheManager.createCache("MyCache", config);
28+
this.listener = new SimpleCacheEntryListener();
29+
}
30+
31+
@Test
32+
public void whenRunEvent_thenCorrect() throws InterruptedException {
33+
this.listenerConfiguration = new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(this.listener), null, false, true);
34+
this.cache.registerCacheEntryListener(this.listenerConfiguration);
35+
36+
assertEquals(false, this.listener.getCreated());
37+
38+
this.cache.put("key", "value");
39+
assertEquals(true, this.listener.getCreated());
40+
assertEquals(false, this.listener.getUpdated());
41+
42+
this.cache.put("key", "newValue");
43+
assertEquals(true, this.listener.getUpdated());
44+
}
45+
46+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.jcache;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import javax.cache.Cache;
6+
import javax.cache.CacheManager;
7+
import javax.cache.Caching;
8+
import javax.cache.configuration.MutableConfiguration;
9+
import javax.cache.spi.CachingProvider;
10+
11+
import org.junit.Test;
12+
13+
public class JCacheTest {
14+
15+
@Test
16+
public void instantiateCache() {
17+
CachingProvider cachingProvider = Caching.getCachingProvider();
18+
CacheManager cacheManager = cachingProvider.getCacheManager();
19+
MutableConfiguration<String, String> config = new MutableConfiguration<>();
20+
Cache<String, String> cache = cacheManager.createCache("simpleCache", config);
21+
cache.put("key1", "value1");
22+
cache.put("key2", "value2");
23+
assertEquals("value1", cache.get("key1"));
24+
assertEquals("value2", cache.get("key2"));
25+
cacheManager.close();
26+
}
27+
}

0 commit comments

Comments
 (0)