2012-01-20 40 views
3

我注意到我的網站有一個緩慢的下降,打開後debug 'org.hibernate.SQL'我看到了麻煩在哪裏。我給自己定一個域類使用緩存....grails不使用緩存

class Foo{ 
    ... 
    static mapping ={ 
     cache 'read-only' 
    } 

    String name   //<-- simple data type, no associations 
    String description //<-- simple data type, no associations 

} 

我休眠的配置看起來像這樣...

hibernate { 
    cache.use_second_level_cache=true 
    cache.use_query_cache=true 
    cache.provider_class='net.sf.ehcache.hibernate.EhCacheProvider' 
} 

我的查詢看起來像這樣(在網絡流量)。 ..

def wizardFlow = { 
    ... 
    def flow.foos = Foo.list([sort:"name", order:"asc", cache:true]) 
    // def flow.foos = Foo.findAll([cache:true]) <--- same result, no caching 

} 

我認爲,無論是查詢緩存或二級緩存將從被擊中停止數據庫,但我的日誌加載...

select ... from thing thing0_ where thing0_.id=? 
select ... from thing thing0_ where thing0_.id=? 
select ... from thing thing0_ where thing0_.id=? 
select ... from thing thing0_ where thing0_.id=? 

任何人都可以闡明可能發生的事情嗎?其他查詢的行爲應該是他們應該的!

我使用Grails 1.3.7

最後,這裏是我的ehcache.xml中......

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="ehcache.xsd" > 
    <diskStore path="java.io.tmpdir"/> 
    <cacheManagerEventListenerFactory class="" properties=""/> 
    <defaultCache 
     maxElementsInMemory="1000000" 
     eternal="false" 
     timeToIdleSeconds="3600" 
     timeToLiveSeconds="7200" 
     overflowToDisk="true" 
     diskPersistent="false" 
     /> 

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

</ehcache> 
+0

您是否檢查過N + 1選擇問題? – cdeszaq

+0

我的現實生活實體只有一個關聯(1到1),它也被緩存,同樣的方式 –

回答

0

看起來我們已經和這是基本的N + 1問題選擇問題。這就是我們所做的:

import org.hibernate.FetchMode as FM 

...

def crit= item.createCriteria(); 
def itemList = crit.listDistinct 
    { 

     fetchMode "subItem", FM.JOIN 
     cache true 
     order("name","asc") 
    } 

你應該能夠子項目轉出到什麼都你有關聯的,並且應該幫助。

+0

沒有效果。它確實緩存了我的一個呼叫,但所有其他呼叫都是db –

+0

您是否仍然收到很多選擇呼叫? –

+0

您是否還將緩存添加到條件?我更新了答案,看看是否有幫助 –