Datastore including memory and db

1Gateway Groovy data stores are either held in memory or backed by a MongoDB collection. There are two global datastores bound to any Groovy script by default: db and memory. To obtain a handle to a private datastore use this:


mem=option.memorystore(“Mystore”)

mydb=option.dbstore(“MyDBstore”)

 

DB type data stores can hold messages and text values. Memory stores can hold any variable.

 

Method

Purpose

Example

put(key,value)

Save a variable in global memory

memory.put(“counter”,0)

get(key)

Returns a previously stored variable

c=memory.get(“counter”) or simply memory.counter

 

Tip: the ternary operator can be useful to initialize variables in memory like this:

 

memory.put("counter",memory.counter ? memory.counter+1 : 0)


This is equivalent to:


if (memory.counter) 
memory.put("counter",memory.counter+1)
else
memory.put("counter",0)