有没有办法在@CacheEvict中使用通配符?
我有一个具有多租户的应用程序,它有时需要从租户的缓存中逐出所有数据,而不是系统中所有租户的数据。
考虑以下方法:
@Cacheable(value="users", key="T(Security).getTenant() + #user.key")
public List<User> getUsers(User user) {
...
}所以,我想做一些类似的事情:
@CacheEvict(value="users", key="T(Security).getTenant() + *")
public void deleteOrganization(Organization organization) {
...
}有没有办法做到这一点?
发布于 2016-11-18 23:29:06
就像宇宙中99%的问题一样,答案是:视情况而定。如果你的缓存管理器实现了一些处理这个问题的东西,那就太好了。但事实似乎并非如此。
如果您使用的是SimpleCacheManager,这是Spring提供的一个基本的内存缓存管理器,那么您可能正在使用Spring自带的ConcurrentMapCache。尽管不可能扩展ConcurrentMapCache来处理键中的通配符(因为缓存存储是私有的,您不能访问它),但您可以将其作为自己实现的灵感。
下面是一个可能的实现(除了检查它是否工作之外,我并没有真正测试它)。这是对evict()方法进行修改后的ConcurrentMapCache的普通副本。不同之处在于,这个版本的evict()处理关键字,以确定它是否是正则表达式。在这种情况下,它会遍历存储中的所有键,并逐出与正则表达式匹配的键。
package com.sigraweb.cache;
import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.util.Assert;
public class RegexKeyCache implements Cache {
private static final Object NULL_HOLDER = new NullHolder();
private final String name;
private final ConcurrentMap<Object, Object> store;
private final boolean allowNullValues;
public RegexKeyCache(String name) {
this(name, new ConcurrentHashMap<Object, Object>(256), true);
}
public RegexKeyCache(String name, boolean allowNullValues) {
this(name, new ConcurrentHashMap<Object, Object>(256), allowNullValues);
}
public RegexKeyCache(String name, ConcurrentMap<Object, Object> store, boolean allowNullValues) {
Assert.notNull(name, "Name must not be null");
Assert.notNull(store, "Store must not be null");
this.name = name;
this.store = store;
this.allowNullValues = allowNullValues;
}
@Override
public final String getName() {
return this.name;
}
@Override
public final ConcurrentMap<Object, Object> getNativeCache() {
return this.store;
}
public final boolean isAllowNullValues() {
return this.allowNullValues;
}
@Override
public ValueWrapper get(Object key) {
Object value = this.store.get(key);
return toWrapper(value);
}
@Override
@SuppressWarnings("unchecked")
public <T> T get(Object key, Class<T> type) {
Object value = fromStoreValue(this.store.get(key));
if (value != null && type != null && !type.isInstance(value)) {
throw new IllegalStateException("Cached value is not of required type [" + type.getName() + "]: " + value);
}
return (T) value;
}
@Override
public void put(Object key, Object value) {
this.store.put(key, toStoreValue(value));
}
@Override
public ValueWrapper putIfAbsent(Object key, Object value) {
Object existing = this.store.putIfAbsent(key, value);
return toWrapper(existing);
}
@Override
public void evict(Object key) {
this.store.remove(key);
if (key.toString().startsWith("regex:")) {
String r = key.toString().replace("regex:", "");
for (Object k : this.store.keySet()) {
if (k.toString().matches(r)) {
this.store.remove(k);
}
}
}
}
@Override
public void clear() {
this.store.clear();
}
protected Object fromStoreValue(Object storeValue) {
if (this.allowNullValues && storeValue == NULL_HOLDER) {
return null;
}
return storeValue;
}
protected Object toStoreValue(Object userValue) {
if (this.allowNullValues && userValue == null) {
return NULL_HOLDER;
}
return userValue;
}
private ValueWrapper toWrapper(Object value) {
return (value != null ? new SimpleValueWrapper(fromStoreValue(value)) : null);
}
@SuppressWarnings("serial")
private static class NullHolder implements Serializable {
}
}我相信读者知道如何使用自定义缓存实现来初始化缓存管理器。有很多文档向您展示了如何做到这一点。正确配置项目后,您可以正常使用注释,如下所示:
@CacheEvict(value = { "cacheName" }, key = "'regex:#tenant'+'.*'")
public myMethod(String tenant){
...
}同样,这还远远没有经过适当的测试,但它为您提供了一种做您想做的事情的方法。如果您正在使用另一个缓存管理器,则可以以类似的方式扩展其缓存实现。
发布于 2013-11-05 05:09:39
答案是:不。
而且实现你想要的东西也不是一件容易的事情。
因此,不是。
但是,如果你找到了解决方案,告诉我,因为你想要的功能真的很有用。
发布于 2018-06-25 22:19:30
下面是我在Redis Cache上的工作。假设您想删除所有关键字前缀为' Cache -name:object-name:parentKey‘的cache条目。调用键值为cache-name:object-name:parentKey*的方法。
import org.springframework.data.redis.core.RedisOperations;
...
private final RedisOperations<Object, Object> redisTemplate;
...
public void evict(Object key)
{
redisTemplate.delete(redisTemplate.keys(key));
}来自RedisOperations.java
/**
* Delete given {@code keys}.
*
* @param keys must not be {@literal null}.
* @return The number of keys that were removed.
* @see <a href="http://redis.io/commands/del">Redis Documentation: DEL</a>
*/
void delete(Collection<K> keys);
/**
* Find all keys matching the given {@code pattern}.
*
* @param pattern must not be {@literal null}.
* @return
* @see <a href="http://redis.io/commands/keys">Redis Documentation: KEYS</a>
*/
Set<K> keys(K pattern);https://stackoverflow.com/questions/17749049
复制相似问题