首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring缓存中的@Caching

spring缓存中的@Caching
EN

Stack Overflow用户
提问于 2015-03-10 16:49:51
回答 1查看 312关注 0票数 1

我习惯在页面列表中使用缓存。

我想要1~5页缓存。

所以,

代码语言:javascript
复制
@Cacheable(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, #pageIndex, #pagePerNumber)", condition = "#pageIndex < 5")

关键字如下:

代码语言:javascript
复制
'brandon@[EventChainServiceImpl]0/10'

'brandon@[EventChainServiceImpl]1/10'

'brandon@[EventChainServiceImpl]2/10'

所以,

代码语言:javascript
复制
@Caching(evict = {
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 0, 5)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 0, 10)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 0, 25)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 0, 50)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 1, 5)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 1, 10)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 1, 25)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 1, 50)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 2, 5)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 2, 10)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 2, 25)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 2, 50)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 3, 5)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 3, 10)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 3, 25)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 3, 50)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 4, 5)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 4, 10)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 4, 25)"),
    @CacheEvict(value = "cacheData", key = "T(com.insoft.pacific.util.CacheKeyGenerator).cacheKey(#root.targetClass, 4, 50)") })

我怎么才能简化这个过程呢?

EN

回答 1

Stack Overflow用户

发布于 2016-05-29 21:07:31

我认为你可以注入CacheManager和...,就像这样:

代码语言:javascript
复制
@Autowired
CacheManager cacheManager;
public Object doSomething() {
    Cache cache = cacheManager.getCache("yourCacheName");
    for (String key : yourKeyIterator) {
        cache.evict(key);
    }
}

我认为这对性能是有害的,因为需要多次调用cache.evict(key)。

我认为调用本机缓存实现方法更好。

代码语言:javascript
复制
(Ehcache)cache.getNativeCache()...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28959402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档