作者:寒月繁华叶落尽 | 来源:互联网 | 2023-10-11 20:05
Ihaveconfiguredmycacheasfollows:我已按如下方式配置缓存:@Configuration@EnableCachingpublicclassCach
I have configured my cache as follows:
我已按如下方式配置缓存:
@Configuration
@EnableCaching
public class CacheConfig {
@Bean(name = "caffeineCachingProvider")
public CachingProvider caffeineCachingProvider() {
return Caching.getCachingProvider("com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider");
}
@Bean(name = "caffeineCacheManager")
public JCacheCacheManager getSpringCacheManager() {
CacheManager cacheManager = caffeineCachingProvider().getCacheManager();
CaffeineConfiguration> caffeineCOnfiguration= new CaffeineConfiguration<>();
caffeineConfiguration.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new AccessedExpiryPolicy(new Duration(TimeUnit.MINUTES, 60))));
caffeineConfiguration.setCopierFactory(Copier::identity);
cacheManager.createCache("informerCache", caffeineConfiguration);
return new JCacheCacheManager(cacheManager);
}
}
Also I have the @Service
that uses it in following way:
我也有@Service以下列方式使用它:
@Service
public class InformerService {
@CacheResult(cacheName = "informerCache")
public List getProducts(@CacheKey String category, @CacheKey String countrySign, @CacheKey long townId) throws Exception {
Thread.sleep(5000);
// do some work
}
}
So I have the next behavior.
所以我有下一个行为。
- When I'm calling the service method first time it takes 5 seconds then doing some work as expected.
- 当我第一次调用服务方法时需要5秒钟,然后按预期进行一些工作。
- Calling method the second time with same parameters - > caching works -> returns result immediately
- 第二次使用相同参数调用方法 - >缓存工作 - >立即返回结果
- Calling the third time with same parameters again results in
Thread.sleep
- 再次使用相同参数调用第三次会导致Thread.sleep
And all over again.
并且一遍又一遍。
How to solve this ? Is that the issue about proxying ? What did I miss ?
怎么解决这个?这是关于代理的问题吗?我错过了什么 ?
1 个解决方案