LATEST VERSION: 8.1.0 - CHANGELOG
Pivotal GemFire® v8.1

API Programming Example – C#

API Programming Example – C#

This C# programming code in the next example demonstrates how to use two or more clients sharing a distributed region in a GemFire cache.

Figure 1. Gets and Puts (C# .NET)
using System;
 
// Use the GemFire namespace
using GemStone.GemFire.Cache;
 
namespace GemStone.GemFire.Cache.QuickStart
{
  // The BasicOperations QuickStart example.
  class BasicOperations
  {
    static void Main(string[] args)
    {
      try
        {
          // Create a GemFire Cache.
          CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(null);
 
          Cache cache = cacheFactory.SetSubscriptionEnabled(true).Create();
 
          Console.WriteLine("Created the GemFire Cache");
 
          RegionFactory regionFactory = 
              cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY);
 
          Region region = regionFactory.Create("exampleRegion");
 
          Console.WriteLine("Created the Region Programmatically.");
 
          // Put an Entry (Key and Value pair) into the Region using the 
          // direct/shortcut method.
          region.Put("Key1", "Value1");
 
          Console.WriteLine("Put the first Entry into the Region");
 
          // Put an Entry into the Region by manually creating a Key and 
          // a Value pair.
          CacheableInt32 key = new CacheableInt32(123);
          CacheableString value = new CacheableString("123");
          region.Put(key, value);
 
          Console.WriteLine("Put the second Entry into the Region");
 
          if (IntPtr.Size == 8) // Are we a 64 bit process?
            {
              Char ch = 'A';
              string text = new string(ch, 1024 * 1024);
              for (int item = 0; item < (5 * 1024 /* 5 GB */); item++)
                {
                  region.LocalPut(item, text);
                }
              Console.WriteLine("Put over 4 GB data locally");
            }
          // Get Entries back out of the Region.
          IGFSerializable result1 = region.Get("Key1");
 
          Console.WriteLine("Obtained the first Entry from the Region");
 
          IGFSerializable result2 = region.Get(key);
 
          Console.WriteLine("Obtained the second Entry from the Region");
 
          // Invalidate an Entry in the Region.
          region.Invalidate("Key1");
 
          Console.WriteLine("Invalidated the first Entry in the Region");
 
          // Destroy an Entry in the Region.
          region.Destroy(key);
 
          Console.WriteLine("Destroyed the second Entry in the Region");
 
          // Close the GemFire Cache.
          cache.Close();
 
          Console.WriteLine("Closed the GemFire Cache");
        }
      // An exception should not occur
      catch (GemFireException gfex)
        {
          Console.WriteLine("BasicOperations GemFire Exception: {0}", 
              gfex.Message);
        }
    }
  }
}