spring-cache

Spring Boot - How to disable @Cachable during development?

元气小坏坏 提交于 2019-11-27 14:42:46
问题 I'm looking for 2 things: How to disable all caching during development with Spring boot "dev" profile. There doesn't seam to be a general setting to turn it all off in application.properties. What's the easiest way? How to disable caching for a specific method? I tried to use SpEl like this: @Cacheable(value = "complex-calc", condition = "#{${spring.profiles.active} != 'dev'}") public String someBigCalculation(String input){ ... } But I can get it to work. There are a couple of questions on

How to load @Cache on startup in spring?

蓝咒 提交于 2019-11-27 12:53:37
I'm using spring-cache to improve database queries, which works fine as follows: @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager("books"); } @Cacheable("books") public Book getByIsbn(String isbn) { return dao.findByIsbn(isbn); } But now I want to prepopulate the full book-cache on startup. Which means I want to call dao.findAll() and put all values into the cache. This routine shall than only be scheduled periodically. But how can I explicit populate a cache when using @Cacheable ? Just use the cache as before, add a scheduler to update cache, code snippet is

Spring Webflux and @Cacheable - proper way of caching result of Mono / Flux type

馋奶兔 提交于 2019-11-27 12:30:57
问题 I'm learning Spring WebFlux and during writing a sample application I found a concern related to Reactive types (Mono/Flux) combined with Spring Cache. Consider the following code-snippet (in Kotlin): @Repository interface TaskRepository : ReactiveMongoRepository<Task, String> @Service class TaskService(val taskRepository: TaskRepository) { @Cacheable("tasks") fun get(id: String): Mono<Task> = taskRepository.findById(id) } Is this valid and safe way of caching method calls returning Mono or

How to test Spring's declarative caching support on Spring Data repositories?

末鹿安然 提交于 2019-11-27 07:06:26
I have developed a Spring Data repository, MemberRepository interface, that extends org.springframework.data.jpa.repository.JpaRepository . MemberRepository has a method: @Cacheable(CacheConfiguration.DATABASE_CACHE_NAME) Member findByEmail(String email); The result is cached by Spring cache abstraction (backed by a ConcurrentMapCache ). The issue I have is that I want to write an integration test (against hsqldb) that asserts that the result is retrieved from db the first time and from cache the second time . I initially thought of mocking the jpa infrastructure (entity manager, etc.) and

Spring cache @Cacheable method ignored when called from within the same class

拟墨画扇 提交于 2019-11-27 03:34:38
I'm trying to call a @Cacheable method from within the same class: @Cacheable(value = "defaultCache", key = "#id") public Person findPerson(int id) { return getSession().getPerson(id); } public List<Person> findPersons(int[] ids) { List<Person> list = new ArrayList<Person>(); for (int id : ids) { list.add(findPerson(id)); } return list; } and hoping that the results from findPersons are cached as well, but the @Cacheable annotation is ignored, and findPerson method got executed everytime. Am I doing something wrong here, or this is intended? Biju Kunjummen This is because of the way proxies

What strategies exist for using Spring Cache on methods that take an array or collection parameter?

谁说胖子不能爱 提交于 2019-11-26 17:06:04
问题 I want to use Spring's Cache abstraction to annotate methods as @Cacheable. However, some methods are designed to take an array or collection of parameters and return a collection. For example, consider this method to find entites: public Collection<Entity> getEntities(Collection<Long> ids) Semantically, I need to cache Entity objects individually (keyed by id), not based on the collection of IDs as a whole. Similar to what this question is asking about. Simple Spring Memcached supports what

How to load @Cache on startup in spring?

ⅰ亾dé卋堺 提交于 2019-11-26 13:45:49
问题 I'm using spring-cache to improve database queries, which works fine as follows: @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager("books"); } @Cacheable("books") public Book getByIsbn(String isbn) { return dao.findByIsbn(isbn); } But now I want to prepopulate the full book-cache on startup. Which means I want to call dao.findAll() and put all values into the cache. This routine shall than only be scheduled periodically. But how can I explicit populate a cache

Spring cache @Cacheable method ignored when called from within the same class

淺唱寂寞╮ 提交于 2019-11-26 10:34:28
问题 I\'m trying to call a @Cacheable method from within the same class: @Cacheable(value = \"defaultCache\", key = \"#id\") public Person findPerson(int id) { return getSession().getPerson(id); } public List<Person> findPersons(int[] ids) { List<Person> list = new ArrayList<Person>(); for (int id : ids) { list.add(findPerson(id)); } return list; } and hoping that the results from findPersons are cached as well, but the @Cacheable annotation is ignored, and findPerson method got executed everytime

How to test Spring&#39;s declarative caching support on Spring Data repositories?

五迷三道 提交于 2019-11-26 09:27:15
问题 I have developed a Spring Data repository, MemberRepository interface, that extends org.springframework.data.jpa.repository.JpaRepository . MemberRepository has a method: @Cacheable(CacheConfiguration.DATABASE_CACHE_NAME) Member findByEmail(String email); The result is cached by Spring cache abstraction (backed by a ConcurrentMapCache ). The issue I have is that I want to write an integration test (against hsqldb) that asserts that the result is retrieved from db the first time and from cache