2011-05-04 19 views
1

是否可以在Grails启动时将整个表加载到缓存中?将整个表加载到缓存中Grails

例如,我有一个2个表,每个5000个记录用作静态只读数据。这个数据是最难受的,因为其他表格上的所有信息都来自这个只读表格。

我知道grails有一个缓存使用场景,但是这个信息在短时间内不断被从缓存中逐出,并且它只在下一个请求中被重新缓存。

基本上试图通过不必为数据库访问静态数据来减少响应时间。

谢谢

回答

6

您可以使用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.xmlhttp://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级高速缓存。

+0

Bootstrap.groovy中的代码是否可以用YourDomainClass.list()替换? – 2011-05-05 08:41:19

+0

谢谢。这似乎工作正常。 – pieterk 2011-05-05 09:00:58

相关问题