Spring Cache Custom KeyGenerator in module spring-all (#4196)
This commit is contained in:
parent
daafd33aac
commit
1d0a6248e5
|
@ -0,0 +1,30 @@
|
|||
package org.baeldung.caching.config;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.cache.support.SimpleCacheManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@EnableCaching
|
||||
@Configuration
|
||||
public class ApplicationCacheConfig {
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
SimpleCacheManager cacheManager = new SimpleCacheManager();
|
||||
Cache booksCache = new ConcurrentMapCache("books");
|
||||
cacheManager.setCaches(Arrays.asList(booksCache));
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
@Bean("customKeyGenerator")
|
||||
public KeyGenerator keyGenerator() {
|
||||
return new CustomKeyGenerator();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package org.baeldung.caching.config;
|
||||
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class CustomKeyGenerator implements KeyGenerator {
|
||||
|
||||
public Object generate(Object target, Method method, Object... params) {
|
||||
return target.getClass().getSimpleName() + "_" + method.getName() + "_"
|
||||
+ StringUtils.arrayToDelimitedString(params, "_");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package org.baeldung.caching.example;
|
||||
|
||||
import org.baeldung.model.Book;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class BookService {
|
||||
|
||||
@Cacheable(value="books", keyGenerator="customKeyGenerator")
|
||||
public List<Book> getBooks() {
|
||||
List<Book> books = new ArrayList<Book>();
|
||||
books.add(new Book(1, "The Counterfeiters", "André Gide"));
|
||||
books.add(new Book(2, "Peer Gynt and Hedda Gabler", "Henrik Ibsen"));
|
||||
return books;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package org.baeldung.model;
|
||||
|
||||
public class Book {
|
||||
|
||||
private int id;
|
||||
private String author;
|
||||
private String title;
|
||||
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Book(int id, String author, String title) {
|
||||
this.id = id;
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue