Exemple #1
0
        public async Task <Author> GetAuthor(int id)
        {
            _cache = NCache.InitializeCache("CacheName");
            var    cacheKey = "Key";
            Author author   = null;

            if (_cache != null)
            {
                author = _cache.Get(cacheKey) as Author;
            }
            if (author == null) //Data not available in the cache
            {
                //Write code here to fetch the author
                // object from the database
                if (author != null)
                {
                    if (_cache != null)
                    {
                        _cache.Insert(cacheKey, author, null,
                                      Alachisoft.NCache.Web.Caching.Cache.NoAbsoluteExpiration,
                                      TimeSpan.FromMinutes(10),
                                      Alachisoft.NCache.Runtime.
                                      CacheItemPriority.Default);
                    }
                }
            }
            return(author);
        }
        public static void Main(string[] args)
        {
            try
            {
                //Initialize cache via 'initializeCache' using 'Cache Name'
                //to be initialized.
                Cache cache = NCache.InitializeCache("mypartitionedcache");

                //Another method to add item(s) to cache is via CacheItem  object
                Customer customer = new Customer();
                customer.Name      = "David Johnes";
                customer.Age       = 23;
                customer.Gender    = "Male";
                customer.ContactNo = "12345-6789";
                customer.Address   = "Silicon Valley, Santa Clara, California";

                DateTime calendar = new DateTime();
                calendar.AddMinutes(1);

                //Adding item with an absolute expiration of 1 minute
                cache.Add("Customer:DavidJohnes", customer, calendar, Cache.NoSlidingExpiration, CacheItemPriority.Normal);
                Customer cachedCustomer = (Customer)cache.Get("Customer:DavidJohnes");

                printCustomerDetails(cachedCustomer);

                //updating item with a sliding expiration of 30 seconds
                customer.Age = 50;
                cache.Insert("Customer:DavidJohnes", customer, Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 30), CacheItemPriority.Normal);

                //get customer from the cache
                cachedCustomer = (Customer)cache.Get("Customer:DavidJohnes");
                printCustomerDetails(cachedCustomer);


                //Total item count in the cache
                long count = cache.Count;
                Console.WriteLine("Cache Count: " + count);

                //remove the existing customer
                cache.Remove("Customer:DavidJohnes");

                //try to get the customer again, getting non-existing items returns null
                cachedCustomer = (Customer)cache.Get("Customer:DavidJohnes");
                printCustomerDetails(cachedCustomer);

                //get count again, item count should be 0
                count = cache.Count;
                Console.WriteLine("Cache Count: " + count);

                //Dispose the cache once done
                cache.Dispose();

                Environment.Exit(0);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Environment.Exit(0);
            }
        }
Exemple #3
0
        public static void Main(string[] args)
        {
            try
            {
                //Initialize cache
                Cache cache;
                cache = NCache.InitializeCache("mypartitionedcache");
                cache.Clear();

                //Locking prevents multiple clients from updating the same data simultaneously
                //and also provides the data consistency.

                //Adding an item the cache
                Customer customer = new Customer();
                customer.Name      = "Kirsten Goli";
                customer.Age       = 40;
                customer.Address   = "45-A West Boulevard, Cartago, Costa Rica";
                customer.Gender    = "Female";
                customer.ContactNo = "52566-1779";

                cache.Add("Customer:KirstenGoli", customer);

                //Get
                TimeSpan   timeSpan    = new TimeSpan(0, 0, 0, 20);
                LockHandle lockHandle  = new LockHandle();
                Customer   getCustomer = (Customer)cache.Get("Customer:KirstenGoli", timeSpan, ref lockHandle, true);

                PrintCustomerDetails(getCustomer);

                Console.WriteLine("Lock acquired on " + lockHandle.LockId);

                //Lock item in cache
                bool isLocked = cache.Lock("Customer:KirstenGoli", timeSpan, out lockHandle);

                if (!isLocked)
                {
                    Console.WriteLine("Lock acquired on " + lockHandle.LockId);
                }

                //Unlock item in cache
                cache.Unlock("Customer:KirstenGoli");

                //Unlock via lockhandle
                cache.Unlock("Customer:KirstenGoli", lockHandle);

                //Must dispose cache
                cache.Dispose();

                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Environment.Exit(0);
            }
        }
Exemple #4
0
 protected override void InitialiseInternal()
 {
     if (_cacheName == null)
     {
         _cacheName = CacheConfiguration.Current.DefaultCacheName;
         Log.Debug("NCacheExpress.Initialise - initialising with cacheName: {0}", _cacheName);
         NoAbsoluteExpiry = DateTime.MaxValue;
         NoSlidingExpiry  = new TimeSpan(0);
         NCache.InitializeCache(_cacheName);
     }
 }
Exemple #5
0
        private static bool InitializeCache()
        {
            string cacheName = ConfigurationSettings.AppSettings.Get("CacheName").ToString();

            try
            {
                _cache = NCache.InitializeCache(cacheName);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error occured while trying to initialize Cache named: [" + cacheName + "] \n"
                                  + "Exception: " + e.Message);
                return(false);
            }
        }
