Skip to content

Commit e22750f

Browse files
fscorromaibin
authored andcommitted
Spring cache: custom key generator (eugenp#4166)
1 parent 258d5c0 commit e22750f

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}

spring-mvc-java/src/main/java/com/baeldung/model/Book.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

0 commit comments

Comments
 (0)