How to use caching in applicationstart phase?
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 开发者_StackOverflowwhen 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,
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.
精彩评论