在Grails中缓存域对象 [英] Caching domain objects in Grails

查看:106
本文介绍了在Grails中缓存域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑在Grails域对象中实现EhCache,如下所示:

  static mapping = {
cache真正的

我不太清楚这个缓存机制究竟是如何工作的,在确定哪些域对象能够从缓存中受益时,一个好的经验法则是什么。例如,很少被访问的对象......经常......?

谢谢!

解决方案

默认情况下,缓存仅适用于 get()调用,但如果使用 cache:true更新查询,查询将使用查询缓存(标准和HQL)。

cache true 创建一个读写缓存,但你可以配置一个只读缓存:

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

只读缓存适用于永不改变的查找数据,例如状态,国家,角色等。

如果您有频繁更新,创建或删除的域类,查询缓存通常比不缓存更慢。这是因为像这样的更改会导致所有缓存的查询都被清除,所以您通常会直接进入数据库。请参阅 http://tech.puredanger.com/2009/07/10/hibernate-query-缓存/ 的更详细的描述。由于这个原因,我很少使用查询缓存,并经常完全禁用它。

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

主要读取的域类是读写缓存的最佳候选对象。每次更新,创建和删除都会清除缓存,但如果这些缓存稍微少一些,您会看到整体性能提升。

Hibernate有一个API来监视缓存用法。 http://grails.org/plugin/app-info http://grails.org/plugin/hibernate-stats 插件使信息可用,您可以在自己的代码中使用该方法。


I have been considering implementing EhCache in my Grails domain objects like this :

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).

cache true creates a read-write cache but you can configure a read-only cache with

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. 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. 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.

这篇关于在Grails中缓存域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