Dan*_*mes 14 java spring caching
在@CacheEvict中有没有使用通配符的方法?
我有一个多租户应用程序,有时需要从租户的缓存中驱逐所有数据,但不是系统中所有租户的数据.
请考虑以下方法:
@Cacheable(value="users", key="T(Security).getTenant() + #user.key")
public List<User> getUsers(User user) {
...
}
Run Code Online (Sandbox Code Playgroud)
所以,我想做的事情如下:
@CacheEvict(value="users", key="T(Security).getTenant() + *")
public void deleteOrganization(Organization organization) {
...
}
Run Code Online (Sandbox Code Playgroud)
无论如何要做到这一点?
答案是:没有。
实现你想要的并不是一件容易的事情。
因此没有。
但是,如果您找到解决方案,请告诉我,因为您想要的功能非常有用。
正如宇宙中 99% 的问题一样,答案是:视情况而定。如果你的缓存管理器实现了一些可以处理这个问题的东西,那就太好了。但事实似乎并非如此。
如果您正在使用SimpleCacheManager
Spring 提供的基本内存缓存管理器,那么您可能正在使用ConcurrentMapCache
Spring 附带的它。尽管无法扩展以ConcurrentMapCache
处理键中的通配符(因为缓存存储是私有的,您无法访问它),但您可以将其用作您自己的实现的灵感。
下面是一个可能的实现(除了检查它是否有效之外,我没有真正测试它)。ConcurrentMapCache
这是对方法进行修改的简单副本evict()
。不同之处在于,此版本会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 {
}
}
Run Code Online (Sandbox Code Playgroud)
我相信读者知道如何使用自定义缓存实现来初始化缓存管理器。有很多文档可以向您展示如何做到这一点。正确配置项目后,您可以正常使用注释,如下所示:
@CacheEvict(value = { "cacheName" }, key = "'regex:#tenant'+'.*'")
public myMethod(String tenant){
...
}
Run Code Online (Sandbox Code Playgroud)
同样,这还远未经过适当的测试,但它为您提供了一种方法来完成您想做的事情。如果您使用另一个缓存管理器,您可以类似地扩展其缓存实现。
归档时间: |
|
查看次数: |
5172 次 |
最近记录: |