如何在Tomcat服务器中为Grails应用程序实现缓存自动刷新

时间:2015-06-25 17:58:43

标签: hibernate tomcat grails caching

我们在tomcat服务器上运行了一个Web应用程序,我们在服务器上维护缓存数据。每当我们获得未在缓存中获取但无法在UI上显示的新数据时,我们就会遇到问题。每当我们获得新流量时,我们都需要重新启动服务器,以便在UI上查看新数据。下面是hibernate配置部分......

hibernate {
cache.use_second_level_cache=true
cache.use_query_cache=true
cache.provider_class='org.hibernate.cache.EhCacheProvider'
show_sql=true
}

让我用示例解释这个问题...说我在缓存中维护Services,而Services来自OracleDB。我在数据库中获得了新的Services,当我在数据库中查询相同时,我确实看到了新的Services但是当我查看UI时,我错过了新的Services。只有在重新启动Tomcat服务器时,我才能看到这些新的ehcache-core.2.4.6

我想知道有没有办法自动刷新缓存而无需重新启动tomcat服务器来获取UI中显示的数据。感谢。

我正在使用ehcache.xml并且未在我的应用中配置 CACHE CONFIGURATION org.hibernate.cache.StandardQueryCache ehcache [maxElementsInMemory = 10000, overflowToDisk = true, maxElementsOnDisk = 10000000, eternal = false, timeToLiveSeconds = 120, timeToIdleSeconds = 120, memoryStoreEvictionPolicy = LRU, diskPersistent = false] org.hibernate.cache.UpdateTimestampsCache ehcache [maxElementsInMemory = 10000, overflowToDisk = true, maxElementsOnDisk = 10000000, eternal = false, timeToLiveSeconds = 120, timeToIdleSeconds = 120, memoryStoreEvictionPolicy = LRU, diskPersistent = false] ,我发现维护了两个缓存,并将这些值设置为以下...

timeToIdleSeconds="120"

如果我需要配置ehcache.xml来解决问题,我是否需要更改timeToLiveSeconds="120"Map.compute值,或者是否需要向配置添加更多属性。我希望每当DB数据发生变化时自动刷新缓存。如果这还不足以解释这个问题,请告诉我。感谢。

1 个答案:

答案 0 :(得分:0)

我希望我在这里不完全偏离主题:-)基本上你需要配置你的ehcache并告诉它你希望它对待各种缓存。这通常使用ehcache.xml文件(在grails-app/conf文件夹中)完成。一个例子可能是:

<ehcache>
   <defaultCache
        maxElementsInMemory="1000"
        eternal="false"
        timeToIdleSeconds="3600"
        timeToLiveSeconds="2600"
        overflowToDisk="false">
   </defaultCache>

   <cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="10000"
        timeToIdleSeconds="300" timeToLiveSeconds="300" />
   <cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="10000"
        timeToIdleSeconds="400" timeToLiveSeconds="1200" />

   ...
</ehcache>

要检查您的缓存,您可以使用Java Melody插件(即compile ":grails-melody:1.55.0"中的BuildConfig.groovy)。有些人不喜欢它,但它可以让你很好地了解你的应用程序,包括缓存。只要确保它没有部署到生产中;-)