spring-cache

Spring cache logging on @Cacheable hit

廉价感情. 提交于 2020-08-21 08:14:29
问题 Currently I am working with a Spring Cache and the @Cacheable / @CacheEvict annotations. I would like to get some sort of a console log statement like "INFO: i got those values from the cache, NOT from the host. awesome" Is there a clean and easy way to do this? We are using slf4j apparently btw, if that is of any interest. 回答1: Spring itself logs some of its Caching Abstractions behaviors under the org.springframework.cache logger in trace level. So, if you append logs under the org

Spring cache logging on @Cacheable hit

你说的曾经没有我的故事 提交于 2020-08-21 08:14:04
问题 Currently I am working with a Spring Cache and the @Cacheable / @CacheEvict annotations. I would like to get some sort of a console log statement like "INFO: i got those values from the cache, NOT from the host. awesome" Is there a clean and easy way to do this? We are using slf4j apparently btw, if that is of any interest. 回答1: Spring itself logs some of its Caching Abstractions behaviors under the org.springframework.cache logger in trace level. So, if you append logs under the org

Spring @Cacheable and @Async annotation

谁说胖子不能爱 提交于 2020-08-06 09:15:10
问题 I have the need to cache some the results of some asynchronous computations. In detail, to overcome this issue, I am trying to use Spring 4.3 cache and asynchronous computation features. As an example, let's take the following code: @Service class AsyncService { @Async @Cacheable("users") CompletableFuture<User> findById(String usedId) { // Some code that retrieves the user relative to id userId return CompletableFuture.completedFuture(user); } } Is it possible? I mean, will the caching

Spring cache with instance variable and parameter as key

时间秒杀一切 提交于 2020-06-17 05:38:08
问题 I am using ehcache for caching the method results. The key has to be a combination of both member object and method's parameter. My class looks something like: Class A { private B b; @Cacheable(value="someCache",key="some key based on B and C") public Result getResult(C c){ ...... } I need the key to be based on B and C. I referred https://code.google.com/p/ehcache-spring-annotations/issues/detail?id=69 but they did not specify how to include the method parameter in the key generation. Could

How to use spring boot 2 and ehcache 3 without xml?

徘徊边缘 提交于 2020-06-12 07:50:42
问题 For now I have following config: @Configuration @EnableCaching public class EhcacheConfig { @Bean public CacheManager cacheManager() throws URISyntaxException { return new JCacheCacheManager(Caching.getCachingProvider().getCacheManager( getClass().getResource("/ehcache.xml").toURI(), getClass().getClassLoader() )); } } It refers to the following XML: <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ehcache.org/v3" xmlns:jsr107="http://www.ehcache.org/v3/jsr107"

Spring cache all elements in list separately

邮差的信 提交于 2020-05-10 03:07:51
问题 I'm trying to add caching to a CRUD app, I started doing something like this: @Cacheable("users") List<User> list() { return userRepository.findAll() } @CachePut(value = "users", key = "#user.id") void create(User user) { userRepository.create(user) } @CachePut(value = "users", key = "#user.id") void update(User user) { userRepository.update(user) } @CacheEvict(value = "users", key = "#user.id") void delete(User user) { userRepository.delete(user) } The problem I have is that I would like

Spring cache all elements in list separately

左心房为你撑大大i 提交于 2020-05-10 03:06:32
问题 I'm trying to add caching to a CRUD app, I started doing something like this: @Cacheable("users") List<User> list() { return userRepository.findAll() } @CachePut(value = "users", key = "#user.id") void create(User user) { userRepository.create(user) } @CachePut(value = "users", key = "#user.id") void update(User user) { userRepository.update(user) } @CacheEvict(value = "users", key = "#user.id") void delete(User user) { userRepository.delete(user) } The problem I have is that I would like

set value to Cacheable annotation from property file

别等时光非礼了梦想. 提交于 2020-01-30 11:15:11
问题 I'm using Spring Cacheable annotation and at the moment I'm struggling with a way of adding the cache name from property file. I tried: @Cacheable("${some.cache.name}") and @Cacheable("#{'${some.cache.name}'}") 回答1: There is a SPI to do that that is much more powerful than just using SpEL. You can implement CacheResolver and resolve cache instance(s) at runtime. You could use the annotated type or any name that is provided via the annotation. You can specify the CacheResolver per annotation,

Schedule Spring cache eviction?

╄→гoц情女王★ 提交于 2020-01-22 14:57:05
问题 Is it possible to schedule spring cache eviction to everyday at midnight? I've read Springs Cache Docs and found nothing about scheduled cache eviction. I need to evict cache daily and recache it in case there were some changes outside my application. 回答1: Try to use @Scheduled Example: @Scheduled(fixedRate = ONE_DAY) @CacheEvict(value = { CACHE_NAME }) public void clearCache() { } You can also use cron expression with @Scheduled. 回答2: If you use @Cacheable on methods with parameters, you

spring-data-redis with @Cachable & @Query gives a java.io.NotSerializableException on DefaultMethodInvokingMethodInterceptor

独自空忆成欢 提交于 2020-01-17 07:14:10
问题 When using redis as a cache and @Query on a repository method, redis JdkSerializationRedisSerializer , is unable to serialize a projection. Put a github project with tests; https://github.com/varunmehta/spring-cache-error Actual Entity @Entity public class User { @Id private Integer id; private String firstname; private String lastname; private String email; private String address1; private String address2; private String city; private String state; private String country; // ... setter