Click here to Skip to main content
15,885,366 members
Articles / Web Development

MyCache: Distributed caching engine for an ASP.NET web farm - Part II: The internal details

Rate me:
Please Sign up or sign in to vote.
4.97/5 (20 votes)
27 Dec 2010CPOL17 min read 80.4K   1.2K   54  
Internal implementation of MyCache: A distributed caching engine for ASP.NET applications deployed under a load-balanced environment in web farms, 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 ViewPerformance : System.Web.UI.Page
    {
        MyCache cache = new MyCache();

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

        /// <summary>
        /// Load data from Cache, or read data and store in cache
        /// </summary>
        protected void LoadDataFromMyCache()
        {
            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);
            }
            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 2KB data <b>Single time</b> from MyCache in <b>{0}</b> milliseconds", (endTimeForCache - startTimeForCache).TotalMilliseconds);

                startTimeForCache = DateTime.Now;

                for (int i = 0; i < 100; i++)
                {
                    data = cache.Get("Key").ToString();
                }

                endTimeForCache = DateTime.Now;

                lblCacheRetrievalTimeFor100Data.Text = string.Format("Retrieved 2KB data <b>100 times</b> from MyCache in <b>{0}</b> milliseconds", (endTimeForCache - startTimeForCache).TotalMilliseconds);

                for (int i = 0; i < 1000; i++)
                {
                    data = cache.Get("Key").ToString();
                }

                endTimeForCache = DateTime.Now;

                lblCacheRetrievalTimeFor1000Data.Text = string.Format("Retrieved 2KB data <b>1000 times</b> from MyCache in <b>{0}</b> milliseconds", (endTimeForCache - startTimeForCache).TotalMilliseconds);

            }

        }

       
        /// <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>
        /// 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