Ehcache code (#734)
* - created packages for each logical part of application - created validator for WebsiteUser rest API - created ValidatorEventRegister class which fixes known bug for not detecting generated events - created custom Exception Handler which creates better response messages * Code formatting * formated pom.xml replaced for loops with streams fixed bug while getting all beans * removed unnecessary code changed repository type * - added test for Spring Data REST APIs - changed bad request return code - formated code * - added source code for ehcache article - added ehcache dependency to pom.xml
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
9c8ef99ed3
commit
b36a7e00cc
@@ -0,0 +1,24 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package org.baeldung.ehcache.calculator;
|
||||
|
||||
import org.baeldung.ehcache.config.CacheHelper;
|
||||
|
||||
public class SquaredCalculator {
|
||||
private CacheHelper cache;
|
||||
|
||||
public int getSquareValueOfNumber(int input) {
|
||||
if (cache.getSquareNumberCache().containsKey(input)) {
|
||||
return cache.getSquareNumberCache().get(input);
|
||||
}
|
||||
|
||||
System.out.println("Calculating square value of " + input + " and caching result.");
|
||||
|
||||
int squaredValue = (int) Math.pow(input, 2);
|
||||
cache.getSquareNumberCache().put(input, squaredValue);
|
||||
|
||||
return squaredValue;
|
||||
}
|
||||
|
||||
public void setCache(CacheHelper cache) {
|
||||
this.cache = cache;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.baeldung.ehcache.config;
|
||||
|
||||
import org.ehcache.Cache;
|
||||
import org.ehcache.CacheManager;
|
||||
import org.ehcache.config.builders.CacheConfigurationBuilder;
|
||||
import org.ehcache.config.builders.CacheManagerBuilder;
|
||||
import org.ehcache.config.builders.ResourcePoolsBuilder;
|
||||
|
||||
public class CacheHelper {
|
||||
|
||||
private CacheManager cacheManager;
|
||||
private Cache<Integer, Integer> squareNumberCache;
|
||||
|
||||
public CacheHelper() {
|
||||
cacheManager = CacheManagerBuilder.newCacheManagerBuilder().withCache("squaredNumber", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, Integer.class, ResourcePoolsBuilder.heap(10))).build();
|
||||
cacheManager.init();
|
||||
|
||||
squareNumberCache = cacheManager.getCache("squaredNumber", Integer.class, Integer.class);
|
||||
}
|
||||
|
||||
public Cache<Integer, Integer> getSquareNumberCache() {
|
||||
return squareNumberCache;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user