Hitachi

In-Memory Data Grid Hitachi Elastic Application Data Store


16.1.1 General procedure for accessing caches and manipulating data

The following figure shows the general procedure for accessing caches and manipulating data:

[Figure]

Organization of this subsection

(1) Example of a source program in Java

The following shows an example of a source program in Java (one that stores keys and values):

// Import the package provided by EADS
import com.hitachi.software.xeads.client.api.*;
 
public class PutSample {
 
public static void main(String[] args) {
    // Initialize the EADS client
 
    final String CONFPATH = "./conf/eads_sample_client.properties";
    final String CACHENAME = "cache1";
    CacheManager cacheManager = null;
    Cache cache = null;
 
    try {
        cacheManager = CacheManager.create(CONFPATH);
        // Start accessing caches
        cache = cacheManager.getCache(CACHENAME);
        System.out.println("cache start succeeded. (cache name = " + CACHENAME + ")");
        // Store keys and values
        final String KEY = "key1";
        final String VALUE = "value1";
 
        cache.put(KEY, VALUE);
        System.out.println("PUT succeeded. (key = " + KEY + ", value = " + VALUE + ")");
 
        String value = (String)cache.get(KEY);
        System.out.println("GET succeeded. (key = " + KEY + ", value = " + value + ")");
 
    } catch (CacheException e) {
        System.out.println("cache operation failed.(cache name = " + CACHENAME + ", error code = " + e.getErrorCode() + ")");
    }finally{
        if(cacheManager != null){
            // Terminate accesses to caches
            if(cache != null){
                try{
                    cacheManager.removeCache(CACHENAME);
                    System.out.println("cache stop succeeded. (cache name = " + CACHENAME + ")");
                }catch(CacheException e){
                    System.out.println("CacheManager.removeCache() failed. (error code = " + e.getErrorCode() + ")");
                }
            }
            // Terminate the use of EADS client
            try{
                cacheManager.destroy();
            }catch(CacheException e){
                System.out.println("CacheManager.destroy() failed. (error code = " + e.getErrorCode() + ")");
            }
        }
    }
  }
}

(2) Importing the package provided by EADS

Import the following package provided by EADS:

import com.hitachi.software.xeads.client.api.*;

(3) Initializing the EADS client

To initialize the EADS client, use create() of the CacheManager class to create an instance of the CacheManager class.

The settings, including the EADS servers to be connected, are specified according to the client properties.

If you want to use multiple instances of a CacheManager class whose settings are different, such as when a connection is established with multiple clusters, edit the client properties and then execute create() of the CacheManager class multiple times. To terminate use of the EADS client when you have executed create() of the CacheManager class multiple times, execute destroy() of the CacheManager class on each of the acquired instances of the CacheManager class.

(4) Starting to access caches

After you have finished initializing the EADS client, start accessing caches.

To start accessing caches, use getCache() of the CacheManager class to create an instance for manipulating data (an instance of the Cache class).

(5) Storing keys and values

To store keys and values in a cache, use put() of the Cache class.

In put(), specify the keys and values to be stored in the cache.

(6) Acquiring values

To acquire values from a cache, use get() of the Cache class.

In get(), specify the key associated with the value you want to acquire.

If the value is acquired successfully by get(), the value associated with the key is returned as a return value.

The following shows an example of a source program for acquiring values.

Example of a source program (for acquiring values)

    // Acquire value
    final String KEY = "key1";
    try {
        String value = (String) cache.get(KEY);
        System.out.println("GET succeeded. (key = " + KEY + ", value = " + value + ")");
    } catch (CacheException e) {
        int errcode = e.getErrorCode();
        System.out.println("GET failed. (key = " + KEY + ", error code = " + errcode + ")");
    }

(7) Deleting keys and values

To delete a specified key and the value associated with that key from a cache, use remove() of the Cache class.

In remove(), specify the key associated with the value you want to delete.

(8) Executing user functions

To execute user functions, use executeFunction() of the Cache class.

Specify in executeFunction() the name of the key or the group for executing the user function or an instance of the Node class, the name of the user function to be executed, and arguments to be passed to the user function.

If the user function is executed by executeFunction(), the user function execution results are returned.

(9) Terminating accesses to caches

To terminate accesses to caches, use removeCache() of the CacheManager class.

In removeCache(), specify the name of the cache to which you want to terminate access.

(10) Terminating use of the EADS client

To terminate use of the EADS client, use destroy() of the CacheManager class.