Put/Get/Remove Examples

The native client release contains examples, written for .NET and C++, showing how a client application can establish a connection to a cluster and then use that connection to perform basic operations on a remote server. The examples are located in ../examples/dotnet/putgetremove and ../examples/cpp/putgetremove, respectively.

Both examples perform the same sequence of operations, displaying simple log entries as they run.

  • To run an example, follow the instructions in the README.md file in the example directory.
  • Review the source code in the example directory to see exactly how it operates.

  • Begin by running a script that sets up the server-side environment by invoking gfsh commands to create a region, simply called “example_userinfo.”

  • Run the example client application, which performs the following steps:

    • Connects to the server
    • Performs region put operations using key/value pairs
    • Uses region get to retrieve the values
    • Uses region remove to remove the values

.NET Example

This section contains code snippets showing highlights of the .NET put/get/remove example. They are not intended for cut-and-paste execution. For the complete source, see the example source directory.

The .NET example creates a cache, then uses it to create a connection pool and a region object (of class IRegion).

   var cacheFactory = new CacheFactory()
       .Set("log-level", "none");
   var cache = cacheFactory.Create();

   var poolFactory = cache.GetPoolFactory()
       .AddLocator("localhost", 10334);
   poolFactory.Create("pool");

   var regionFactory = cache.CreateRegionFactory(RegionShortcut.PROXY)
       .SetPoolName("pool");
   var region = regionFactory.Create<string, string("example_userinfo");

After declaring some keys and values, the client then populates the data store with two key/value pairs.

      region.Put(rtimmonsKey, rtimmonsValue);
      region.Put(scharlesKey, scharlesValue);

Next, the application retrieves the stored values using Get operations.

      var user1 = region.Get(rtimmonsKey, null);
      var user2 = region.Get(scharlesKey, null);

Finally, the application deletes one of the stored values using the Remove method.

      if (region.Remove(rtimmonsKey))
      {
        Console.WriteLine("Info for " + rtimmonsKey + " has been deleted");
      }
      else
      {
        Console.WriteLine("Info for " + rtimmonsKey + " has not been deleted");
      }

C++ Example

This section contains code snippets showing highlights of the C++ put/get/remove example. They are not intended for cut-and-paste execution. For the complete source, see the example source directory.

The C++ example creates a cache, then uses it to create a connection pool and a region object (of class Region).

  auto cacheFactory = CacheFactory();
  cacheFactory.set("log-level", "none");
  auto cache = cacheFactory.create();
  auto poolFactory = cache.getPoolManager().createFactory();

  poolFactory.addLocator("localhost", 10334);
  auto pool = poolFactory.create("pool");
  auto regionFactory = cache.createRegionFactory(RegionShortcut::PROXY);
  auto region = regionFactory.setPoolName("pool").create("example_userinfo");

The client then populates the data store with two key/value pairs.

  region->put("rtimmons", "Robert Timmons");
  region->put("scharles", "Sylvia Charles");

Next, the application retrieves the stored values using Get operations.

  auto user1 = region->get("rtimmons");
  auto user2 = region->get("scharles");

Finally, the application deletes one of the stored values using the Remove method.

  if (region->existsValue("rtimmons")) {
    std::cout << "rtimmons's info not deleted" << std::endl;
  } else {
    std::cout << "rtimmons's info successfully deleted" << std::endl;
  }