// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Collections.Generic; using System.Linq; using System.Threading; using Hazelcast.Core; using Hazelcast.Client; namespace Cache.Hazelcast.Functional { public class MyEntryAddedListener : EntryAddedListener { public void EntryAdded(EntryEvent ee) { Console.WriteLine("Add : {0} = {1}", ee.GetKey(), ee.GetValue()); } } public class MyEntryRemovedListener : EntryRemovedListener { public void EntryRemoved(EntryEvent ee) { Console.WriteLine("Remove : {0}", ee.GetKey()); } } public class Program { private static void Test(IMap cache) { // basic put, get and remove cache.Put("v", 123); Console.WriteLine(cache.Get("v")); cache.Remove("v"); Console.WriteLine(cache.Get("v")); cache.Put("v", 123); Console.WriteLine(cache.Get("v")); // persistence int? counter = cache.Get("counter"); if(counter == null) { counter = 0; } counter++; Console.WriteLine(counter); cache.Put("counter", counter); // list all foreach(KeyValuePair kvp in cache.EntrySet()) { Console.WriteLine("{0} = {1}", kvp.Key, kvp.Value); } // listener cache.AddEntryListener(new MyEntryAddedListener(), true); cache.AddEntryListener(new MyEntryRemovedListener(), true); cache.Put("x", 123); cache.Remove("x"); Thread.Sleep(10); // ensure sufficient time to call listener } public static void Main(string[] args) { IHazelcastInstance cli = HazelcastClient.NewHazelcastClient(); IMap cache = cli.GetMap("functional_cache"); Test(cache); cli.Shutdown(); } } }