JAVA-2421: Merge spring-ehcache module into spring-caching module (#10072)
* JAVA-2421: Moved 1 article from spring-ehcache to spring-caching * JAVA-2421: Removed module spring-ehcache * JAVA-2421: Removed module spring-ehcache from main pom
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.cachetest;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.cachetest.config;
|
||||
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class CacheConfig {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.cachetest.config;
|
||||
|
||||
import org.ehcache.event.CacheEvent;
|
||||
import org.ehcache.event.CacheEventListener;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class CacheEventLogger implements CacheEventListener<Object, Object> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(CacheEventLogger.class);
|
||||
|
||||
@Override
|
||||
public void onEvent(CacheEvent<? extends Object, ? extends Object> cacheEvent) {
|
||||
log.info("Cache event {} for item with key {}. Old value = {}, New value = {}", cacheEvent.getType(), cacheEvent.getKey(), cacheEvent.getOldValue(), cacheEvent.getNewValue());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.cachetest.rest;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.cachetest.service.NumberService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(path = "/number", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
public class NumberController {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(NumberController.class);
|
||||
|
||||
@Autowired
|
||||
private NumberService numberService;
|
||||
|
||||
@GetMapping(path = "/square/{number}")
|
||||
public String getThing(@PathVariable Long number) {
|
||||
log.info("call numberService to square {}", number);
|
||||
return String.format("{\"square\": %s}", numberService.square(number));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.cachetest.service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class NumberService {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(NumberService.class);
|
||||
|
||||
@Cacheable(value = "squareCache", key = "#number", condition = "#number>10")
|
||||
public BigDecimal square(Long number) {
|
||||
BigDecimal square = BigDecimal.valueOf(number)
|
||||
.multiply(BigDecimal.valueOf(number));
|
||||
log.info("square of {} is {}", number, square);
|
||||
return square;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,13 +6,15 @@ import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Book {
|
||||
public class Book implements Serializable {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
@@ -6,3 +6,6 @@ spring.datasource.password=
|
||||
# Enabling H2 Console
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2
|
||||
|
||||
#ehcache
|
||||
spring.cache.jcache.config=classpath:ehcache.xml
|
||||
|
||||
54
spring-caching/src/main/resources/ehcache.xml
Normal file
54
spring-caching/src/main/resources/ehcache.xml
Normal file
@@ -0,0 +1,54 @@
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.ehcache.org/v3"
|
||||
xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
|
||||
xsi:schemaLocation="
|
||||
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
|
||||
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
|
||||
|
||||
<cache alias="squareCache">
|
||||
<key-type>java.lang.Long</key-type>
|
||||
<value-type>java.math.BigDecimal</value-type>
|
||||
<expiry>
|
||||
<ttl unit="seconds">30</ttl>
|
||||
</expiry>
|
||||
|
||||
<listeners>
|
||||
<listener>
|
||||
<class>com.baeldung.cachetest.config.CacheEventLogger</class>
|
||||
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
|
||||
<event-ordering-mode>UNORDERED</event-ordering-mode>
|
||||
<events-to-fire-on>CREATED</events-to-fire-on>
|
||||
<events-to-fire-on>EXPIRED</events-to-fire-on>
|
||||
</listener>
|
||||
</listeners>
|
||||
|
||||
<resources>
|
||||
<heap unit="entries">2</heap>
|
||||
<offheap unit="MB">10</offheap>
|
||||
</resources>
|
||||
</cache>
|
||||
|
||||
<cache alias="books">
|
||||
<key-type>java.lang.String</key-type>
|
||||
<value-type>com.baeldung.springdatacaching.model.Book</value-type>
|
||||
<expiry>
|
||||
<ttl unit="seconds">30</ttl>
|
||||
</expiry>
|
||||
|
||||
<listeners>
|
||||
<listener>
|
||||
<class>com.baeldung.cachetest.config.CacheEventLogger</class>
|
||||
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
|
||||
<event-ordering-mode>UNORDERED</event-ordering-mode>
|
||||
<events-to-fire-on>CREATED</events-to-fire-on>
|
||||
<events-to-fire-on>EXPIRED</events-to-fire-on>
|
||||
</listener>
|
||||
</listeners>
|
||||
|
||||
<resources>
|
||||
<heap unit="entries">2</heap>
|
||||
<offheap unit="MB">10</offheap>
|
||||
</resources>
|
||||
</cache>
|
||||
|
||||
</config>
|
||||
Reference in New Issue
Block a user