File tree Expand file tree Collapse file tree
spring-mvc-java/src/main/java/com/baeldung Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .cache ;
2+
3+ import com .baeldung .model .Book ;
4+ import org .springframework .cache .annotation .Cacheable ;
5+ import org .springframework .stereotype .Component ;
6+
7+ import java .util .ArrayList ;
8+ import java .util .List ;
9+
10+ @ Component
11+ public class BookService {
12+
13+ @ Cacheable (value ="books" , keyGenerator ="customKeyGenerator" )
14+ public List <Book > getBooks () {
15+ List <Book > books = new ArrayList <Book >();
16+ books .add (new Book (1 , "The Counterfeiters" , "André Gide" ));
17+ books .add (new Book (2 , "Peer Gynt and Hedda Gabler" , "Henrik Ibsen" ));
18+ return books ;
19+ }
20+
21+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .cache ;
2+
3+ import org .springframework .cache .interceptor .KeyGenerator ;
4+ import org .springframework .util .StringUtils ;
5+
6+ import java .lang .reflect .Method ;
7+
8+ public class CustomKeyGenerator implements KeyGenerator {
9+
10+ public Object generate (Object target , Method method , Object ... params ) {
11+ return target .getClass ().getSimpleName () + "_" + method .getName () + "_"
12+ + StringUtils .arrayToDelimitedString (params , "_" );
13+ }
14+ }
Original file line number Diff line number Diff line change @@ -6,6 +6,15 @@ public class Book {
66 private String author ;
77 private String title ;
88
9+ public Book () {
10+ }
11+
12+ public Book (int id , String author , String title ) {
13+ this .id = id ;
14+ this .author = author ;
15+ this .title = title ;
16+ }
17+
918 public int getId () {
1019 return id ;
1120 }
Original file line number Diff line number Diff line change 1+ package com .baeldung .spring .web .config ;
2+
3+ import com .baeldung .cache .CustomKeyGenerator ;
4+ import org .springframework .cache .Cache ;
5+ import org .springframework .cache .CacheManager ;
6+ import org .springframework .cache .annotation .CachingConfigurerSupport ;
7+ import org .springframework .cache .annotation .EnableCaching ;
8+ import org .springframework .cache .concurrent .ConcurrentMapCache ;
9+ import org .springframework .cache .interceptor .KeyGenerator ;
10+ import org .springframework .cache .support .SimpleCacheManager ;
11+ import org .springframework .context .annotation .Bean ;
12+ import org .springframework .context .annotation .Configuration ;
13+
14+ import java .util .Arrays ;
15+
16+ @ EnableCaching
17+ @ Configuration
18+ public class ApplicationCacheConfig extends CachingConfigurerSupport {
19+
20+ @ Bean
21+ public CacheManager cacheManager () {
22+ SimpleCacheManager cacheManager = new SimpleCacheManager ();
23+ Cache booksCache = new ConcurrentMapCache ("books" );
24+ cacheManager .setCaches (Arrays .asList (booksCache ));
25+ return cacheManager ;
26+ }
27+
28+ @ Bean ("customKeyGenerator" )
29+ public KeyGenerator keyGenerator () {
30+ return new CustomKeyGenerator ();
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments