缓存逐出多个密钥之一

时间:2016-12-06 10:04:22

标签: java spring caching spring-el evict

在我的应用程序中,我有多个具有多个键的可缓存方法:

@Cacheable(cacheNames = "valueCodes", key = "{#value, #fieldId, #projectId}")
@Cacheable(cacheNames = "fieldNames", key = "{#field, #value, #projectId}")
@Cacheable(cacheNames = "qi", key = "{#langCode, #question, #projectId}")
@Cacheable(cacheNames = "fieldCodes", key = "{#name, #projectId}")

现在我想要一个cachevict方法清除所有缓存 #projectId键,这是一个UUID匹配:

@CacheEvict(value = {"valueCodes", "fieldCodes", "qi"; "fieldCodes"}, key = "#projectId")

我在this文章中读到这是不可能的,

  

只有evict注释的关键正则表达式匹配多个元素   在每个cacheNames中

我不确定它们的意思,但我想这与在SpEL中使用正则表达式有关。

所以我开始考虑将我的密钥合并为一个密钥:

@Cacheable(cacheNames="cahceName", key="concat(#projectId).concat(#otherKey)")

并使用正则表达式将所有键与projectId匹配,后跟通配符。但我真的找不到办法做到这一点。

我正在努力实现的目标是什么?如果是这样,我该怎么做?

2 个答案:

答案 0 :(得分:3)

  

我试图完成的是什么?如果是这样,我该怎么做?

你不想做什么。

通常,缓存的行为类似于哈希表,您只能对唯一键进行操作。选择属于项目id的所有内容将需要缓存中的索引和查询机制。有些缓存有,但不是全部,并且没有共同的标准如何完成。

仔细检查分别缓存属于项目的所有零碎是否真的有意义。如果一切都需要一起驱逐,也许它一直在一起使用。或者,例如,在缓存中保留3 2 1 0 9 8 7 6 5 [4] 作为值,该缓存包含属于项目的各种组件。

有关详情,请参阅问题:What is the better option for a multi-level, in-process cache?

删除注释并直接使用缓存可能是有意义的。带注释的选项有限。

答案 1 :(得分:0)

我没有使用注释来通过部分键找到密钥,而是创建了一个为我管理密钥的bean

  1. 我删除了所有@CacheEvict注释。
  2. 创建了一项新服务,管理所有缓存的驱逐

    public interface CacheEvictionService {
        /**
         * Adds the provided key to a global list of keys that we'll need later for eviction
         *
         * @param key the cached key for any entry
         */
        void addKeyToList(String key);
    
        /**
         * Find keys that contain the partial key
         *
         * @param partialKey the cached partial key for an entry
         * @return List of matching keys
         */
        List<String> findKeyByPartialKey(String partialKey);
    
        /**
         * Evicts the cache and key for an entry matching the provided key
         *
         * @param key the key of the entry you want to evict
         */
        void evict(String key);
    
    }
    
    @Service
    public class CacheEvictionServiceImpl implements CacheEvictionService {
        LinkedHashSet<String> cachedKeys = new LinkedHashSet<>();
    
        @Override
        public void addKeyToList(String key) {
            this.cachedKeys.add(key);
        }
    
        @Override
        public List<String> findKeyByPartialKey(String partialKey) {
            List<String> foundKeys = new ArrayList<>();
            for (String cachedKey : this.cachedKeys) {
                if (cachedKey.contains(partialKey)) {
                    foundKeys.add(cachedKey);
                }
            }
            return foundKeys;
        }
    
        @Override
        @CacheEvict(value = {"valueCodes", "fieldCodes", "qi", "fieldNames", "fieldsByType"}, key = "#key")
        public void evict(String key) {
            this.cachedKeys.remove(key);
        }
    }
    
  3. 不使用多个键,而是将不同的键连接成一个字符串

    @Cacheable(cacheNames = "valueCodes", key = "#value.concat(#fieldId).concat(#projectId)")
    
  4. 每次缓存某些内容时将密钥发送到服务

    cacheEvictionService.addKeyToList(StringUtils.join(value, fieldId, projectId));
    
  5. 遍历包含项目ID(或任何其他键)的每个现有密钥

    for (String cachedKey : cacheEvictionService.findKeyByPartialKey(projectId)) {
        cacheEvictionService.evict(cachedKey);
    }
    
相关问题