demon101
demon101

Reputation: 564

Grails ehcache.xml custom caches configuration

ehache.xml for cache configuration has been created. But setting in this file works only for hibernate 2L cache For custom caches (for example caching service methods invocation: @Cacheable('someCache') ) cache plugin settings don't work

<cache name="someCache" maxElementsInMemory="100" timeToLiveSeconds="86400" timeToIdleSeconds="86400"/>

But if setting in Config.groovy -- it works

grails.cache.config = {
  cache {
    name 'someCache'
    timeToIdleSeconds 86400
    timeToLiveSeconds 86400
    maxElementsInMemory 100
  }
}

Like for springcache plugin I tried to share cacheManager

cacheManager(EhCacheManagerFactoryBean) {
    shared = true
}

Where is no result

Now I have to write configuration in two places and for cache managing use grailsCacheManager.cacheManager & CacheManager.instance

Any ideas?

Upd: Configuration in Config.groovy has been created

import  grails.test.Test
grails.cache.config = {
  domain {
    name Test
    timeToIdleSeconds 3600
    timeToLiveSeconds 3600
    maxElementsInMemory 50000
  }
  cache {
    name 'myCache'
    timeToIdleSeconds 86400
    timeToLiveSeconds 86400
    maxElementsInMemory 1000
  }
  defaults {
    eternal false
    overflowToDisk false
    diskPersistent false
    timeToIdleSeconds 600
    timeToLiveSeconds 3600
    memoryStoreEvictionPolicy 'LRU'
  }
}

Domain:

package grails.test
class Test {
  static mapping = {
    cache 'nonstrict-read-write'
    version false
  }
}

and in controller I call:

Test.get(params.id)

But cache is empty: grails.test.Test cur size:0

Custom cache "myCache" -- work

Upvotes: 0

Views: 2869

Answers (1)

Dror Bereznitsky
Dror Bereznitsky

Reputation: 20376

Take a look at the Grails ehcache plugin documentation. It contains sections which explains how to configure the Hibernate 2nd level cache using the Grails cache plugin DSL. Look for "Hibernate second-level cache" and "Hibernate domain class second-level caches"

Upvotes: 1

Related Questions