1

I am really confused to use caching. In ASP.NET Cache the cache object is global so as I understand we can reach it everywhere. But when I looked at the caching application block how can I use a cache that I created on the application_start phase. What is the strategy to use a cache that I created on application_start ?

Thanks in advance,

3
  • do you want to use Application Blocks absolutely or ASP.NET Caching would also be enough? I do not understand your question... I think you should read this: stackoverflow.com/questions/21870/… Sep 18, 2011 at 19:17
  • @Davide Piras I read it before opened this question. I want to use application blocks but lets assume I queried database and put it on a cache in the application_start. Then for example how can I use it on a business layer or on any class on the project ? Sep 18, 2011 at 19:25
  • show us the way you put objects in the cache in the application_start :-) Sep 18, 2011 at 19:33

1 Answer 1

1

once you have created the proper configuration snippet for your caching block and added to the web.config of the ASP.NET application, you can add items to the cache in the same way from anywhere in the asp.net application. Similarly, from a business or service layer which shares the same configuration snippet in its app.config or web.config you should be able to retrieve items from the cache.

This is well explained here: Exploring Caching : Using Caching Application Enterprise Library 4.1

so just try to create and use the ICacheManager, for example in this way:

//Create Instance of CacheManager 
ICacheManager objCacheManager = CacheFactory.GetCacheManager();    

//Add a new CacheItem to Cache
objCacheManager.Add("YourKey", yourObject);

then from another project or web service running on that IIS but as another application, if the web.config contains the same snippet to configure caching, use this:

//Create Instance of CacheManager 
ICacheManager objCacheManager = CacheFactory.GetCacheManager();

// Check If Key is in Cache Collection
if(objCacheManager.Contains("YourKey"))
{
  var myObject = objCacheManager.GetData("YourKey");
}

you should in fact imagine this to be down at lower level in the application architecture, if you are loading data from the database via Business Logic, I imagine that piece of BL will retrieve from database and add to the cache then in next query will check if it exists in the cache and if not will load again from database.

For an example of configuration snippet check the link I mentioned above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.