Skip to content

Commit c0bf1c7

Browse files
pivovaritpedja4
authored andcommitted
Refactor Infinispan example (eugenp#3684)
* Refactor Infinispan examples * Refactor Infinispan examples
1 parent 6da6722 commit c0bf1c7

4 files changed

Lines changed: 9 additions & 29 deletions

File tree

‎libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public class CacheConfiguration {
2020
public static final String TRANSACTIONAL_CACHE = "transactional-cache";
2121

2222
public DefaultCacheManager cacheManager() {
23-
DefaultCacheManager cacheManager = new DefaultCacheManager();
24-
return cacheManager;
23+
return new DefaultCacheManager();
2524
}
2625

2726
public Cache<String, Integer> transactionalCache(DefaultCacheManager cacheManager, CacheListener listener) {

‎libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ public void entryLoaded(CacheEntryLoadedEvent<String, String> event) {
4040
@CacheEntriesEvicted
4141
public void entriesEvicted(CacheEntriesEvictedEvent<String, String> event) {
4242
final StringBuilder builder = new StringBuilder();
43-
event.getEntries().entrySet().forEach((e) ->
44-
builder.append(e.getKey() + ", ")
45-
);
43+
event.getEntries().forEach((key, value) -> builder.append(key).append(", "));
4644
System.out.println("Evicting following entries from cache: " + builder.toString());
4745
}
4846

‎libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ public HelloWorldService(HelloWorldRepository repository, CacheListener listener
3131

3232
public String findSimpleHelloWorld() {
3333
String cacheKey = "simple-hello";
34-
String helloWorld = simpleHelloWorldCache.get(cacheKey);
35-
if (helloWorld == null) {
36-
helloWorld = repository.getHelloWorld();
37-
simpleHelloWorldCache.put(cacheKey, helloWorld);
38-
}
39-
return helloWorld;
34+
return simpleHelloWorldCache.computeIfAbsent(cacheKey, k -> repository.getHelloWorld());
4035
}
4136

4237
public String findExpiringHelloWorld() {
@@ -79,12 +74,7 @@ public String findEvictingHelloWorld(String key) {
7974
}
8075

8176
public String findPassivatingHelloWorld(String key) {
82-
String value = passivatingHelloWorldCache.get(key);
83-
if(value == null) {
84-
value = repository.getHelloWorld();
85-
passivatingHelloWorldCache.put(key, value);
86-
}
87-
return value;
77+
return passivatingHelloWorldCache.computeIfAbsent(key, k -> repository.getHelloWorld());
8878
}
8979

9080
}

‎libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.junit.After;
1010
import org.junit.Before;
1111

12-
import java.util.concurrent.Callable;
12+
import java.util.function.Supplier;
1313

1414
public class ConfigurationTest {
1515

@@ -47,23 +47,16 @@ public void setup() {
4747
passivatingHelloWorldCache);
4848

4949
this.transactionalService = new TransactionalService(transactionalCache);
50-
5150
}
5251

5352
@After
5453
public void tearDown() {
5554
cacheManager.stop();
5655
}
5756

58-
protected long timeThis(Callable callable) {
59-
try {
60-
long milis = System.currentTimeMillis();
61-
callable.call();
62-
return System.currentTimeMillis() - milis;
63-
} catch (Exception e) {
64-
e.printStackTrace();
65-
}
66-
return 0l;
57+
protected <T> long timeThis(Supplier<T> supplier) {
58+
long millis = System.currentTimeMillis();
59+
supplier.get();
60+
return System.currentTimeMillis() - millis;
6761
}
68-
6962
}

0 commit comments

Comments
 (0)