Saturday, June 21, 2014

MemoryCache

If you ever need a in-memory cache to be maintained, then use MemoryCache.

It is an in memory object cache which uses a key to identify the item in the cache. From the first look it sounds like a dictionary.

Look at the sample code below :

Code
  1. using System;
  2. using System.Runtime.Caching;
  3.  
  4. namespace MemoryCacheExample
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             var myCache = new MemoryCache("SampleCache");
  11.  
  12.             var cacheItemPolicy = new CacheItemPolicy();
  13.             myCache.Add(new CacheItem(
  14.                             "FOO", new Contact
  15.                                 {
  16.                                     Address = new Address
  17.                                         {
  18.                                             AddressLine1 = "street 1",
  19.                                             AddressLine2 = "Area 1",
  20.                                             City = "Bangalore",
  21.                                             Country = "India",
  22.                                             State = "Kar",
  23.                                             ZipCode = 1243435
  24.                                         },
  25.                                     Id = 34,
  26.                                     Name = "Vikram"
  27.                                 }), cacheItemPolicy);
  28.  
  29.  
  30.             var retrievedItemFromCache = (Contact)myCache.Get("FOO");
  31.  
  32.             PrintContact(retrievedItemFromCache);
  33.         }
  34.  
  35.         private static void PrintContact(Contact contact)
  36.         {
  37.             Console.WriteLine("Name : {0}", contact.Name);
  38.             Console.WriteLine("Id : {0}", contact.Id);
  39.             Console.WriteLine("Address1 : {0}", contact.Address.AddressLine1);
  40.             Console.WriteLine("Address2 : {0}", contact.Address.AddressLine2);
  41.             Console.WriteLine("City : {0}", contact.Address.City);
  42.             Console.WriteLine("State : {0}", contact.Address.State);
  43.             Console.WriteLine("Country : {0}", contact.Address.Country);
  44.         }
  45.     }
  46.  
  47.     public class Contact
  48.     {
  49.  
  50.         public Contact()
  51.         {
  52.             Address = new Address();
  53.         }
  54.         public int Id { get; set; }
  55.         public string Name { get; set; }
  56.         public Address Address { get; set; }
  57.     }
  58.  
  59.     public class Address
  60.     {
  61.         public string AddressLine1 { get; set; }
  62.         public string AddressLine2 { get; set; }
  63.         public string City { get; set; }
  64.         public string State { get; set; }
  65.         public string Country { get; set; }
  66.         public long ZipCode { get; set; }
  67.     }
  68. }


However memory cache provides some advantages.
The MemoryCache has an eviction strategy, so that it can discard objects that are no longer needed or for that you do not have enough memory anymore.

MemoryCache is thread-safe and has methods such as AddOrGetExisting. Doing things yourself in a dictionary or concurrent dictionary is tedious.


No comments:

Post a Comment