(Quick Reference)

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 in Config.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() }

Regions may be configured with a syntax like this:

// grails-app/conf/Config.groovy

import com.gemstone.gemfire.cache.DataPolicy

grails.gemfire.regions = {

region1 { // configure region1… dataPolicy = DataPolicy.REPLICATE publisher = false }

region2 { // configure region2… dataPolicy = DataPolicy.PARTITION }

}

The DSL supports all of the configuration attributes which are supported by the AttributesFactory.

The DSL provides some syntax to simplify the configuration and eliminate explicit references to GemFire classes like DataPolicy. The previous example could be written like this:

// grails-app/conf/Config.groovy

grails.gemfire.regions = {

region1 { // configure region1… dataPolicy = REPLICATE publisher = false }

region2 { // configure region2… dataPolicy = PARTITION }

}

The following table lists all of the properties which may be referenced directly without a class prefix.

ClassProperty Name
com.gemstone.gemfire.cache.DataPolicyEMPTY
com.gemstone.gemfire.cache.DataPolicyNORMAL
com.gemstone.gemfire.cache.DataPolicyPARTITION
com.gemstone.gemfire.cache.DataPolicyPERSISTENT_REPLICATE
com.gemstone.gemfire.cache.DataPolicyPRELOADED
com.gemstone.gemfire.cache.DataPolicyREPLICATE
com.gemstone.gemfire.cache.ExpirationActionDESTROY
com.gemstone.gemfire.cache.ExpirationActionINVALIDATE
com.gemstone.gemfire.cache.ExpirationActionLOCAL_DESTROY
com.gemstone.gemfire.cache.ExpirationActionLOCAL_INVALIDATE
com.gemstone.gemfire.cache.ScopeDISTRIBUTED_ACK
com.gemstone.gemfire.cache.ScopeDISTRIBUTED_NO_ACK
com.gemstone.gemfire.cache.ScopeGLOBAL
com.gemstone.gemfire.cache.ScopeLOCAL

Several properties require an instance of the ExpirationAttributes class. These include 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.ExpirationAttributes

grails.gemfire.regions = {

region1 { entryTimeToLive = new ExpirationAttributes(120) entryTimeToIdle = new ExpirationAttributes(200, ExpirationAction.DESTROY) }

}

The DSLS allows the explicit references to the ExpirationAction and ExpirationAttributes classes to be removed.

// grails-app/conf/Config.groovy

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

grails.gemfire.regions = {

departmentData { entryTimeToLive = expirationAttributes(120) }

}

It could be accessed like this:

// grails-app/controllers/com/demo/ReportingController.groovy

package com.demo

class 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' }

// … }