2011-05-04 2 views
1

Grails를 시작할 때 전체 테이블을 캐시에로드 할 수 있습니까?캐시에 전체 테이블로드 Grails

예를 들어 정적 읽기 전용 데이터로 사용되는 5000 개의 레코드가있는 2 개의 테이블이 있습니다. 이 데이터는 다른 테이블의 모든 정보가이 읽기 전용 테이블에서 파생되었으므로 가장 많이 발생합니다.

나는 grails에 캐시 사용 시나리오가 있지만이 정보는 짧은 시간 후에 캐시에서 지속적으로 제거되며 다음 요청시에만 다시 캐시됩니다.

기본적으로이 정적 데이터에 대한 데이터베이스에 액세스하지 않아도 응답 시간이 단축됩니다.

감사합니다.

답변

6

ehcache.xml을 사용하여 캐시 동작을 구성 할 수 있습니다. 캐시가없는 경우 캐시는 기본값으로 구성되지만 사용하면 캐시가 대신 사용됩니다. grails-app/conf에 넣으면 클래스 패스에 복사됩니다.

가정 도메인 클래스는 폐기되지 않도록 당신은 캐시와 = 진정한 영원한 설정하는 요소의 수를 지정할 수 있습니다 com.yourcompany.yourapp.YourDomainClass입니다 :

<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()'로 대체 할 수 있습니까? –

+0

감사합니다. 이것은 잘 작동하는 것 같습니다. – pieterk

관련 문제