我在独立环境中使用 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" />
谢谢,雷。
原文由 rayman 发布,翻译遵循 CC BY-SA 4.0 许可协议
CacheManager.getCache() 返回一个 net.sf.ehcache.Cache,它有一个 getKeys() 方法,它返回一个可以迭代的缓存键列表。要检索已存储的实际对象(与包装的 net.sf.ehcache.Element 相对),请使用 Element.getObjectValue()。
编辑:根据 Spring 的说法, 它们看起来永远不会支持 Cache.getKeys() ,因此您必须转换为底层提供程序。
是这样的: