Example #1
0
        public string Invoke()
        {
            IIgnite ignite = Ignition.TryGetIgnite("TRex");

            if (ignite != null)
            {
                ICache <string, MyCacheClass> cache = ignite.GetCache <string, MyCacheClass>("TestCache");
                MyCacheClass c = cache.Get(s);
                return("Affinity: " + c.name);
            }
            else
            {
                Console.WriteLine("Unable to get Ignite instance with Ignition.TryGetIgnite()");
                return("Affinity: <Error: No Ignite>");
            }

            //return "Affinity: " + Ignition.TryGetIgnite().GetCache<String, MyCacheClass>("TestCache").Get(s).name;
        }
Example #2
0
        static void Main(string[] args)
        {
            string   s;
            DateTime startTime;
            DateTime endTime;

            IgniteConfiguration cfg = new IgniteConfiguration()
            {
                IgniteInstanceName = "TRex",

                // Register custom class for Ignite serialization
                BinaryConfiguration = new Apache.Ignite.Core.Binary.BinaryConfiguration(typeof(MyCacheClass))
            };

            IIgnite ignite = Ignition.Start(cfg);

            // Add a cache to Ignite
            ICache <string, MyCacheClass> cache = ignite.CreateCache <string, MyCacheClass>
                                                      (new CacheConfiguration()
            {
                Name                      = "TestCache",
                CopyOnRead                = false,
                KeepBinaryInStore         = false,
                CacheStoreFactory         = new TRexCacheStoreFactory(),
                ReadThrough               = true,
                WriteThrough              = true,
                WriteBehindFlushFrequency = new TimeSpan(0, 0, 5),     // 5 seconds
                EvictionPolicy            = new LruEvictionPolicy()
                {
                    MaxMemorySize = 1000000,
                }
            });

            int NumCacheEntries = 5000;

            // Add a cache item
            cache.Put("First", new MyCacheClass("FirstItem"));

            // Add a collectikon of items
            startTime = DateTime.Now;
            for (int i = 0; i < NumCacheEntries; i++)
            {
                cache.Put("First" + i, new MyCacheClass("First" + i));
            }
            endTime = DateTime.Now;

            s = string.Format("{0}", endTime - startTime);
            Console.WriteLine("Time to add cache items with serialisation: {0}", s);

            int sumsum = 0;

            // Query back the cache items with serialisation
            startTime = DateTime.Now;
            for (int i = 0; i < NumCacheEntries; i++)
            {
                MyCacheClass first = cache.Get("First" + i);

                int sum = 0;
                for (int ii = 0; ii < first.localData.Length; ii++)
                {
                    sum += first.localData[ii];
                }
                sumsum += sum;
            }
            endTime = DateTime.Now;

            s = string.Format("{0}", endTime - startTime);
            Console.WriteLine("Time to query cache items with serialisation: {0}, sum = {1}", s, sumsum);

            var binCache = cache.WithKeepBinary <string, IBinaryObject>();

            //            IBinaryObject binCacheItem = binCache["First"];
            //            Console.WriteLine(binCacheItem.GetField<string>("Name"));

            // Query back the cache items without serialisation (using BinaryObject)
            startTime = DateTime.Now;
            for (int i = 0; i < NumCacheEntries; i++)
            {
                IBinaryObject binCacheItem = binCache["First" + i];

                byte[] bytes = binCacheItem.GetField <byte[]>("localData");

                int sum = 0;
                for (int ii = 0; ii < bytes.Length; ii++)
                {
                    sum += bytes[ii];
                }
                sumsum += sum;
            }
            endTime = DateTime.Now;

            s = string.Format("{0}", endTime - startTime);
            Console.WriteLine("Time to query cache items without serialisation: {0}, sum = {1}", s, sumsum);

            // Get compute instance over all nodes in the cluster.
            ICompute      compute  = ignite.GetCompute();
            IClusterGroup compute2 = ignite.GetCompute().ClusterGroup;



            // Execute a map reduce on the cluster
            if (compute2.ForServers()?.GetNodes()?.Any() == true)
            {
                try
                {
                    string mapReduceResult = ignite.GetCompute().Execute <string, string, string>(new MyComputeTask(), "Bob");
                    Console.WriteLine("Mapreduce result = '{0}'", mapReduceResult);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception executing mapReduce execution\r\n: {0}", e);
                }
            }
            else
            {
                Console.WriteLine("Calling cluster mapReduce broadcast function: No servers present in cluster");
            }

            // Execute a command using affinity on the cluster
            try
            {
                string affinityResult = ignite.GetCompute().AffinityCall <string>("TestCache", "First", new AffinityComputeFunc("First"));
                Console.WriteLine("Affinity result = '{0}'", affinityResult);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception executing affinity execution\r\n: {0}", e);
            }

            if (ignite == null)
            {
                Console.WriteLine("Ignite instance is null at end of method");
            }
            Console.ReadKey();
        }