Click here to Skip to main content
15,884,472 members
Articles / Web Development / ASP.NET

MyCache: Distributed caching engine for an ASP.NET web farm - Part I: The demonstration

Rate me:
Please Sign up or sign in to vote.
4.85/5 (39 votes)
14 Dec 2010CPOL18 min read 94.1K   1.3K   102  
Demonstration of a distributed caching engine for ASP.NET applications deployed under a load-balanced environment in a web farm, which is built on top of WCF and the Microsoft Caching Application Block.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MyCacheAPI;
using System.Web.Caching;
using System.IO;
using System.Text;
using System.Data.SqlClient;

namespace DistributedCache
{
    public partial class _Default : System.Web.UI.Page
    {
        MyCache cache = new MyCache();

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// Load data from Cache, or read data and store in cache
        /// </summary>
        protected void LoadData()
        {
            lblWebFarm.Text = ConfigurationManager.AppSettings["WebFarmId"];
            string strData = cache.Get("Key") as string;
            if (strData == null)
            {
                divCacheMiss.Visible = true;

                string dependencyFilePath = GetDependencyFilePath();

                DateTime startTimeForCache = DateTime.Now;

                object Value = GetObjectFromFile(dependencyFilePath);

                DateTime endTimeForCache = DateTime.Now;
                cache.Add("Key", Value, dependencyFilePath, Cache.NoAbsoluteExpiration, new TimeSpan(0, 5, 10), CacheItemPriority.Normal, new CacheItemRemovedCallback(onRemoveCallback));
                lblData2.Text = Value.ToString();
            }
            else
            {
                divCacheHit.Visible = true;
                DateTime startTimeForCache = DateTime.Now;
                StringBuilder sbOutput = new StringBuilder();

                string data = cache.Get("Key").ToString();

                DateTime endTimeForCache = DateTime.Now;

                lblCacheRetrievalTime.Text = string.Format("Retrieved following data in <b>{0}</b> milliseconds", (endTimeForCache - startTimeForCache).TotalMilliseconds);
                lblData.Text = data;
            }

        }

        /// <summary>
        /// Get dependency file location
        /// </summary>
        /// <returns></returns>
        private string GetDependencyFilePath()
        {
            string dependencyFilePath = Path.Combine(Path.Combine(Path.GetDirectoryName(Server.MapPath("")), "Data"), "Test.txt");
            return dependencyFilePath;
        }

        /// <summary>
        /// Callback function to re-insert the item in cache if cache dependency changed
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Value"></param>
        /// <param name="reason"></param>
        protected void onRemoveCallback(string Key, object Value, CacheItemRemovedReason reason)
        {
            if (cache == null)
            {
                cache = new MyCache();
            }
            if (reason == CacheItemRemovedReason.DependencyChanged)
            {
                //Aquire lock on MyCache service for the Key and proceed only if
                //no lock is currently set for this Key. This has been done to prevent
                //multiple load-balanced web application update the same data on MyCache service
                //sumultaneously when the underlying file content is modified
                if (cache.SetLock(Key))
                {
                    string dependencyFilePath = GetDependencyFilePath();
                    object modifiedValue = GetObjectFromFile(dependencyFilePath);
                    cache.Add(Key, modifiedValue, dependencyFilePath, Cache.NoAbsoluteExpiration, new TimeSpan(0, 5, 60), CacheItemPriority.Normal, new CacheItemRemovedCallback(onRemoveCallback));
                    //Release lock when done
                    cache.ReleaseLock(Key);
                }

            }
            if (reason == CacheItemRemovedReason.Expired || reason == CacheItemRemovedReason.Removed)
            {
                if (cache.Exists(Key))
                {
                    cache.Remove(Key);
                }
            }

        }

        /// <summary>
        /// Read data from File
        /// </summary>
        /// <param name="dependencyFilePath"></param>
        /// <returns></returns>
        private object GetObjectFromFile(string dependencyFilePath)
        {
            object updatedValue = File.ReadAllText(dependencyFilePath);
            return updatedValue;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder SmartAspects
Bangladesh Bangladesh
I write codes to make life easier, and that pretty much describes me.

Comments and Discussions