Ejemplo n.º 1
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);
			}
		}
Ejemplo n.º 2
0
        /// <summary>
        /// Method for printing customer type details.
        /// </summary>
        /// <param name="customer"></param>
		public static void PrintCustomerDetails(Customer customer)
		{
			Console.WriteLine();
			Console.WriteLine("Customer Details are as follows: ");
			Console.WriteLine("Name: " + customer.Name);
			Console.WriteLine("Age: " + customer.Age);
			Console.WriteLine("Gender: " + customer.Gender);
			Console.WriteLine("Contact No: " + customer.ContactNo);
			Console.WriteLine("Address: " + customer.Address);
		}
Ejemplo n.º 3
0
        /// <summary>
        /// Method for printing detials of customer type.
        /// </summary>
        /// <param name="customer"></param>
		private static void printCustomerDetails(Customer customer)
        {
            if (customer == null) return;
            
            Console.WriteLine();
			Console.WriteLine("Customer Details are as follows: ");
			Console.WriteLine("Name: " + customer.Name);
			Console.WriteLine("Age: " + customer.Age);
			Console.WriteLine("Gender: " + customer.Gender);
			Console.WriteLine("Contact No: " + customer.ContactNo);
			Console.WriteLine("Address: " + customer.Address);
			Console.WriteLine();
		}
Ejemplo n.º 4
0
		public static void Main(string[] args)
		{
			try
			{
				//Initialize cache via 'initializeCache' using 'Cache Name'
				//to be initialized. 
				Cache cache = NCache.InitializeCache("mypartitionedcache");
				cache.Clear();

				//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);
			}
		}