spring-cache

spring cache expire using bean property

跟風遠走 提交于 2019-12-13 18:18:36
问题 Is there a way of indicating to expire/evict the cache object based on a property of the POJO cached. In below code, it caches Foo instance. Foo class has a expiresIn property class Foo { Date expiresIn; } I want to hint to spring to expire the cache based on the value of expiresIn property of cached element. Is this feasible? @Cacheable("my-cache-key") Foo getCachedToken(String userName, String password) throws AuthException My Cache.xml is below: <cache:annotation-driven/> <bean id=

Spring caching works only sometimes

我的梦境 提交于 2019-12-13 17:28:31
问题 I have a Spring controller and want to cache the response. When I move the @Cacheable annotation from the getBooks to the doGetBooks method, the caching will stop. Once I move it back to the getBooks method caching works again. Why is that and how can I fix it? This will cache the public method response @GetMapping @Cacheable(value = "cache", key = "{ #root.methodName }") public Books getBooks(@RequestHeader(value = "user-agent", required = false) String userAgent) throws Exception { if(valid

What are the best Cache practices in ehcache or spring cache for spring MVC?

偶尔善良 提交于 2019-12-13 11:53:56
问题 Planning to implement cache mechanism for static data in spring web based application, can any one explain which is the best and how it works? EhCache Spring Cache 回答1: Disclaimer : I am a Terracotta / Software AG employee, the maintainers of Ehcache Ehcache is a JVM caching library, famous for being used as the default 2nd level cache for the Hibernate ORM Spring cache was introduced in Spring 3.1, and brought annotations such as @CachePut to define uses of caches in a Spring application; by

Changing cache manager for child method

蹲街弑〆低调 提交于 2019-12-13 06:34:06
问题 @CacheConfig(cacheManager = ACacheManager, cacheNames = ACache) class A { @Cacheable(key = "#user.userName", cacheManager="ACacheManager", cacheNames= {"ACache"}) methodA (User user) { } } @CacheConfig(cacheManager = BCacheManager, cacheNames = BCache) class B extends A { @Cacheable(key = "#user.userName", cacheManager="BCacheManager", cacheNames= {"BCache"}) @Override methodA (User user) { } } A a = new A(); a.methodA(); Result should save in ACacheManager's cache ACache A b = new B(); b

Spring 4 JDBC - How to load DB Properties and optimize (using Cache or DB Connection Pool)

半腔热情 提交于 2019-12-13 03:43:53
问题 Am maintaining a codebase which was written in Spring MVC 4.3.9.RELEASE (not Spring Boot)... Under src/main/resources: There are two different database configuration files: sampledb.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org

set expire key at specific time when using Spring caching with Redis

萝らか妹 提交于 2019-12-12 12:18:28
问题 I am spring caching abstraction annotations to apply caching to my service methods. Since I am using Redis as the cache store, I want to use the option of expiring cache at a specific time, since that is supported by Redis. expireat command in redis can be used to set expire time at a future time. I am not sure how I can do that for the keys which are part of my cache when using RedisCache. I tried to customize RedisCacheManager by creating a bean of it. I see there is a getNativeCache()

@cacheEvict not working while calling the method from different controller

岁酱吖の 提交于 2019-12-12 01:53:15
问题 In spring application we have two controllers i.e. controller1, controller2 and one services i.e. service1 i want to use method caching and for that i have configured spring cache. i am caching method named method1 in service1 with @Cacheable(value = "cache1") and for removing cache i am using @CacheEvict(value = "cache1", allEntries = true) on another method named method2 in service1. so caching works fine but evicting not working as i want. so if i call method1 (cachable method) from

Looking for Matched CachePublicMetrics in spring-boot 2.1.9.release

别等时光非礼了梦想. 提交于 2019-12-11 16:54:08
问题 I am using following classes in one of controllers of (spring-boot.1.5.12 release) I am unable to find matching classes in spring 2.1.9 release. The following is the code snippet import org.springframework.boot.actuate.endpoint.CachePublicMetrics; import org.springframework.boot.actuate.metrics.Metric; public class CachingController extends CloudRestTemplate { @Autowired private CachePublicMetrics metrics; public @ResponseBody Map<String, Object> getData(@Pattern(regexp=Constants.STRING_VALID

Ehcache with Spring Cache assigns wrong key

爷,独闯天下 提交于 2019-12-11 05:54:19
问题 I've got a method in UserService: @Cacheable(value="user", key="#p0") public User find(String name) { return userRepository.findOneByName(name); } And it caches. But then I try to get all keys from 'user' cache: CacheManager cacheManager = CacheManager.getInstance(); cacheManager.getCache("user").getKeys().forEach(o -> log.debug(o.toString())); Output: com.cache.domain.User#1 Instead, for example, 'John Doe'. 回答1: See the Javadoc of getKeys Returns a list of all elements in the cache, whether

Method annotated with @Cacheable not getting intercepted

假如想象 提交于 2019-12-11 05:12:39
问题 I am new to Spring caching. I am using spring-boot-starter-1.4.0.RELEASE in my maven pom. As far as I understand the documentation, if I take something like this: @Configuration @EnableCaching public class TestApplication { @Bean public CacheManager cacheManager() { // configure and return an implementation of Spring's CacheManager SPI SimpleCacheManager cacheManager = new SimpleCacheManager(); cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default"))); return cacheManager; }