Exemple #6
0
        public static void Main(string[] args)
        {
            try
            {
                //Initialize cache
                Cache cache = NCache.InitializeCache("mypartitionedcache");

                Customer customer1 = new Customer();
                customer1.Name      = "john viking";
                customer1.Address   = "10 downing streat";
                customer1.ContactNo = "25-555-265";
                customer1.Gender    = "male";
                customer1.Age       = 59;
                cache.Add("Item:1", customer1);

                //Key based notifications allows item notifications for particular
                //items in cache

                CacheDataNotificationCallback myMethod = new CacheDataNotificationCallback(KeyNotificationMethod);
                cache.RegisterCacheNotification("Item:1", myMethod, EventType.ItemAdded | EventType.ItemRemoved | EventType.ItemUpdated);


                //value for customer changed for notifications
                customer1.name = "sophie silver";
                //See output

                cache.Insert("Item:1", customer1);

                //See output

                cache.Delete("Item:1");

                Console.WriteLine();

                //Must dispose cache
                cache.Dispose();

                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Environment.Exit(0);
            }
        }
Exemple #7
0
        public static void Main(string[] args)
        {
            try
            {
                //Initialize cache
                Cache cache;
                cache = NCache.InitializeCache("mypartitionedcache");
                cache.Clear();

                //See output

                cache.Add("Item:1", "Value:1");

                //Key based notifications allows item notifications for particular
                //items in cache

                CacheDataNotificationCallback myMethod = new CacheDataNotificationCallback(KeyNotificationMethod);
                cache.RegisterCacheNotification("Item:1", myMethod, EventType.ItemAdded | EventType.ItemRemoved | EventType.ItemUpdated, EventDataFilter.None);

                //See output

                cache.Insert("Item:1", "Value:1-------Changed for notifications");

                //See output

                cache.Delete("Item:1");

                Console.WriteLine();

                //Must dispose cache
                cache.Dispose();

                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Environment.Exit(0);
            }
        }
Exemple #8
0
        public static void Main(string[] args)
        {
            try
            {
                Cache cache;
                cache = NCache.InitializeCache("mypartitionedcache");
                cache.Clear();

                string[] keysToAdd = new string[] { "Product:Cheese", "Product:Butter", "Product:Cream", "Product:Youghart" };

                Product[] products = new Product[4];

                products[0] = new Product(1, "Dairy Milk Cheese", "ClassA", 1);
                products[1] = new Product(2, "American Butter", "ClassA", 1);
                products[2] = new Product(3, "Walmart Delicious Cream", "ClassA", 2);
                products[3] = new Product(4, "Nestle Youghart", "ClassA", 2);

                //Adding Bulk Items
                //Bulk operation returns
                IDictionary result = cache.AddBulk(keysToAdd, new CacheItem[] { new CacheItem(products[0]), new CacheItem(products[1]), new CacheItem(products[2]), new CacheItem(products[3]) });

                if (result.Count == 0)
                {
                    Console.WriteLine("All items are successfully added to cache.");
                }
                else
                {
                    Console.WriteLine("One or more items could not be added to the cache. Iterate hashmap for details.");
                    //This is how to iterate the hashmap
                    for (IEnumerator iter = result.Values.GetEnumerator(); iter.MoveNext();)
                    {
                        Product product = (Product)iter;
                        PrintProductDetails(product);
                    }
                }

                //Getting bulk items

                IDictionary items = cache.GetBulk(keysToAdd);

                if (items.Count > 0)
                {
                    for (IEnumerator iter = items.Values.GetEnumerator(); iter.MoveNext();)
                    {
                        Product product = (Product)iter.Current;
                        PrintProductDetails(product);
                    }
                }

                //Updating Bulk Items
                //Previous products have their product id, class and category changed.
                products[0].ClassName = "ClassB";
                products[1].ClassName = "ClassC";
                products[2].ClassName = "ClassA";
                products[3].ClassName = "ClassD";

                result = cache.InsertBulk(keysToAdd, new CacheItem[] { new CacheItem(products[0]), new CacheItem(products[1]), new CacheItem(products[2]), new CacheItem(products[3]) });

                if (result.Count == 0)
                {
                    Console.WriteLine("All items successfully added/updated in cache.");
                }
                else
                {
                    Console.WriteLine("One or more items could not be added to the cache. Iterate hashmap for details.");
                    //This is how to iterate the hashmap
                    for (IEnumerator iter = result.Values.GetEnumerator(); iter.MoveNext();)
                    {
                        Product product = (Product)iter;
                        PrintProductDetails(product);
                    }
                }

                items = cache.GetBulk(keysToAdd);

                if (items.Count > 0)
                {
                    for (IEnumerator iter = items.Values.GetEnumerator(); iter.MoveNext();)
                    {
                        Product product = (Product)iter.Current;
                        PrintProductDetails(product);
                    }
                }

                //Remove Bulk operation
                //Remove returns all the items removed from the cache in them form of Hashmap

                result = cache.RemoveBulk(keysToAdd);

                if (result.Count == 0)
                {
                    Console.WriteLine("No items removed from the cache against the provided keys.");
                }
                else
                {
                    Console.WriteLine("Following products have been removed from the cache:");
                    //This is how to iterate the hashmap
                    for (IEnumerator iter = result.Values.GetEnumerator(); iter.MoveNext();)
                    {
                        Product product = (Product)iter.Current;
                        PrintProductDetails(product);
                    }
                }

                //Delete Bulk from Cache is same as Remove
                //Delete bulk does not return anything

                cache.DeleteBulk(keysToAdd);

                Console.WriteLine("Cache contains " + cache.Count + " items.");

                //Must dispose cache
                cache.Dispose();

                Environment.Exit(0);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Environment.Exit(0);
            }
        }
Exemple #9
0
 static MvcApplication()
 {
     cache = NCache.InitializeCache("AspNetDataCache");
 }
Exemple #10
0
 protected NCacheStorageBase(string cacheName)
 {
     Storage = NCache.InitializeCache(cacheName);
 }