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 static final String TRANSACTIONAL_CACHE = "transactional-cache";
public DefaultCacheManager cacheManager() { public DefaultCacheManager cacheManager() {
DefaultCacheManager cacheManager = new DefaultCacheManager(); return new DefaultCacheManager();
return cacheManager;
} }
public Cache<String, Integer> transactionalCache(DefaultCacheManager cacheManager, CacheListener listener) { public Cache<String, Integer> transactionalCache(DefaultCacheManager cacheManager, CacheListener listener) {

View File

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

View File

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

View File

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