diff --git a/spring-mvc-java/src/main/java/com/baeldung/cache/BookService.java b/spring-mvc-java/src/main/java/com/baeldung/cache/BookService.java new file mode 100644 index 0000000000..91c37ce5b6 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/cache/BookService.java @@ -0,0 +1,21 @@ +package com.baeldung.cache; + +import com.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 getBooks() { + List books = new ArrayList(); + books.add(new Book(1, "The Counterfeiters", "André Gide")); + books.add(new Book(2, "Peer Gynt and Hedda Gabler", "Henrik Ibsen")); + return books; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/cache/CustomKeyGenerator.java b/spring-mvc-java/src/main/java/com/baeldung/cache/CustomKeyGenerator.java new file mode 100644 index 0000000000..2cb4bba95f --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/cache/CustomKeyGenerator.java @@ -0,0 +1,14 @@ +package com.baeldung.cache; + +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, "_"); + } +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/Book.java b/spring-mvc-java/src/main/java/com/baeldung/model/Book.java index b0cabe0125..bdfa1d835a 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/model/Book.java +++ b/spring-mvc-java/src/main/java/com/baeldung/model/Book.java @@ -6,6 +6,15 @@ public class Book { 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; } diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ApplicationCacheConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ApplicationCacheConfig.java new file mode 100644 index 0000000000..e78506deaa --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/ApplicationCacheConfig.java @@ -0,0 +1,32 @@ +package com.baeldung.spring.web.config; + +import com.baeldung.cache.CustomKeyGenerator; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.CachingConfigurerSupport; +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 extends CachingConfigurerSupport { + + @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(); + } +}