【发布时间】:2017-08-04 00:12:45
【问题描述】:
使用 grails 2.4.2、cache:1.1.8 和 cache-ehcache:1.0.5,我发现缓存只包含一个条目。一旦添加第二个条目,第一个条目就会消失。
配置
BuildConfig.groovy:
compile ":cache:1.1.8"
compile ":cache-ehcache:1.0.5"
Config.groovy:
grails.hibernate.cache.queries = true
grails {
cache {
order = 2000
enabled = true
clearAtStartup = true
shared = true
ehcache {
reloadable = false
}
}
}
grails.cache.clearAtStartup = true
grails.cache.config = {
cache {
name 'siteSettings'
}
provider {
updateCheck false
monitoring 'on'
dynamicConfig false
}
defaultCache {
maxElementsInMemory 10000
timeToIdleSeconds 600
timeToLiveSeconds 600
overflowToDisk false
diskPersistent false
memoryStoreEvictionPolicy 'LRU'
}
defaults {
maxElementsInMemory 10000
timeToIdleSeconds 600
timeToLiveSeconds 600
overflowToDisk false
diskPersistent false
memoryStoreEvictionPolicy 'LRU'
}
}
SettingsService.groovy 中的缓存方法:
@Cacheable(value='siteSettings')
public JSONElement getSiteSettings(Integer site){
log.info "NOT CACHED for this request (${site})"
日志输出
1) site=229 的第一个请求(没有缓存):
Initialised cache: siteSettings
CacheDecoratorFactory not configured for defaultCache. Skipping for 'siteSettings'.
siteSettings size: 0
cacheKeys: 0
NOT CACHED for this request (229)
put at siteSettings: [ key = grails.plugin.cache.CustomCacheKeyGenerator$CacheKey@490f0efa, value={"test":229}, version=1, hitCount=3, CreationTime = 1489534645017, LastAccessTime = 1489534976819 ]
2) site=229 的第二个请求(缓存保存 229):
siteSettings size: 1
cacheKeys: 1
key 229: [test:229]
3) site=282 的第一个请求(缓存保存 229):
siteSettings size: 1
cacheKeys: 1
key 229: [test:229]
NOT CACHED for this request (282)
evicted from siteSettings: [ key = grails.plugin.cache.CustomCacheKeyGenerator$CacheKey@490f0efa, value={"test":229}, version=1, hitCount=3, CreationTime = 1489534645017, LastAccessTime = 1489534976819 ]
put at siteSettings: [ key = grails.plugin.cache.CustomCacheKeyGenerator$CacheKey@492726a5, value={"test":282}, version=1, hitCount=0, CreationTime = 1489534976848, LastAccessTime = 1489534976848 ]
4) site=282 的第二个请求(缓存保存 282,而不是 229):
siteSettings size: 1
cacheKeys: 1
key 282: [test:282]
5) site=229 的第三个请求(缓存保存 282,而不是 229):
siteSettings size: 1
cacheKeys: 1
key 282: [test:282]
NOT CACHED for this request (229)
evicted from siteSettings: [ key = grails.plugin.cache.CustomCacheKeyGenerator$CacheKey@492726a5, value={"test":282}, version=1, hitCount=3, CreationTime = 1489534976848, LastAccessTime = 1489535172110 ]
put at siteSettings: [ key = grails.plugin.cache.CustomCacheKeyGenerator$CacheKey@490f0efa, value={"test":229}, version=1, hitCount=0, CreationTime = 1489535172193, LastAccessTime = 1489535172193 ]
分析
在 site=229 的第一个请求中,没有缓存任何内容。正如预期的那样
在 site=229 的第二个请求中,以 229 为关键字的条目被缓存。正如预期的那样
在 site=282 的第一个请求中,以 229 为键的条目被缓存,但以 282 为键的条目未被缓存。正如预期的那样
在 site=282 的第二个请求中,282 键上的条目被缓存,但 229 键上的条目不再被缓存。 UNEXPECTED - 期望两个键都被缓存。
在 site=229 的第三个请求中,282 键上的条目仍被缓存,但 229 键上的条目未被缓存。 UNEXPECTED - 期望两个键都被缓存(随后,282 被驱逐,而 229 被放置,这与观察到的行为相匹配)
我做错了什么?感谢您的帮助。
【问题讨论】:
-
您从哪里获取 siteSettings、cacheKeys 和键
... 日志记录语句?你能在 Ehcache 上添加一个缓存事件监听器,看看你是否有效地获得了驱逐吗? -
我实现了一个 CacheEventListener 并将输出添加到我的原始帖子中。它似乎验证了我的怀疑:每次将一个条目添加到缓存中时,前一个条目都会被逐出。
-
为了回答您问题的第一部分,Louis,日志记录语句源自调用可缓存 grails 服务方法的 grails 控制器操作。