2 Cache Regions - Reference Documentation
Authors: Jeff Brown, Graeme Rocher
Version: 1.0.0.BUILD-SNAPSHOT
2 Cache Regions
GemFire allows your data to be organized within a cache using data regions. The Grails GemFire plugin provides a DSL for describing the regions available to the application and provides a simple convention based approach to accessing regions.2.1 Cache Region Configuration
GemFire regions may be described inConfig.groovy by assigning a value to
the grails.gemfire.regions property. The value should be a closure which
contains GemFire Region DSL code. Details about the DSL are described below.The code below declares 2 regions with the names region1 and region2.// grails-app/conf/Config.groovy
grails.gemfire.regions = { // declare region1
region1() // declare region2
region2()
}// grails-app/conf/Config.groovyimport com.gemstone.gemfire.cache.DataPolicygrails.gemfire.regions = { region1 { // configure region1… dataPolicy = DataPolicy.REPLICATE publisher = false } region2 { // configure region2… dataPolicy = DataPolicy.PARTITION }}
DataPolicy. The previous
example could be written like this:// grails-app/conf/Config.groovygrails.gemfire.regions = { region1 {
// configure region1…
dataPolicy = REPLICATE
publisher = false
} region2 {
// configure region2…
dataPolicy = PARTITION
}}| Class | Property Name |
|---|---|
| com.gemstone.gemfire.cache.DataPolicy | EMPTY |
| com.gemstone.gemfire.cache.DataPolicy | NORMAL |
| com.gemstone.gemfire.cache.DataPolicy | PARTITION |
| com.gemstone.gemfire.cache.DataPolicy | PERSISTENT_REPLICATE |
| com.gemstone.gemfire.cache.DataPolicy | PRELOADED |
| com.gemstone.gemfire.cache.DataPolicy | REPLICATE |
| com.gemstone.gemfire.cache.ExpirationAction | DESTROY |
| com.gemstone.gemfire.cache.ExpirationAction | INVALIDATE |
| com.gemstone.gemfire.cache.ExpirationAction | LOCAL_DESTROY |
| com.gemstone.gemfire.cache.ExpirationAction | LOCAL_INVALIDATE |
| com.gemstone.gemfire.cache.Scope | DISTRIBUTED_ACK |
| com.gemstone.gemfire.cache.Scope | DISTRIBUTED_NO_ACK |
| com.gemstone.gemfire.cache.Scope | GLOBAL |
| com.gemstone.gemfire.cache.Scope | LOCAL |
regionTimeToLive, regionIdleTimeout, entryTimeToLive and entryIdleTimeout. Configuring those
properties might look something like this:// grails-app/conf/Config.groovy import com.gemstone.gemfire.cache.ExpirationAction import com.gemstone.gemfire.cache.ExpirationAttributesgrails.gemfire.regions = { region1 { entryTimeToLive = new ExpirationAttributes(120) entryTimeToIdle = new ExpirationAttributes(200, ExpirationAction.DESTROY) }}
ExpirationAction and ExpirationAttributes classes
to be removed.// grails-app/conf/Config.groovygrails.gemfire.regions = { region1 {
entryTimeToLive = expirationAttributes(120)
entryTimeToIdle = expirationAttributes(200, DESTROY)
}}2.2 Accesing Cache Regions
For each configured cache region a bean is added to the Spring application context with a corresponding name. Those beans are the simplest way to interact with the cache region. The bean may be treated as a map of key value pairs.If a region were configured like this:// grails-app/conf/Config.groovygrails.gemfire.regions = { departmentData {
entryTimeToLive = expirationAttributes(120)
}}// grails-app/controllers/com/demo/ReportingController.groovypackage com.democlass ReportingController { def departmentData def index = {
def hrData = departmentData['hr']
def accountData = departmentData['accounting'] // …
} def addToCache = {
def key = params.key
def value = params.value
departmentData[key] = value redirect action: 'list'
} // …
}