【问题标题】:Retrieve caching list In Spring在 Spring 中检索缓存列表
【发布时间】:2012-08-02 17:05:04
【问题描述】:

我在独立环境中使用 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 时,它都会首先查看它的缓存资源库。

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

谢谢。

【问题讨论】:

    标签: java spring caching ehcache spring-3


    【解决方案1】:

    Spring Cache 中没有这样的方法来迭代缓存列表。如果要遍历 ClientDTO 的集合,则需要将其放入缓存中:

    @Cacheable(value="client", key="all")
    @Override
    public List<ClientDTO> getAll() throws FixException  {
      List<ClientDTO> clients = null;
      try {
        clients = ....; // fetch all objects
      } catch (EmptyResultDataAccessException e) {
        //
      }
    
      if (clients == null) {
        //
      }
      return clients;
    }
    

    在这种情况下,每次修改客户端对象时,都应该使列表无效。

    【讨论】:

    • 它做了一些事情,但它没有正确使用 Spring Cache。 getClientDTOByClientId 方法如何与 getClientByLogin 连接?它不会返回所有客户端的列表,而只会返回缓存中的一个。
    • 我只想要缓存中的那些。
    • 是的.. 但我怎么能迭代呢?喜欢每个
    • 仅使用 Spring Cache 是不可能的,因为您无法获取存储在缓存中的所有键的列表,因此您不知道可以获取哪些对象。如果您知道一系列客户端 ID,您可以尝试以下操作:for(int clientId=MIN_ID; clientId
    【解决方案2】:

    我找到了解决办法:

    private ClientDTO getClientDTOByClientId(Integer clientId)
    {
        ClientDTO clientDTO = null;
        Cache clientCache = null;
        try
        {
            clientCache = ehCacheCacheManager.getCache("client");
            clientDTO = null;
            if (clientCache != null)
            {
                clientDTO = (ClientDTO) clientCache.get(clientId);
            }
            else
            {
                log.error("clientCache is null");
            }
        }
        catch (Exception e)
        {
            log.error("Couldnt retrieve client from cache. clientId=" + clientId);
        }
        return clientDTO;
    }
    

    【讨论】:

    • 你说你想获取所有缓存对象的列表,但你在这里只获取一个对象......
    【解决方案3】:

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

    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;
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-16
      • 2020-12-05
      • 1970-01-01
      • 2018-01-17
      • 2010-09-23
      • 2018-02-12
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多