Grails\Hibernate:要缓存还是不缓存?

Ole*_*ndr 5 grails hibernate ehcache

什么是Hibernate\Grails中最好的缓存策略.是否缓存所有实体和查询以及如何找到最佳解决方案?

这是我的hibernate配置.

hibernate {
  cache.use_second_level_cache = true
  cache.use_query_cache = true
  cache.provider_class = 'org.hibernate.cache.EhCacheProvider'
  connection.useUnicode = true
  connection.characterEncoding = 'UTF-8'
  connection.provider_class = 'org.hibernate.connection.C3P0ConnectionProvider'
  dialect = 'org.hibernate.dialect.MySQL5InnoDBDialect'
  order_updates = true
  c3p0.min_size = 5
  c3p0.max_size = 20
  c3p0.max_statements = 20 * 10
  c3p0.idle_test_period = 15 * 60
}
Run Code Online (Sandbox Code Playgroud)

Ehcache配置

<defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        maxElementsOnDisk="10000000"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"
        diskSpoolBufferSizeMB="100"
        memoryStoreEvictionPolicy="LRU"
/>

<cache
            name="org.hibernate.cache.StandardQueryCache"
            maxElementsInMemory="50"
            eternal="false"
            timeToLiveSeconds="120"
            overflowToDisk="true"
/>
Run Code Online (Sandbox Code Playgroud)

jpk*_*ing 12

最好的缓存策略是:不缓存.说真的,应该只进行缓存(和任何优化)以解决现有问题.如果您没有性能问题,请不要使用缓存.如果确实存在性能问题,请应用这个简单的5步过程来解决性能问题:

  1. 测量.
  2. 测量.
  3. 优化(例如添加缓存)
  4. 测量.
  5. 测量.

请注意,如果您不了解缓存在Hibernate中的工作方式,那么最终可能会导致性能不佳,而不是改进它.此外,即使您了解缓存如何在Hibernate中工作,也可能存在影响性能的其他影响,导致数据库往返速度比查找缓存更快.这就是为什么你应该在做"改进"之前和之后进行衡量.

  • 确实,对不起的语气,但我会保留它.原因是大多数应用程序甚至没有达到数百个并发请求,并且可以通过缓存解决的性能问题不是通用解决方案,并且涉及应用程序本身内部工作的大量知识.这就是为什么"没有缓存"总是最好的通用方法,除非你能证明不是这样. (3认同)