spring-cache

Spring @Cacheable annotation for same method in different service

佐手、 提交于 2020-01-16 13:20:13
问题 I have implemented the standard redis caching template in a Spring boot application as per the following article: What I have is two different services that get a list of objects: @RequestMapping("/admin/test/list") public String testCache() { List<Cocktail> cocktails = cocktailsService.list(); List<Ingredient> ingredients = ingredientsService.list(); return "index"; } Note: that the method name and signature is the same (i.e. list() ) but they both have different cache names as such: //

Handling Java 8 Optional with Spring cache

心不动则不痛 提交于 2020-01-14 08:58:32
问题 Considering a service class that can insert and retrieve objects and use Spring cache abstraction, how can I annotate methods in a way that an Optional is returned? class MyServiceImpl implements MyService { private static final String CACHE_NAME = "itemCache"; @Override @Cacheable(CACHE_NAME) public Optional<Item> findById(Long id) { // access the repository to retrieve the item } @Override @CachePut(cacheNames = CACHE_NAME, key = "#item.id") public Item insertItem(Item item) { ... } } In

Putting all returned elements into a Spring-Boot cache using annotations

╄→гoц情女王★ 提交于 2020-01-10 03:08:18
问题 Using spring-boot and its caching mechanism, is it possible to automatically store all entities returned as a collection into the cache one by one? For instance picture the following Repository method: @Query("...") List<Foo> findFooByBar(Bar bar); I'd like to insert these in a Spring Cache, one by one, meaning there would be N insertions (one for each element in the list) rather than just one (the whole list). Example: @Query("...") @CachePut(value = "foos", key = "result.each.id") List<Foo>

LazyInitializationException when trying to access detached objects left around in Redis by RedisCacheManager

江枫思渺然 提交于 2020-01-06 13:59:39
问题 I use Spring data Redis in order to cache serialized JPA entities in Redis using org.springframework.data.redis.cache.RedisCacheManager Here is the method: @Override @Cacheable(value = MapCacheConfiguration.DATABASE_CACHE_NAME, key = "#root.method.name") public Curriculum findCurriculumByMemberId(Long memberId) { return curriculumRepository.findCurriculumByMemberId(memberId); } Unfortunately, upon restart of my boot application, the entities are still cached in Redis and I get a org.hibernate

LazyInitializationException when trying to access detached objects left around in Redis by RedisCacheManager

懵懂的女人 提交于 2020-01-06 13:59:12
问题 I use Spring data Redis in order to cache serialized JPA entities in Redis using org.springframework.data.redis.cache.RedisCacheManager Here is the method: @Override @Cacheable(value = MapCacheConfiguration.DATABASE_CACHE_NAME, key = "#root.method.name") public Curriculum findCurriculumByMemberId(Long memberId) { return curriculumRepository.findCurriculumByMemberId(memberId); } Unfortunately, upon restart of my boot application, the entities are still cached in Redis and I get a org.hibernate

Spring Caching - ignore parameter for key

↘锁芯ラ 提交于 2020-01-03 15:56:12
问题 I want to cache the results of a simple getter that has an optional parameter (user-agent in the example below). How can I instruct the key to be created without taking into consideration the optional user-agent parameter? @Cacheable(value="bookCache") public Book getBooks(@RequestHeader(value = "user-agent", required = false) String userAgent) ... 回答1: One could customize how a key for a certain cache-object is created by providing a custom KeyGenerator. Here's what it could look: @Service

spring cache with custom cacheResolver

▼魔方 西西 提交于 2020-01-02 23:15:08
问题 I wand to have dynamic cache names, and spring 4.1 allows that Since Spring 4.1, the value attribute of the cache annotations are no longer mandatory since this particular information can be provided by the CacheResolver regardless of the content of the annotation. Notice how I paranoidly set cacheResolver on all possible levels: @Cacheable(cacheResolver = "defaultCacheResolver") @CacheConfig(cacheResolver = "defaultCacheResolver") public interface GatewayRepository extends CrudRepository

spring @Cacheable with Ehcache, spel find null for valid object

自古美人都是妖i 提交于 2019-12-28 07:05:51
问题 I have a similar problem which but sometimes it works. The error described only happens once in a while. I am using spring 3.2.5 and ehcache 2.6.5. Exception trace: org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'applicationID' cannot be found on null at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) at org.springframework.expression.spel.ast.PropertyOrFieldReference

spring @Cacheable with Ehcache, spel find null for valid object

心不动则不痛 提交于 2019-12-28 07:04:06
问题 I have a similar problem which but sometimes it works. The error described only happens once in a while. I am using spring 3.2.5 and ehcache 2.6.5. Exception trace: org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'applicationID' cannot be found on null at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) at org.springframework.expression.spel.ast.PropertyOrFieldReference

Concurrent calls to cached method

假装没事ソ 提交于 2019-12-25 07:05:12
问题 I'm using Spring cache abstraction with Guava cache. I have a method with @Cacheable annotation and parameter (that serves as a cache key) to put values into the cache. But this method is accessed in a multi threaded env so there are multiple concurrent calls to the method with the same parameter value. So that means the same logic that creates the value to be cached is done for the same cache key multiple times and put into the cache multiple times concurrently. It'd be much more efficient