简体   繁体   English

在Grails中缓存域对象

[英]Caching domain objects in Grails

I have been considering implementing EhCache in my Grails domain objects like this : 我一直在考虑在我的Grails域对象中实现EhCache,如下所示:

static mapping = {
   cache true
}

I am not too familiar with exactly how this caching mechanism works and was wondering what a good rule of thumb is in determining which domain objects would benefit from being cached. 我不太熟悉这个缓存机制是如何工作的,并且想知道在确定哪些域对象可以从缓存中受益时,有什么好的经验法则。 eg, objects that are accessed rarely.. often... ? 例如,很少被访问的对象...通常......?

Thanks! 谢谢!

Caching only works for get() calls by default, but queries use the query cache if you update them with cache: true (criteria and HQL). 缓存仅在默认情况下适用于get()调用,但如果使用cache: true (criteria和HQL)更新查询,则查询将使用查询缓存。

cache true creates a read-write cache but you can configure a read-only cache with cache true创建一个读写缓存,但您可以使用配置只读缓存

static mapping = {
   cache usage:'read-only'
}

The read-only cache is good for lookup data that never changes, for example states, countries, roles, etc. 只读缓存适用于永不更改的查找数据,例如状态,国家/地区,角色等。

If you have domain classes that update, create, or delete frequently, query caching will often be slower than not caching. 如果您经常更新,创建或删除域类,则查询缓存通常比不缓存慢。 This is because changes like these cause all cached queries to be cleared, so you're often going directly to the database anyway. 这是因为这些更改会导致清除所有缓存的查询,因此您无论如何都经常直接访问数据库。 See http://tech.puredanger.com/2009/07/10/hibernate-query-cache/ for a more detailed description of this. 有关此内容的更详细说明,请参见http://tech.puredanger.com/2009/07/10/hibernate-query-cache/ For this reason I rarely use query caching and often disable it completely with 出于这个原因,我很少使用查询缓存,并经常完全禁用它

hibernate {
    cache.use_second_level_cache=true
    cache.use_query_cache=false
    cache.provider_class='org.hibernate.cache.EhCacheProvider'
}

Domain classes that are "read-mostly" are the best candidates for read-write caching. “主要读取”的域类是读写缓存的最佳候选者。 The caches get cleared for each update, create, and delete, but if these are somewhat rare you'll see an overall performance boost. 每次更新,创建和删除都会清除缓存,但如果这些缓存有点罕见,您将看到整体性能提升。

Hibernate has an API to monitor cache usage. Hibernate有一个API来监控缓存使用情况。 The http://grails.org/plugin/app-info and http://grails.org/plugin/hibernate-stats plugins make the information available and you can use the approach there in your own code. http://grails.org/plugin/app-infohttp://grails.org/plugin/hibernate-stats插件使信息可用,您可以在自己的代码中使用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM