diff --git a/spring-all/src/main/java/org/baeldung/caching/config/ApplicationCacheConfig.java b/spring-all/src/main/java/org/baeldung/caching/config/ApplicationCacheConfig.java new file mode 100644 index 0000000000..8bf23de2cc --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/config/ApplicationCacheConfig.java @@ -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(); + } +} diff --git a/spring-all/src/main/java/org/baeldung/caching/config/CustomKeyGenerator.java b/spring-all/src/main/java/org/baeldung/caching/config/CustomKeyGenerator.java new file mode 100644 index 0000000000..c1da9493e0 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/config/CustomKeyGenerator.java @@ -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, "_"); + } +} diff --git a/spring-all/src/main/java/org/baeldung/caching/example/BookService.java b/spring-all/src/main/java/org/baeldung/caching/example/BookService.java new file mode 100644 index 0000000000..26118d61de --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/example/BookService.java @@ -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 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-all/src/main/java/org/baeldung/model/Book.java b/spring-all/src/main/java/org/baeldung/model/Book.java new file mode 100644 index 0000000000..9305ce9653 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/model/Book.java @@ -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; + } +}