Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring中检索缓存列表_Java_Spring_Caching_Ehcache_Spring 3 - Fatal编程技术网

Java 在Spring中检索缓存列表

Java 在Spring中检索缓存列表,java,spring,caching,ehcache,spring-3,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.quer

我在独立环境中使用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时,它都会首先查看其缓存响应

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


谢谢。

在SpringCache中没有这样的方法来迭代缓存列表。如果要迭代ClientD集合,则需要将其放入缓存:

@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;
}
@Cacheable(value=“client”,key=“all”)
@凌驾
public List getAll()引发修复异常{
列表客户端=null;
试一试{
客户端=…;//获取所有对象
}捕获(EmptyResultDataAccessE异常){
//
}
如果(客户端==null){
//
}
返回客户;
}

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

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

@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;
}
@Cacheable(value=“client”,key=“all”)
@凌驾
public List getAll()引发修复异常{
列表客户端=null;
试一试{
客户端=…;//获取所有对象
}捕获(EmptyResultDataAccessE异常){
//
}
如果(客户端==null){
//
}
返回客户;
}
在这种情况下,每次修改客户端对象时,都应该使列表无效。

我找到了一个解决方案:

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;
}
我找到了一个解决方案:

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

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

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;

    }
public ClientDTO getCachedClient(){
Cache Cache=cacheManager.getCache(“客户端”);
对象cachedObject=null;
对象nativeCache=cache.getNativeCache();
if(net.sf.ehcache.ehcache的nativeCache实例){
net.sf.ehcache.ehcache ehcache=(net.sf.ehcache.ehcache)nativeCache;
List keys=ehCache.getKeys();
如果(key.size()>0){
用于(对象关键点:关键点){
Element=ehCache.get(key);
if(元素!=null){
cachedObject=element.getObjectValue();
}
}
}
}
返回(ClientDTO)cachedObject;
}

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

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;

    }
public ClientDTO getCachedClient(){
Cache Cache=cacheManager.getCache(“客户端”);
对象cachedObject=null;
对象nativeCache=cache.getNativeCache();
if(net.sf.ehcache.ehcache的nativeCache实例){
net.sf.ehcache.ehcache ehcache=(net.sf.ehcache.ehcache)nativeCache;
List keys=ehCache.getKeys();
如果(key.size()>0){
用于(对象关键点:关键点){
Element=ehCache.get(key);
if(元素!=null){
cachedObject=element.getObjectValue();
}
}
}
}
返回(ClientDTO)cachedObject;
}

它做了一些事情,但没有正确使用Spring缓存。getClientDTOByClientId方法如何与getClientByLogin连接?它不会返回所有客户端的列表,但只返回缓存中的一个。我只需要缓存中的客户端。是。。但是我怎么能重复呢??和for eachUsing only Spring缓存一样,这是不可能的,因为您无法获取缓存中存储的所有键的列表,所以您不知道可以获取哪些对象。如果您知道客户机ID的范围,可以尝试以下操作:for(int-clientId=MIN\u-ID;clientId