Refactor Infinispan example (#3684)

* Refactor Infinispan examples

* Refactor Infinispan examples
This commit is contained in:
Grzegorz Piwowarek 2018-02-19 17:20:16 +01:00 committed by Predrag Maric
parent 6da6722ec4
commit c0bf1c7a93
4 changed files with 9 additions and 29 deletions

View File

@ -20,8 +20,7 @@ public class CacheConfiguration {
public static final String TRANSACTIONAL_CACHE = "transactional-cache";
public DefaultCacheManager cacheManager() {
DefaultCacheManager cacheManager = new DefaultCacheManager();
return cacheManager;
return new DefaultCacheManager();
}
public Cache<String, Integer> transactionalCache(DefaultCacheManager cacheManager, CacheListener listener) {

View File

@ -40,9 +40,7 @@ public class CacheListener {
@CacheEntriesEvicted
public void entriesEvicted(CacheEntriesEvictedEvent<String, String> event) {
final StringBuilder builder = new StringBuilder();
event.getEntries().entrySet().forEach((e) ->
builder.append(e.getKey() + ", ")
);
event.getEntries().forEach((key, value) -> builder.append(key).append(", "));
System.out.println("Evicting following entries from cache: " + builder.toString());
}

View File

@ -31,12 +31,7 @@ public class HelloWorldService {
public String findSimpleHelloWorld() {
String cacheKey = "simple-hello";
String helloWorld = simpleHelloWorldCache.get(cacheKey);
if (helloWorld == null) {
helloWorld = repository.getHelloWorld();
simpleHelloWorldCache.put(cacheKey, helloWorld);
}
return helloWorld;
return simpleHelloWorldCache.computeIfAbsent(cacheKey, k -> repository.getHelloWorld());
}
public String findExpiringHelloWorld() {
@ -79,12 +74,7 @@ public class HelloWorldService {
}
public String findPassivatingHelloWorld(String key) {
String value = passivatingHelloWorldCache.get(key);
if(value == null) {
value = repository.getHelloWorld();
passivatingHelloWorldCache.put(key, value);
}
return value;
return passivatingHelloWorldCache.computeIfAbsent(key, k -> repository.getHelloWorld());
}
}

View File

@ -9,7 +9,7 @@ import org.infinispan.manager.DefaultCacheManager;
import org.junit.After;
import org.junit.Before;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
public class ConfigurationTest {
@ -47,7 +47,6 @@ public class ConfigurationTest {
passivatingHelloWorldCache);
this.transactionalService = new TransactionalService(transactionalCache);
}
@After
@ -55,15 +54,9 @@ public class ConfigurationTest {
cacheManager.stop();
}
protected long timeThis(Callable callable) {
try {
long milis = System.currentTimeMillis();
callable.call();
return System.currentTimeMillis() - milis;
} catch (Exception e) {
e.printStackTrace();
}
return 0l;
protected <T> long timeThis(Supplier<T> supplier) {
long millis = System.currentTimeMillis();
supplier.get();
return System.currentTimeMillis() - millis;
}
}