我在独立环境中使用Spring3.1。 我正在使用@Cachable注释来缓存我的条目。
有时我需要迭代缓存列表才能获得特定值(非密钥)。
所以我设法检索了缓存列表但是我怎么能迭代它的元素。
private ClientDTO getClientDTOByClientId(Integer clientId)
{
Cache clientCache = null;
try
{
clientCache = ehCacheCacheManager.getCache("client");
//need here to iterate on clientCache. how?
}
catch (Exception e)
{
log.error("Couldnt retrieve client from cache. clientId=" + clientId);
}
return clientDTO;
}
我使用的是ehcache机制。
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache" />
<bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml" />
感谢, 射线。
答案 0 :(得分:13)
CacheManager.getCache()返回一个net.sf.ehcache.Cache,它有一个getKeys()方法,它返回一个可以迭代的缓存键列表。要检索已存储的实际对象(而不是包装的net.sf.ehcache.Element),请使用Element.getObjectValue()。
编辑: 根据Spring it doesn't look like they will ever support Cache.getKeys(),所以你必须转发给底层提供者。
这样的事情:
public boolean contains(String cacheName, Object o) {
net.sf.ehcache.EhCache cache = (net.sf.ehcache.EhCache) org.springframework.cache.CacheManager.getCache(cacheName).getNativeCache();
for (Object key: cache.getKeys()) {
Element element = cache.get(key);
if (element != null && element.getObjectValue().equals(o)) {
return true;
}
}
return false;
}
答案 1 :(得分:0)
下面的方法将提供一组缓存对象的键,但是在这里,如果您添加带有键的缓存,则很容易检索(如果直接添加要缓存的对象列表,则将无法解决)。 / p>
此外,我在如下所示的缓存管理器中使用了GuavaCache。
@Bean
public CacheManager cacheManager() {
SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
GuavaCache userLabCache = new GuavaCache("yourCacheName",
CacheBuilder.newBuilder().expireAfterAccess(5, TimeUnit.MINUTES).build());
simpleCacheManager.setCaches(Arrays.asList(userLabCache));
return simpleCacheManager;
}
然后它将键列表作为对象返回,然后您将获得键,并且可以遍历键并从缓存中逐个获取对象。
在您的班级中自动接线Springboot CacheManager
@Autowired
CacheManager cacheManager;
下面是获取所有密钥的代码。
public Set<Object> getAllCachedUserLabKeys() {
Set<Object> keys = null;
try {
GuavaCache cache = (GuavaCache) cacheManager.getCache("yourCacheName");
if (cache != null) {
ConcurrentMap<Object, Object> cachedMap = cache.getNativeCache().asMap();
keys = cachedMap.keySet();
}
} catch (Exception e) {
LOGGER.error("Unknown exception occured while fetchting all cached User Labs..!", e);
}
return keys;
}
答案 2 :(得分:0)
另一种解决方案,通过使用getNativeCache()方法将org.springframework.cache.Cache解析为javax.cache.Cache并使用java迭代器,因为javax.cache.Cache已经扩展了Iterable>。
有关更多详细信息,请阅读javax.cache.Cache javadoc
Cache cache = (Cache) cacheManager.getCache("yourCacheName").getNativeCache();
Iterator<Cache.Entry> iterator = cache.iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next().getKey();
System.out.println(key);
}
答案 3 :(得分:0)
我用这个
第一个端点会告诉你你拥有的缓存
第二个端点将为您提供特定缓存的所有条目(具有安全映射)
第三个端点将为您提供所有缓存的所有条目(使用安全映射)
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
@RestController
@RequestMapping("cache")
public class CacheController {
private final CacheManager cacheManager;
private final ObjectMapper objectMapper;
public CacheController(final CacheManager cacheManager) {
this.cacheManager = cacheManager;
this.objectMapper = new ObjectMapper();
this.objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
this.objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
}
@GetMapping("/names")
public Collection<String> getCacheNames() {
return cacheManager.getCacheNames();
}
@GetMapping("{cacheName}")
public Map<String, Object> getEntriesForCache(@PathVariable(name = "cacheName") final String cacheName) throws JsonProcessingException {
final Cache<String, Object> cache = (Cache<String, Object>) cacheManager.getCache(cacheName).getNativeCache();
final ConcurrentMap<String, Object> data = cache.asMap();
final String json = objectMapper.writeValueAsString(data);
final TypeReference<HashMap<String,Object>> typeRef = new TypeReference<>() {};
return objectMapper.readValue(json, typeRef);
}
@GetMapping("entries")
public Map<String, Map<String, Object>> getAllEntries() throws JsonProcessingException {
final Map<String, Map<String, Object>> entries = new HashMap<>();
for(final String cacheName: getCacheNames()){
entries.put(cacheName, getEntriesForCache(cacheName));
}
return entries;
}
}