spring-cache

@Cacheable doesn't intercept the method, cache is always empty

感情迁移 提交于 2019-12-11 03:00:21
问题 I have a method as following: @Cacheable(value = "SAMPLE") public List<SomeObj> find() { // Method that initiates and returns the List<SomeObj> and takes around 2-3 seconds, does some logging too } And I am enabling caching in one of my configuration classes: @EnableCaching @Configuration public SomeConf extends CachingConfigurerSupport { // Here I also initialize my classes with @Cacheable annotation @Bean @Override public CacheManager cacheManager() { SimpleCacheManager cacheManager = new

How to create a Jcache in Spring Java config?

感情迁移 提交于 2019-12-10 14:21:04
问题 I have a problem setting up a jcache with spring cache abstraction. @Configuration @EnableCaching public class CacheConfig { @Bean(name = "caffeineCachingProvider") public CachingProvider caffeineCachingProvider() { return new CaffeineCachingProvider(); } @Bean(name = "caffeineCacheManager") public JCacheCacheManager getSpringCacheManager() { CacheManager cacheManager = caffeineCachingProvider().getCacheManager(); CaffeineConfiguration<String, List<Product>> caffeineConfiguration = new

How to have spring cache store the ResponseBody and not the intermediary object

白昼怎懂夜的黑 提交于 2019-12-10 01:42:45
问题 I use spring cache with this method which returns the queried value as JSON: @RequestMapping("/getById") @ResponseBody @Cacheable public HugeValue getHugeValueFromSlowFoo( @RequestParam(value = "id", defaultValue = "") String id ) { return Foo.getById( id ); } This works fine and the HugeValue-object is stored in the cache (Hazelcast in this case). I want to further improve this because the time taken to create JSON from the HugeValue is quite high. Can I tell spring cache to cache the JSON

How to cache the list of entires based on its primary key using spring caching

北慕城南 提交于 2019-12-09 00:58:29
I want to cache the response of the repository class which has the following methods: @Cacheable(cacheNames = "books", key="#id") Book findById(Long Id); @CacheEvict(cacheNames = "books", key = "#id") void deleteById(Long id); @Cacheable(cacheNames = "books", key="#book.id") Book save(Book book); @Cacheable("books") List<Book> findAll(); Except the findAll() method, others are working as expected. How to make findAll() to populate the books cache with book.id as key? John Blum You need to provide a customized CacheManager and Cache implementation for your caching provider (e.g. Ehcache, Redis

Spring Infinispan - Setting expiry time for cached objects

冷暖自知 提交于 2019-12-08 13:50:37
问题 We are currently using below. It's quite old, but cannot upgrade to higher version now ` <dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-spring3</artifactId> <version>6.4.0.Final-redhat-4</version> </dependency> <dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-client-hotrod</artifactId> <version>6.4.0.Final-redhat-4</version> </dependency> ` We have modified our code from something with a direct JDG implementation (as shown below) to

Unable to disable @Cacheable with YAML Spring Profile

一个人想着一个人 提交于 2019-12-08 13:18:46
问题 I created a cache called "mycache" which is applied to a method in my Service like: @Cacheable(value = "mycache") public String getValue(String something) { ...breakpoint here... } I also have the following in my application.yml : --- spring: profiles: dev cache: type: NONE (NOTE: I also tried: none - all lowercase - and that also did not work) And in my class I have a field that shows me it is the "dev" profile: @Value("${spring.profiles.active}") private String activeProfiles; When I call

How to cache the list of entires based on its primary key using spring caching

北慕城南 提交于 2019-12-08 06:07:02
问题 I want to cache the response of the repository class which has the following methods: @Cacheable(cacheNames = "books", key="#id") Book findById(Long Id); @CacheEvict(cacheNames = "books", key = "#id") void deleteById(Long id); @Cacheable(cacheNames = "books", key="#book.id") Book save(Book book); @Cacheable("books") List<Book> findAll(); Except the findAll() method, others are working as expected. How to make findAll() to populate the books cache with book.id as key? 回答1: You need to provide

How to cache Spring Data JPA Projections

有些话、适合烂在心里 提交于 2019-12-08 04:17:59
问题 I am running Spring Boot 1.5.1 with Spring Data JPA repositories. I have added a method to my User repository that makes use of JPA projections(UserProfile) which works great. I now wish to cache the results of that method in my Service layer which should return a result of type Page< UserProfile > as shown The JPA Projection. public interface UserProfile extends Serializable { long getId(); @Value("#{target.firstname} #{target.othernames}") String getFullName(); String getFirstname(); String

after upgrade to Spring Boot 2, how to expose cache metrics to prometheus?

独自空忆成欢 提交于 2019-12-07 06:40:13
问题 I recently upgraded a spring boot application from 1.5 to 2.0.1. I also migrated the prometheus integration to the new actuator approach using micrometer. Most things work now - including some custom counters and gauges. I noted the new prometheus endpoint /actuator/prometheus does no longer publish the spring cache metrics (size and hit ratio). The only thing I could find was this issue and its related commit. Still I can't get cache metrics on the prometheus export. I tried settings some

Spring @Cacheable default ttl

北战南征 提交于 2019-12-06 07:58:00
问题 I generally use the @Cacheable with a cache config in my spring-boot app and set specific TTL (time to live) for each cache. I recently inherited a spring boot app that uses @Cacheable without explicitly stating a cache manager and ttl. I will be changing it to be explicit. But I am not able to find out what are the defaults when there is nothing explicit. I did look at the docs but found nothing there 回答1: With spring boot, I was able to achieve success with this: First, you need to add