Justin Mathew Blog

My Innovations in .Net

Archive for the ‘Caching’ Category

Caching in .Net 4.0

with 3 comments


.Net 4.0 “Memory Cache” can be used in Web and Non Web applications

Finally Microsoft has come up with an independent caching module in .Net 4.0 called “Memory Cache” and it cleared lots of confusion among all Techies. If we look at earlier versions of .Net, caching provided in “System.web.caching” and it meant to be used only for ASP.Net web applications and also caching accessing from “Http Runtime.Cache”.  This is really confused everybody using the ASP.NET Cache in Non-Web Applications and nobody was able to give an answer.

Before jumping into new cash let me give an introduction about  caching technologies so far available in .Net until 4.0 is released and I will say all that is obsolete if you decided to go with .Net framework 4.0.

ASP.NET Caching:  System.Web.Caching.Cache (obsolete)

This caching works perfectly in an ASP.NET Web application and it is part of Web framework since it is provided in “System.Web”. Like other cash mechanisms even here data will be managed with some kind of static objects and other cache policies. ASP.Net data can be read from the static property “Cache” of “http Runtime” and there will be only one instance per application domain.

Using the ASP.NET Cache in Non-Web Applications

This is the same confusion I talked beginning of this article. First of all using ASP.Net caching is not recommended in any other Non-Web applications because System.web is big library and there will be a heavy performance issue if same library using  to keep some objects in memory as it suppose to do additional process since it is as part of Web Application framework.

Enterprise Library Caching block (obsolete)

                This is one of the way caching can be used in any applications like web and non web applications. Even this library is heavy it terms of applications want to use only caching objects. This library comes up with lots of additional features and all that is available in .Net 4.0 caching.

Memory Cache : System.Runtime.Caching ( New One)

Here is the latest one which anybody can use in Non Web applications. It provides same functionality as ASP.NET Cache provides and main difference is  that this classes  has been changed to make it usable by .NET Framework applications that are not ASP.NET applications. For example, the Memory Cache class has no dependencies on the System.Web assembly. Another difference is that you can create multiple instances of the Memory Cache class for use in the same application and in the same App domain instance.

Below is the sample Code

public static OrderConfiguration GetOrderConfig(int salesOrderNumber)       
        {            
            OrderConfiguration defaultValues = null;            
            try            
            {              
                ObjectCache defaultValuesCache = MemoryCache.Default;  
                DictionaryOrderConfiguration> cacheContents = defaultValuesCache["ORDERDEFAULTS"] as Dictionary;    
                if (cacheContents != null) defaultValues = cacheContents[salesOrderNumber];  
                if (defaultValues != null) return defaultValues;    
                else if (cacheContents == null || defaultValues == null)          
                {                    
                    SapConfigDB configDB = new SapConfigDB();  
                    cacheContents = new Dictionary();       
                    foreach (OrderConfiguration config in configDB.OrderConfigurations) 
                    {                        
                        cacheContents.Add(config.Sales_Org, config);               
                    }                    
                    CacheItemPolicy policy = new CacheItemPolicy();  
                    policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(int.Parse(ConfigurationManager.AppSettings["CacheExpiryMinutes"])); 
                    defaultValuesCache.Set(“ORDERDEFAULTS”, cacheContents, policy);    
                    defaultValues = cacheContents[salesOrderNumber];         
                }        
            } catch (Exception ex)   
            {              
                throw new Exception(“Caching default values error”, ex);   
            }           
            return defaultValues;       
        }

Written by Justin

November 15, 2010 at 4:26 pm