java - 在 Spring 中检索缓存列表

标签 java spring caching ehcache spring-3

我在独立环境中使用 Spring3.1。

我正在尝试缓存我的条目。 所以在3.1中我可以这样使用@Cacheable:

@Cacheable("client")
@Override
public ClientDTO getClientByLogin(String login) throws FixException
{
    ClientDTO client = null;
    try
    {
        client = (ClientDTO) jdbcTemplate.queryForObject(GET_CLIENT_BY_LOGIN_STATEMENT, new Object[]
        { login }, new ClientDTO());
    }
    catch (EmptyResultDataAccessException e)
    {
        log.error("Client login not exist in database. login=" + login);
    }

    if (client == null)
    {
        throw new FixException("Return null from DB when executing getClientByLogin(), login=" + login);
    }
    return client;
}

现在,每次我调用 getClient 时,它都会首先在其缓存存储库中查找。

如果我想检索缓存列表以便对其进行迭代。我该怎么做?

谢谢。

最佳答案

如果你想检索缓存的对象,那么下面的代码应该可以工作

public ClientDTO  getCachedClient() {
        Cache cache = cacheManager.getCache("client");
        Object cachedObject = null;
        Object nativeCache = cache.getNativeCache();
        if (nativeCache instanceof net.sf.ehcache.Ehcache) {
            net.sf.ehcache.Ehcache ehCache = (net.sf.ehcache.Ehcache) nativeCache;
            List<Object> keys = ehCache.getKeys();

            if (keys.size() > 0) {
                for (Object key : keys) {
                    Element element = ehCache.get(key);
                    if (element != null) {

                        cachedObject = element.getObjectValue();

                    }
                }
            }
        }
        return (ClientDTO)cachedObject;

    }

关于java - 在 Spring 中检索缓存列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11816215/

相关文章:

java - KeyListener 仅在调试器上工作,但在运行时不起作用

c# - 英特尔的开源 uPNP SDK 有绝对 0 的文档,为什么?

java - Spring 覆盖 bean 配置设置其 "Primary"一个

java - 将数据从应用程序(Java)导入临时表的最快方法是什么?

caching - Redis 将数据存储在哪里

java - Apache Ignite 缓存与 Postgresql

ios - Xcode iOS 图像缓存

java - 通过执行一个简单的 Spring Hibernate 集成程序,我遇到了以下问题

java - 是否有独立于持久化技术的Java Data API

spring - Hibernate 返回 PersistentBag 而不是 List