1
是否可以在Grails啓動時將整個表加載到緩存中?將整個表加載到緩存中Grails
例如,我有一個2個表,每個5000個記錄用作靜態只讀數據。這個數據是最難受的,因爲其他表格上的所有信息都來自這個只讀表格。
我知道grails有一個緩存使用場景,但是這個信息在短時間內不斷被從緩存中逐出,並且它只在下一個請求中被重新緩存。
基本上試圖通過不必爲數據庫訪問靜態數據來減少響應時間。
謝謝
是否可以在Grails啓動時將整個表加載到緩存中?將整個表加載到緩存中Grails
例如,我有一個2個表,每個5000個記錄用作靜態只讀數據。這個數據是最難受的,因爲其他表格上的所有信息都來自這個只讀表格。
我知道grails有一個緩存使用場景,但是這個信息在短時間內不斷被從緩存中逐出,並且它只在下一個請求中被重新緩存。
基本上試圖通過不必爲數據庫訪問靜態數據來減少響應時間。
謝謝
您可以使用ehcache.xml配置緩存行爲。如果你沒有一個緩存配置了默認值,但是如果你這樣做了,那就用它來代替。將它放在grails-app/conf
中,它將被複制到類路徑中。
假設你的域類是com.yourcompany.yourapp.YourDomainClass
,您可以指定元素的數量緩存,並設置永恆= true,所以它們不會被丟棄:
<ehcache>
<diskStore path='java.io.tmpdir' />
<defaultCache
maxElementsInMemory='10000'
eternal='false'
timeToIdleSeconds='120'
timeToLiveSeconds='120'
overflowToDisk='true'
maxElementsOnDisk='10000000'
diskPersistent='false'
diskExpiryThreadIntervalSeconds='120'
memoryStoreEvictionPolicy='LRU'
/>
<cache name='com.yourcompany.yourapp.YourDomainClass'
maxElementsInMemory='10000'
eternal='true'
overflowToDisk='false'
/>
<!-- hibernate stuff -->
<cache name='org.hibernate.cache.StandardQueryCache'
maxElementsInMemory='50'
eternal='false'
timeToLiveSeconds='120'
maxElementsOnDisk='0'
/>
<cache
name='org.hibernate.cache.UpdateTimestampsCache'
maxElementsInMemory='5000'
eternal='true'
maxElementsOnDisk='0'
/>
</ehcache>
有關如何配置的詳細信息ehcache.xml
看http://ehcache.org/ehcache.xml這在評論中有很多文檔。
已經這樣做了,你應該BootStrap.groovy
看起來是這樣的:
import com.yourcompany.yourapp.YourDomainClass
class BootStrap {
def init = { servletContext ->
def ids = YourDomainClass.executeQuery('select id from YourDomainClass')
for (id in ids) {
YourDomainClass.get(id)
}
}
}
已經呼籲get()
每個實例,未來get()
通話將用2級高速緩存。
Bootstrap.groovy中的代碼是否可以用YourDomainClass.list()替換? – 2011-05-05 08:41:19
謝謝。這似乎工作正常。 – pieterk 2011-05-05 09:00:58