Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.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:如何从Ecache中删除?_Java_Spring_Java 8_Ehcache_Hybris - Fatal编程技术网

Java:如何从Ecache中删除?

Java:如何从Ecache中删除?,java,spring,java-8,ehcache,hybris,Java,Spring,Java 8,Ehcache,Hybris,这就是我在GET请求上缓存数据的方式: @Cacheable(value = "userCache", key = "T(de.hybris.platform.commercewebservicescommons.cache.CommerceCacheKeyGenerator).generateKey(true,true,'DTO',#currentPage,#pageSize,#sort,#fields)") public UsersDTO getUsers(final int current

这就是我在GET请求上缓存数据的方式:

@Cacheable(value = "userCache", key = "T(de.hybris.platform.commercewebservicescommons.cache.CommerceCacheKeyGenerator).generateKey(true,true,'DTO',#currentPage,#pageSize,#sort,#fields)")
public UsersDTO getUsers(final int currentPage, final int pageSize, final String sort,
            final String fields)
{
        // Code......
}
如何仅从缓存中删除其他请求的“DTO”(例如,当我触发删除请求时,我想删除“DTO”)呢?我们可以通过注释来实现吗

现在我正在这样做,但我认为它正在删除整个用户缓存

public void updateUserCache()
    {
        final Cache cache = cacheManager.getCache("userCache");
        final Ehcache ehCache = (Ehcache) cache.getNativeCache();
        ehCache.getKeys().forEach(key -> {
            cache.evict(key);
        });
    }
请尝试以下代码:

@CacheEvict(value = "userCache", key="#userId")
public void updateUserCache()
    {
....
...
}
或者你可以这样做

   if (cache.isKeyInCache(userId))
    {
        cache.remove(key);
    }