Skip to content

Commit b34d507

Browse files
authored
[JAVA-40687] Moving some article links on Github - spring-boot-caching (#18122)
1 parent 1cf2280 commit b34d507

40 files changed

+122
-78
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## Relevant articles
2-
- [Spring Boot Cache with Redis](https://www.baeldung.com/spring-boot-redis-cache)
3-
- [Setting Time-To-Live Value for Caching](https://www.baeldung.com/spring-setting-ttl-value-cache)
42
- [Get All Cached Keys with Caffeine Cache in Spring Boot](https://www.baeldung.com/spring-boot-caffeine-spring-get-all-keys)
53
- [Implement Two-Level Cache With Spring](https://www.baeldung.com/spring-two-level-cache)
4+
- [Testing @Cacheable on Spring Data Repositories](https://www.baeldung.com/spring-data-testing-cacheable)
5+
- [Spring Cache – Creating a Custom KeyGenerator](http://www.baeldung.com/spring-cache-custom-keygenerator)
6+
- [Introduction To Ehcache](http://www.baeldung.com/ehcache)
67
- More articles: [[<-- prev]](/spring-boot-modules/spring-boot-caching)
78

spring-boot-modules/spring-boot-caching-2/pom.xml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
<groupId>org.springframework.boot</groupId>
2424
<artifactId>spring-boot-starter-data-jpa</artifactId>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-data-redis</artifactId>
29+
</dependency>
2630
<dependency>
2731
<groupId>org.springframework.data</groupId>
2832
<artifactId>spring-data-commons</artifactId>
@@ -41,10 +45,6 @@
4145
<groupId>org.springframework.boot</groupId>
4246
<artifactId>spring-boot-starter-cache</artifactId>
4347
</dependency>
44-
<dependency>
45-
<groupId>org.springframework.boot</groupId>
46-
<artifactId>spring-boot-starter-data-redis</artifactId>
47-
</dependency>
4848
<dependency>
4949
<groupId>com.github.ben-manes.caffeine</groupId>
5050
<artifactId>caffeine</artifactId>
@@ -62,12 +62,17 @@
6262
</exclusions>
6363
<scope>test</scope>
6464
</dependency>
65+
<dependency>
66+
<groupId>org.ehcache</groupId>
67+
<artifactId>ehcache</artifactId>
68+
<classifier>jakarta</classifier>
69+
</dependency>
6570
</dependencies>
6671

6772
<properties>
6873
<embedded.redis.version>1.4.0</embedded.redis.version>
6974
<caffeine.version>3.1.8</caffeine.version>
70-
<start-class>com.baeldung.caching.ttl.CachingTTLApplication</start-class>
75+
<start-class>com.baeldung.caching.twolevelcache.TwoLevelCacheApplication</start-class>
7176
</properties>
7277

7378
</project>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.baeldung.caching.config;
22

3+
import java.util.Arrays;
4+
35
import org.springframework.cache.Cache;
46
import org.springframework.cache.CacheManager;
57
import org.springframework.cache.annotation.EnableCaching;
@@ -9,8 +11,6 @@
911
import org.springframework.context.annotation.Bean;
1012
import org.springframework.context.annotation.Configuration;
1113

12-
import java.util.Arrays;
13-
1414
@EnableCaching
1515
@Configuration
1616
public class ApplicationCacheConfig {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.baeldung.caching.config;
22

3+
import java.lang.reflect.Method;
4+
35
import org.springframework.cache.interceptor.KeyGenerator;
46
import org.springframework.util.StringUtils;
57

6-
import java.lang.reflect.Method;
7-
88
public class CustomKeyGenerator implements KeyGenerator {
99

1010
public Object generate(Object target, Method method, Object... params) {

spring-boot-modules/spring-boot-caching/src/main/java/com/baeldung/caching/example/BookService.java renamed to spring-boot-modules/spring-boot-caching-2/src/main/java/com/baeldung/caching/example/BookService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.baeldung.caching.example;
22

3-
import com.baeldung.caching.model.Book;
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
46
import org.springframework.cache.annotation.Cacheable;
57
import org.springframework.stereotype.Component;
68

7-
import java.util.ArrayList;
8-
import java.util.List;
9+
import com.baeldung.caching.model.Book;
910

1011
@Component
1112
public class BookService {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.baeldung.ehcache.calculator;
22

3-
import com.baeldung.ehcache.config.CacheHelper;
43
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
65

6+
import com.baeldung.ehcache.config.CacheHelper;
7+
78
public class SquaredCalculator {
89

910
private static final Logger LOGGER = LoggerFactory.getLogger(SquaredCalculator.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.springdatacaching;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cache.annotation.EnableCaching;
6+
//import org.springframework.context.annotation.ComponentScan;
7+
8+
@SpringBootApplication
9+
@EnableCaching
10+
//to run any module like ehcache,caching or multiplecachemanager in local machine
11+
//add it's package for ComponentScan like below
12+
//@ComponentScan("com.baeldung.multiplecachemanager")
13+
public class CacheApplication {
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(CacheApplication.class, args);
17+
}
18+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.baeldung.springdatacaching.repositories;
22

3-
import com.baeldung.springdatacaching.model.Book;
3+
import java.util.Optional;
4+
import java.util.UUID;
5+
46
import org.springframework.cache.annotation.Cacheable;
57
import org.springframework.data.repository.CrudRepository;
68

7-
import java.util.Optional;
8-
import java.util.UUID;
9+
import com.baeldung.springdatacaching.model.Book;
910

1011
public interface BookRepository extends CrudRepository<Book, UUID> {
1112

spring-boot-modules/spring-boot-caching-2/src/main/resources/application.properties

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ spring.datasource.password=
66
spring.h2.console.enabled=true
77
spring.h2.console.path=/h2
88
spring.jpa.hibernate.ddl-auto=update
9-
#setting cache TTL
10-
caching.spring.hotelListTTL=43200
119
# Connection details
1210
#spring.redis.host=localhost
1311
#spring.redis.port=6379
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.baeldung.ehcache;
22

3-
import com.baeldung.ehcache.calculator.SquaredCalculator;
4-
import com.baeldung.ehcache.config.CacheHelper;
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
56
import org.junit.Before;
67
import org.junit.Test;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
910

10-
import static org.junit.Assert.assertFalse;
11-
import static org.junit.Assert.assertTrue;
11+
import com.baeldung.ehcache.calculator.SquaredCalculator;
12+
import com.baeldung.ehcache.config.CacheHelper;
1213

1314
public class SquareCalculatorUnitTest {
1415

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.baeldung.springdatacaching.repositories;
22

3-
import com.baeldung.caching.boot.CacheApplication;
4-
import com.baeldung.springdatacaching.model.Book;
3+
import static java.util.Optional.empty;
4+
import static java.util.Optional.ofNullable;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
import java.util.Optional;
8+
import java.util.UUID;
9+
510
import org.junit.jupiter.api.BeforeEach;
611
import org.junit.jupiter.api.Test;
712
import org.junit.jupiter.api.extension.ExtendWith;
@@ -12,12 +17,8 @@
1217
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
1318
import org.springframework.test.context.junit.jupiter.SpringExtension;
1419

15-
import java.util.Optional;
16-
import java.util.UUID;
17-
18-
import static java.util.Optional.empty;
19-
import static java.util.Optional.ofNullable;
20-
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import com.baeldung.springdatacaching.CacheApplication;
21+
import com.baeldung.springdatacaching.model.Book;
2122

2223
@ExtendWith(SpringExtension.class)
2324
@EntityScan(basePackageClasses = Book.class)

spring-boot-modules/spring-boot-caching-2/src/test/resources/application.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ spring.h2.console.enabled=true
88
spring.h2.console.path=/h2
99
spring.jpa.hibernate.ddl-auto=update
1010
spring.jpa.show-sql=false
11-
caching.spring.hotelListTTL=43200
1211
# Connection details
1312
#spring.redis.host=localhost
1413
#spring.redis.port=6379
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
### Relevant articles:
2-
- [Introduction To Ehcache](http://www.baeldung.com/ehcache)
32
- [A Guide To Caching in Spring](http://www.baeldung.com/spring-cache-tutorial)
4-
- [Spring Cache – Creating a Custom KeyGenerator](http://www.baeldung.com/spring-cache-custom-keygenerator)
53
- [Cache Eviction in Spring Boot](https://www.baeldung.com/spring-boot-evict-cache)
64
- [Using Multiple Cache Managers in Spring](https://www.baeldung.com/spring-multiple-cache-managers)
7-
- [Testing @Cacheable on Spring Data Repositories](https://www.baeldung.com/spring-data-testing-cacheable)
85
- [Spring Boot Ehcache Example](https://www.baeldung.com/spring-boot-ehcache)
6+
- [Setting Time-To-Live Value for Caching](https://www.baeldung.com/spring-setting-ttl-value-cache)
7+
- [Spring Boot Cache with Redis](https://www.baeldung.com/spring-boot-redis-cache)
98
- More articles: [[next -->]](/spring-boot-modules/spring-boot-caching-2)

spring-boot-modules/spring-boot-caching/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@
7171
<groupId>org.springframework.data</groupId>
7272
<artifactId>spring-data-commons</artifactId>
7373
</dependency>
74+
<dependency>
75+
<groupId>org.springframework.boot</groupId>
76+
<artifactId>spring-boot-starter-data-redis</artifactId>
77+
</dependency>
78+
<dependency>
79+
<groupId>com.github.codemonstur</groupId>
80+
<artifactId>embedded-redis</artifactId>
81+
<version>${embedded.redis.version}</version>
82+
<exclusions>
83+
<exclusion>
84+
<groupId>org.slf4j</groupId>
85+
<artifactId>slf4j-simple</artifactId>
86+
</exclusion>
87+
</exclusions>
88+
<scope>test</scope>
89+
</dependency>
7490
</dependencies>
7591

7692
<build>
@@ -87,6 +103,7 @@
87103

88104
<properties>
89105
<start-class>com.baeldung.caching.boot.CacheApplication</start-class>
106+
<embedded.redis.version>1.4.0</embedded.redis.version>
90107
</properties>
91108

92109
</project>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.baeldung.caching.redis;
22

3+
import static org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
4+
5+
import java.time.Duration;
6+
37
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
48
import org.springframework.context.annotation.Bean;
59
import org.springframework.context.annotation.Configuration;
610
import org.springframework.data.redis.cache.RedisCacheConfiguration;
711
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
812

9-
import java.time.Duration;
10-
11-
import static org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
12-
1313
@Configuration
1414
public class CacheConfig {
1515

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.baeldung.caching.redis;
22

3-
import lombok.AllArgsConstructor;
43
import org.springframework.web.bind.annotation.GetMapping;
54
import org.springframework.web.bind.annotation.PathVariable;
65
import org.springframework.web.bind.annotation.RestController;
76

7+
import lombok.AllArgsConstructor;
8+
89
@RestController
910
@AllArgsConstructor
1011
public class ItemController {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.baeldung.caching.redis;
22

3-
import lombok.AllArgsConstructor;
43
import org.springframework.cache.annotation.Cacheable;
54
import org.springframework.stereotype.Service;
65

6+
import lombok.AllArgsConstructor;
7+
78
@Service
89
@AllArgsConstructor
910
public class ItemService {
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.baeldung.caching.ttl.controller;
22

3-
import com.baeldung.caching.ttl.service.HotelService;
4-
import com.baeldung.caching.ttl.model.Hotel;
3+
import java.util.List;
4+
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.http.HttpStatus;
77
import org.springframework.web.bind.annotation.GetMapping;
88
import org.springframework.web.bind.annotation.RequestMapping;
99
import org.springframework.web.bind.annotation.ResponseStatus;
1010
import org.springframework.web.bind.annotation.RestController;
1111

12-
import java.util.List;
12+
import com.baeldung.caching.ttl.model.Hotel;
13+
import com.baeldung.caching.ttl.service.HotelService;
1314

1415
@RestController
1516
@RequestMapping("/hotel")
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.baeldung.caching.ttl.exception;
22

3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
36
import org.springframework.http.HttpStatus;
47
import org.springframework.http.ResponseEntity;
58
import org.springframework.web.bind.annotation.ControllerAdvice;
69
import org.springframework.web.bind.annotation.ExceptionHandler;
710
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
811

9-
import java.util.LinkedHashMap;
10-
import java.util.Map;
11-
1212
@ControllerAdvice
1313
public class ControllerAdvisor extends ResponseEntityExceptionHandler {
1414

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.baeldung.caching.ttl.repository;
22

3-
import com.baeldung.caching.ttl.model.City;
43
import org.springframework.data.jpa.repository.JpaRepository;
54

5+
import com.baeldung.caching.ttl.model.City;
6+
67
public interface CityRepository extends JpaRepository<City, Long> {}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.baeldung.caching.ttl.repository;
22

3-
import com.baeldung.caching.ttl.model.Hotel;
3+
import java.util.List;
4+
45
import org.springframework.data.jpa.repository.JpaRepository;
56

6-
import java.util.List;
7-
import java.util.Optional;
7+
import com.baeldung.caching.ttl.model.Hotel;
88

99
public interface HotelRepository extends JpaRepository<Hotel, Long> {
1010

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.baeldung.caching.ttl.service;
22

3-
import com.baeldung.caching.ttl.repository.HotelRepository;
4-
import com.baeldung.caching.ttl.model.Hotel;
3+
import java.util.List;
4+
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77
import org.springframework.cache.annotation.CacheEvict;
88
import org.springframework.cache.annotation.Cacheable;
99
import org.springframework.scheduling.annotation.Scheduled;
1010
import org.springframework.stereotype.Service;
1111

12-
import java.util.List;
12+
import com.baeldung.caching.ttl.model.Hotel;
13+
import com.baeldung.caching.ttl.repository.HotelRepository;
1314

1415
@Service
1516
public class HotelService {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.baeldung.caching.ttl.service;
22

3+
import static java.util.Arrays.asList;
4+
35
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer;
46
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
57
import org.springframework.stereotype.Component;
68

7-
import static java.util.Arrays.asList;
8-
99
@Component
1010
public class SpringCacheCustomizer implements CacheManagerCustomizer<ConcurrentMapCacheManager> {
1111

0 commit comments

Comments
 (0)