Ehcache:驱逐缓存元素时的意外行为
Ehcache: unexpected behavior when evicting cache elements
我有两个缓存方法:
@Cacheable(value = CacheName.CACHE_STATIC, key="'getImageUserLayer'.concat(':').concat(#layerId)")
public byte[] getImageUserLayer(int layerId) {
和
@Cacheable(value = CacheName.CACHE_STATIC, key="'getLastModifiedUserLayer'.concat(':').concat(#layerId)")
@Override
public String getLastModifiedUserLayer(int layerId) {
我也有驱逐元素的方法:
@Caching(evict = {
@CacheEvict(value = CacheName.CACHE_STATIC, key = "'getImageUserLayer'.concat(':').concat(#layerId)"),
@CacheEvict(value = CacheName.CACHE_STATIC, key = "'getLastModifiedUserLayer'.concat(':').concat(#layerId)")
})
public void updateLayer(UserLayer layer, int layerId) {
为什么在调用 updateLayer(5510461) 之后,出现下一个奇怪的行为:
2020-02-10 16:47:07.065 [http-nio-8080-exec-9] TRACE o.s.c.i.CacheInterceptor - Computed cache key 'getLastModifiedUserLayer:4900061' for operation Builder[public java.lang.String com.prognoz.smkoob.data.dao.ImageServiceImpl.getLastModifiedUserLayer(int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
2020-02-10 16:48:32.695 [http-nio-8080-exec-7] TRACE o.s.c.i.CacheInterceptor - Invalidating cache key [getImageUserLayer:5510461] for operation Builder[public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)] caches=[cacheStatic] | key=''getImageUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='',false,false on method public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)
2020-02-10 16:48:32.701 [http-nio-8080-exec-7] TRACE o.s.c.i.CacheInterceptor - Invalidating cache key [getLastModifiedUserLayer:5510461] for operation Builder[public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='',false,false on method public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)
2020-02-10 16:48:33.591 [http-nio-8080-exec-4] TRACE o.s.c.i.CacheInterceptor - Computed cache key 'getLastModifiedUserLayer:5510461' for operation Builder[public java.lang.String com.prognoz.smkoob.data.dao.ImageServiceImpl.getLastModifiedUserLayer(int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
2020-02-10 16:48:33.591 [http-nio-8080-exec-4] TRACE o.s.c.i.CacheInterceptor - No cache entry for key 'getLastModifiedUserLayer:5510461' in cache(s) [cacheStatic]
2020-02-10 16:48:33.909 [http-nio-8080-exec-3] TRACE o.s.c.i.CacheInterceptor - Computed cache key 'getLastModifiedUserLayer:4900061' for operation Builder[public java.lang.String com.prognoz.smkoob.data.dao.ImageServiceImpl.getLastModifiedUserLayer(int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
2020-02-10 16:48:33.909 [http-nio-8080-exec-3] TRACE o.s.c.i.CacheInterceptor - No cache entry for key 'getLastModifiedUserLayer:4900061' in cache(s) [cacheStatic]
2020-02-10 16:48:33.909 [http-nio-8080-exec-8] TRACE o.s.c.i.CacheInterceptor - No cache entry for key 'getLastModifiedUserLayer:5129071' in cache(s) [cacheStatic]
2020-02-10 16:48:33.910 [http-nio-8080-exec-8] TRACE o.s.c.i.CacheInterceptor - Computed cache key 'getLastModifiedUserLayer:5129071' for operation Builder[public java.lang.String com.prognoz.smkoob.data.dao.ImageServiceImpl.getLastModifiedUserLayer(int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
为什么它也使另一个密钥失效???例如 4900061.
我的ehcache.xml:
<cache name="cacheStatic"
maxBytesLocalHeap="128M"
maxBytesLocalDisk="2G"
eternal="false"
timeToLiveSeconds="7200"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap"/>
</cache>
根据日志,在线程 http-nio-8080-exec-7
:
中只有两个失效
2020-02-10 16:48:32.695 [http-nio-8080-exec-7] TRACE o.s.c.i.CacheInterceptor - Invalidating cache key [getImageUserLayer:5510461] for operation Builder[public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)] caches=[cacheStatic] | key=''getImageUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='',false,false on method public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)
2020-02-10 16:48:32.701 [http-nio-8080-exec-7] TRACE o.s.c.i.CacheInterceptor - Invalidating cache key [getLastModifiedUserLayer:5510461] for operation Builder[public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='',false,false on method public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)
在线程 http-nio-8080-exec-3
中使用 4900061
的操作是什么,看起来就像是在调用方法 getLastModifiedUserLayer
:
期间尝试查找缓存值
2020-02-10 16:48:33.909 [http-nio-8080-exec-3] TRACE o.s.c.i.CacheInterceptor - Computed cache key 'getLastModifiedUserLayer:4900061' for operation Builder[public java.lang.String com.prognoz.smkoob.data.dao.ImageServiceImpl.getLastModifiedUserLayer(int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
2020-02-10 16:48:33.909 [http-nio-8080-exec-3] TRACE o.s.c.i.CacheInterceptor - No cache entry for key 'getLastModifiedUserLayer:4900061' in cache(s) [cacheStatic]
我有两个缓存方法:
@Cacheable(value = CacheName.CACHE_STATIC, key="'getImageUserLayer'.concat(':').concat(#layerId)")
public byte[] getImageUserLayer(int layerId) {
和
@Cacheable(value = CacheName.CACHE_STATIC, key="'getLastModifiedUserLayer'.concat(':').concat(#layerId)")
@Override
public String getLastModifiedUserLayer(int layerId) {
我也有驱逐元素的方法:
@Caching(evict = {
@CacheEvict(value = CacheName.CACHE_STATIC, key = "'getImageUserLayer'.concat(':').concat(#layerId)"),
@CacheEvict(value = CacheName.CACHE_STATIC, key = "'getLastModifiedUserLayer'.concat(':').concat(#layerId)")
})
public void updateLayer(UserLayer layer, int layerId) {
为什么在调用 updateLayer(5510461) 之后,出现下一个奇怪的行为:
2020-02-10 16:47:07.065 [http-nio-8080-exec-9] TRACE o.s.c.i.CacheInterceptor - Computed cache key 'getLastModifiedUserLayer:4900061' for operation Builder[public java.lang.String com.prognoz.smkoob.data.dao.ImageServiceImpl.getLastModifiedUserLayer(int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
2020-02-10 16:48:32.695 [http-nio-8080-exec-7] TRACE o.s.c.i.CacheInterceptor - Invalidating cache key [getImageUserLayer:5510461] for operation Builder[public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)] caches=[cacheStatic] | key=''getImageUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='',false,false on method public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)
2020-02-10 16:48:32.701 [http-nio-8080-exec-7] TRACE o.s.c.i.CacheInterceptor - Invalidating cache key [getLastModifiedUserLayer:5510461] for operation Builder[public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='',false,false on method public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)
2020-02-10 16:48:33.591 [http-nio-8080-exec-4] TRACE o.s.c.i.CacheInterceptor - Computed cache key 'getLastModifiedUserLayer:5510461' for operation Builder[public java.lang.String com.prognoz.smkoob.data.dao.ImageServiceImpl.getLastModifiedUserLayer(int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
2020-02-10 16:48:33.591 [http-nio-8080-exec-4] TRACE o.s.c.i.CacheInterceptor - No cache entry for key 'getLastModifiedUserLayer:5510461' in cache(s) [cacheStatic]
2020-02-10 16:48:33.909 [http-nio-8080-exec-3] TRACE o.s.c.i.CacheInterceptor - Computed cache key 'getLastModifiedUserLayer:4900061' for operation Builder[public java.lang.String com.prognoz.smkoob.data.dao.ImageServiceImpl.getLastModifiedUserLayer(int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
2020-02-10 16:48:33.909 [http-nio-8080-exec-3] TRACE o.s.c.i.CacheInterceptor - No cache entry for key 'getLastModifiedUserLayer:4900061' in cache(s) [cacheStatic]
2020-02-10 16:48:33.909 [http-nio-8080-exec-8] TRACE o.s.c.i.CacheInterceptor - No cache entry for key 'getLastModifiedUserLayer:5129071' in cache(s) [cacheStatic]
2020-02-10 16:48:33.910 [http-nio-8080-exec-8] TRACE o.s.c.i.CacheInterceptor - Computed cache key 'getLastModifiedUserLayer:5129071' for operation Builder[public java.lang.String com.prognoz.smkoob.data.dao.ImageServiceImpl.getLastModifiedUserLayer(int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
为什么它也使另一个密钥失效???例如 4900061.
我的ehcache.xml:
<cache name="cacheStatic"
maxBytesLocalHeap="128M"
maxBytesLocalDisk="2G"
eternal="false"
timeToLiveSeconds="7200"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap"/>
</cache>
根据日志,在线程 http-nio-8080-exec-7
:
2020-02-10 16:48:32.695 [http-nio-8080-exec-7] TRACE o.s.c.i.CacheInterceptor - Invalidating cache key [getImageUserLayer:5510461] for operation Builder[public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)] caches=[cacheStatic] | key=''getImageUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='',false,false on method public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)
2020-02-10 16:48:32.701 [http-nio-8080-exec-7] TRACE o.s.c.i.CacheInterceptor - Invalidating cache key [getLastModifiedUserLayer:5510461] for operation Builder[public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='',false,false on method public void com.prognoz.smkoob.data.dao.meta.UserLayerServiceImpl.updateLayer(com.prognoz.smkoob.domain.UserLayer.UserLayer,int)
在线程 http-nio-8080-exec-3
中使用 4900061
的操作是什么,看起来就像是在调用方法 getLastModifiedUserLayer
:
2020-02-10 16:48:33.909 [http-nio-8080-exec-3] TRACE o.s.c.i.CacheInterceptor - Computed cache key 'getLastModifiedUserLayer:4900061' for operation Builder[public java.lang.String com.prognoz.smkoob.data.dao.ImageServiceImpl.getLastModifiedUserLayer(int)] caches=[cacheStatic] | key=''getLastModifiedUserLayer'.concat(':').concat(#layerId)' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
2020-02-10 16:48:33.909 [http-nio-8080-exec-3] TRACE o.s.c.i.CacheInterceptor - No cache entry for key 'getLastModifiedUserLayer:4900061' in cache(s) [cacheStatic]