Hibernate 如何在tomcat server for Grails应用程序中实现缓存自动刷新
我们在tomcat服务器上运行一个web应用程序,并在服务器上维护缓存数据。每当我们得到新的数据,而这些数据并没有在缓存中获取,也不能在UI上显示时,我们就面临着这个问题。每当获得新流量时,我们都需要重新启动服务器,以便在UI上查看新数据。下面是hibernate配置部分Hibernate 如何在tomcat server for Grails应用程序中实现缓存自动刷新,hibernate,tomcat,grails,caching,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.cach
hibernate {
cache.use_second_level_cache=true
cache.use_query_cache=true
cache.provider_class='org.hibernate.cache.EhCacheProvider'
show_sql=true
}
让我用例子来解释这个问题。。。假设我在缓存中维护服务
,这个服务
来自OracleDB
。我在数据库中获得了新的服务
,当我在数据库中查询相同的服务时,我确实看到了新的服务
,但当我查看UI时,我缺少了新的服务。只有在重新启动Tomcat服务器时,我才能看到这些新的服务
我想知道是否有任何方法可以在不重新启动tomcat服务器的情况下自动刷新缓存,从而在UI中显示数据。谢谢
我正在使用ehcache core.2.4.6
并且没有在我的应用程序中配置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]
如果需要配置ehcache.xml来解决此问题,是否需要更改timeToIdleSeconds=“120”
和timeToLiveSeconds=“120”
值,或者是否需要向配置中添加更多属性。我希望只要数据库中的数据发生变化,缓存就会自动刷新。如果这还不足以解释问题,请告诉我。谢谢。我希望我没有完全偏离主题:-)基本上,您需要配置您的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
中)。有些人不喜欢它,但它可以让您很好地了解应用程序,包括缓存。只需确保它没有部署到生产环境中;-) 我希望我没有完全偏离主题:-)基本上,您需要配置您的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
中)。有些人不喜欢它,但它可以让您很好地了解应用程序,包括缓存。只需确保它没有部署到生产环境中;-) 谢谢你的回复。。。在我的应用程序中,我们没有配置ehcache.xml,而是使用ehcache核心。2.4.6
1和以下是默认缓存。。。和org.hibernate.cache.StandardQueryCache[maxElementsInMemory=10000,overflowToDisk=true,maxElementsOnDisk=10000000,eternal=false,timeToLiveSeconds=120,timeToIdleSeconds=120,MemoryStoreReceivingPolicy=LRU,diskPersistent=false]与上面的org.hibernate.cache.UpdateTimestampsCache相同,ehcache正在使用ehcache core.2.4.6中的默认值,我不确定这些值是否会对我的问题产生影响。。。任何建议请…谢谢你的回复。。。在我的应用程序中,我们没有配置ehcache.xml,而是使用ehcache核心。2.4.6
1和以下是默认缓存。。。和org.hibernate.cache.StandardQueryCache[maxElementsInMemory=10000,overflowToDisk=true,maxElementsOnDisk=10000000,eternal=false,timeToLiveSeconds=120,timeToIdleSeconds=120,MemoryStoreReceivingPolicy=LRU,diskPersistent=false]与上面的org.hibernate.cache.UpdateTimestampsCache相同,ehcache正在使用ehcache core.2.4.6中的默认值,我不确定这些值是否会对我的问题产生影响。。。有什么建议请。。。