DataCaching using System.Runtime.Caching using WCF / Cache in DB layer/ Caching in Database level

Here is an interesting topic about doing caching for result sets returned from the requests.

I m providing a 2 way caching mechanism, 
First * is using "System.Runtime.Caching" in WCF LAYER
Second * is using "System.Web" in DATA LAYER, which will only work for web applications, so when some cached content is missing in WCF, we can make the cache work perfect in DATA LAYER (But the restriction is its works for web applications)

Explanation:

             Attached application contains 4 projects, one is a class library acting for database interactions, next is a WCF Service provider(WCF ServiceApplication), next is a Website(which consumes the WCF Services provided) and other is an Windows application(which consumes the WCF Services provided)

01. DataCaching (Class Library): 
            Use System.Runtime.Serialization (to write the DataContract & Service Contracts which we are going to use in WCF services Ex: Customer Entity )
            Implement all methods which do the database transactions, here I have 2 methods, "GetCustomerList()", "AddCustomer(_cust)"
            After implementing it just build and put it on hold.

02. RequestHandler (WCF Service Application):
            Use System.Runtime.Caching to make use of data caching happens at WCF layer only dont let it move to next layer if your requested content is cached.
            
            Prepare a service file with an interface which serves the two action methods defined in DataCaching namespace, i.e GetAGetCustomerList llCustomers & AddCustomer,
            In GetCustomerList, you need to provide a mechanism for caching, in the given name space you have a class called "ObjectCache", which is to store the content of cached objects, 
 
      var myList ;
      {
           ObjectCache myCache = MemoryCache.Default;
             if( myCache.Get("Cache_Key") == null)
                 get the information from database
             else
                 myCache.Get("Cache_Key",out myList);
       }
       return myList; 
              Publish your service and try using the service in windows and web applications, it will work with cache.


03. Web Application
              ServiceModel.ServiceClient obj = new --();
              Grid.DataSource = obj.GetCustomerList();
              Grid.DataBind();

 open the same request in a browser and see the result, delete a record from database and open the same link in another browser, it will fetch the information from cache and shows you the total content which showing in previous page.

04. Windows Applications (Acts as a consumer to WCF service)
              same like web app, get the service reference, have a grid and bind the content.

05. Make cache invalidate (********) IMP
               here is a tricky thing keeps in mind while doing caching is, when ever user adds/ edits/ deletes the things related to cached content in the database, you need to clear/refresh your cache, so that the modifications will affect and users can able to see the updates with in no time.


Note:
01. if you have any error in importing service library or while consuming the requests in web application/windows application, like "target framework is not supported, you need to remove the assembly defined to the application "System.Runtime.Caching", for this you just need to go the application properties of your current Project and choose ".Net Framework 4 Client Profile" as Target Framework, and rebuild.

02. if there is any error saying, Login failed for user "IIS APPPOOL\ASP .NET V4.0", you just need to do the following
-- just go to your IIS, Apppools- Set Applicationpool Defaults- Process model -identity - to : LOCAL SYSTEM
for more info about this issue click here 

For More info about DataCaching