- refactored CacheHelper class (#756)

This commit is contained in:
Ante Pocedulic 2016-10-18 16:54:54 +02:00 committed by Grzegorz Piwowarek
parent de1b5e0a17
commit 35009ad3c4
2 changed files with 6 additions and 26 deletions

View File

@ -1,24 +0,0 @@
package org.baeldung.ehcache.app;
import org.baeldung.ehcache.calculator.SquaredCalculator;
import org.baeldung.ehcache.config.CacheHelper;
public class App {
public static void main(String[] args) {
SquaredCalculator squaredCalculator = new SquaredCalculator();
CacheHelper cacheHelper = new CacheHelper();
squaredCalculator.setCache(cacheHelper);
calculate(squaredCalculator);
calculate(squaredCalculator);
}
private static void calculate(SquaredCalculator squaredCalculator) {
for (int i = 10; i < 15; i++) {
System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n");
}
}
}

View File

@ -12,14 +12,18 @@ public class CacheHelper {
private Cache<Integer, Integer> squareNumberCache;
public CacheHelper() {
cacheManager = CacheManagerBuilder.newCacheManagerBuilder().withCache("squaredNumber", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, Integer.class, ResourcePoolsBuilder.heap(10))).build();
cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build();
cacheManager.init();
squareNumberCache = cacheManager.getCache("squaredNumber", Integer.class, Integer.class);
squareNumberCache = cacheManager.createCache("squaredNumber", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, Integer.class, ResourcePoolsBuilder.heap(10)));
}
public Cache<Integer, Integer> getSquareNumberCache() {
return squareNumberCache;
}
public Cache<Integer, Integer> getSquareNumberCacheFromCacheManager() {
return cacheManager.getCache("squaredNumber", Integer.class, Integer.class);
}
}