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:
parent
9c8ef99ed3
commit
b36a7e00cc
|
@ -1,4 +1,5 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-all</artifactId>
|
<artifactId>spring-all</artifactId>
|
||||||
|
@ -160,6 +161,11 @@
|
||||||
<version>3.4</version>
|
<version>3.4</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.ehcache</groupId>
|
||||||
|
<artifactId>ehcache</artifactId>
|
||||||
|
<version>3.1.3</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue