天道酬勤,学无止境

spring-cache

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

问题 我正在维护一个用 Spring MVC 4.3.9.RELEASE(不是 Spring Boot)编写的代码库...... 在 src/main/resources 下: 有两个不同的数据库配置文件: 示例数据库.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/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- Initialization for

2022-05-17 05:27:04    分类:技术分享    spring   spring-jdbc   spring-cache

spring aop that intercepts org.springframework.cache.interceptor.CacheInterceptor#invoke

问题 我尝试了以下代码,但它不起作用: @Component @Aspect @Order(Integer.MAX_VALUE) public class CacheAspect { @Around("execution(public * org.springframework.cache.interceptor.CacheInterceptor.invoke(..))") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); //CLASS_CACHE.set(signature.getReturnType()); return joinPoint.proceed(); } } PS我确信CacheInterceptor是一个弹簧管理的 bean。 回答1 经过一些实验,我发现用用户定义的替换CacheInterceptor内置的弹簧可以解决我的问题。 这是代码,以防有人有类似要求。 @Configuration @EnableCaching @Profile("test") public class CacheConfig { @Bean

2022-05-12 12:31:11    分类:技术分享    java   spring   aop   aspect   spring-cache

Spring : Spring-cache not working

问题 我正在开发一个 Spring-MVC 应用程序。 在分析器通过后端后,我注意到 getCurrentlyAuthenticatedUser() 是一个热点。 由于我想到使用缓存的原因。 不幸的是,它不起作用。 用户登录后,即使用户已登录,我也会得到空用户。出了什么问题。 当然,当我从 XML 中删除 @Cacheable 注释和配置时,一切正常。 你能帮忙的话,我会很高兴。 PersonServiceImpl : @Service @Transactional public class PersonServiceImpl implements PersonService { private final PersonDAO personDAO; @Autowired public PersonServiceImpl(PersonDAO personDAO) { this.personDAO = personDAO; } @Override @Cacheable(value = "person", condition = "#person == null") public Person getCurrentlyAuthenticatedUser() { Authentication authentication = SecurityContextHolder.getContext

2022-05-11 16:04:09    分类:技术分享    java   spring   spring-mvc   caching   spring-cache

Spring Cache: Evict multiple caches

问题 回答1 你可以这样做: @Caching(evict = { @CacheEvict("primary"), @CacheEvict(value = "secondary", key = "#p0") }) 查看参考资料了解详情 回答2 @CacheEvict(value = { "cache1", "cache2" }, allEntries = true)

2022-05-08 15:21:20    分类:技术分享    java   spring   spring-cache

caffeine Cache is not evicted after expireAfterAccess/expireAfterWrite

My configuration: @Bean public CaffeineCacheManager cacheManager() { return new CaffeineCacheManager(); } @Bean public CaffeineCache testCache() { return new CaffeineCache("test_cache", Caffeine.newBuilder() .maximumSize(10000) .expireAfterAccess(30, TimeUnit.SECONDS) .expireAfterWrite(30, TimeUnit.SECONDS) .recordStats() .build()); } test code:(read cache 3 times in a row with 45 seconds pause between reads) static int value = 1; ... Cache testCache = cacheManager.getCache("test_cache"); System.out.println("read " + testCache.get("myKey", () -> value++)); try { Thread.sleep(45000); } catch

2022-05-03 02:39:29    分类:问答    java   spring   caching   spring-cache   caffeine-cache

Spring @Cacheable default TTL

问题 我通常在我的 spring-boot 应用程序中使用带有缓存配置的@Cacheable ,并为每个缓存设置特定的 TTL(生存时间)。 我最近继承了一个使用@Cacheable而没有明确说明缓存管理器和 ttl 的 Spring Boot 应用程序。 我会将其更改为明确的。 但是当没有明确的内容时,我无法找出默认值是什么。 我确实查看了文档,但在那里什么也没找到 回答1 Spring @Cacheable 没有任何可配置的选项来设置缓存的TTL ,尽管您可以使用 @CacheEvict 和 @Scheduled 构建它,如下所示: @CacheEvict(allEntries = true, cacheNames = { "cache_1", "cache_2" }) @Scheduled(fixedDelay = 30000) public void cacheEvict() { } 您可以在此处找到详细的解决方案/说明 - Setting TTL for @Cacheable – Spring。 回答2 使用弹簧靴,我能够通过以下方式取得成功: 首先,您需要将spring-boot-starter-data-redis工件添加到您的 POM 文件中。 <dependency> <groupId>org.springframework.boot</groupId>

2022-05-02 11:48:23    分类:技术分享    spring   spring-boot   caching   spring-cache

spring aop that intercepts org.springframework.cache.interceptor.CacheInterceptor#invoke

I have tried the following code , but it does not work: @Component @Aspect @Order(Integer.MAX_VALUE) public class CacheAspect { @Around("execution(public * org.springframework.cache.interceptor.CacheInterceptor.invoke(..))") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); //CLASS_CACHE.set(signature.getReturnType()); return joinPoint.proceed(); } } P.S. I am sure CacheInterceptor is a spring managed bean.

2022-05-01 16:18:52    分类:问答    java   spring   aop   aspect   spring-cache

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

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/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www

2022-04-29 14:08:48    分类:问答    spring   spring-jdbc   spring-cache

Spring Cache: Evict multiple caches

I'm using Spring Cache abstraction and I have multiple caches defined. Sometimes, when data changes, I want to evict more than one caches. Is there away to evict multiple cache using Spring's @CacheEvict annotation?

2022-04-29 03:49:34    分类:问答    java   spring   spring-cache

Spring : Spring-cache not working

I am working on a Spring-MVC application. After the profiler went through the backend, I noticed that getCurrentlyAuthenticatedUser() is an hotspot. For the reason I thought of using cache. Unfortunately it is not working. Once the user logs in, I am getting null user, even though the user is logged in. What is going wrong. Ofcourse when I remove the @Cacheable annotation and config from XML, everything works fine. Any help would be nice. PersonServiceImpl : @Service @Transactional public class PersonServiceImpl implements PersonService { private final PersonDAO personDAO; @Autowired public

2022-04-28 08:12:04    分类:问答    java   spring   spring-mvc   caching   spring-cache