rayman
rayman

Reputation: 21596

Retrieve caching list In Spring

I am using Spring3.1 in standalone Env.

I am trying to cache my entries. So in 3.1 I can use @Cacheable this way:

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

now each time i invoke getClient it will look first in it's cache respositry.

If I want to retrieve the caching list in order to iterate on it. How i do it?

thanks.

Upvotes: 1

Views: 32136

Answers (3)

Dikshit Rajkhowa
Dikshit Rajkhowa

Reputation: 111

If you want to retrieve the cached object, then the following code should work

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;

    }

Upvotes: 4

rayman
rayman

Reputation: 21596

I found a solution:

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

Upvotes: -5

ragnor
ragnor

Reputation: 2528

There's no such way in Spring Cache to iterate on caching list. If you want to iterate over collection of ClientDTO you need to put it into the cache:

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

In such case each time you modify client object you should invalidate the list.

Upvotes: 2

Related Questions