Caching in Asp.net Application

by daz 3/14/2012 8:58:00 PM

ASP.NET came up with a very powerful feature of in-memory object cache in it earlier days. In asp.net 4.0 they introduce the notion of MemoryCache, but yet has to be proven.

The good thing with the first one is that if you have a website you can make your site run faster by caching some data. On the other site this was only advisable for web applicatioms. . It was designed and tested for use in ASP.NET to provide caching for Web applications. In other types of applications, such as console applications or Windows Forms applications, ASP.NET caching might not work correctly.” http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx

While creating a webservice or simply a WCF , in some reason, you may be tented to use caching. You can the create your customized cache engine that will take care of your memory management. 

In my example below, which is straight forward, you will be able to cache object by reference type and by value type. Let say you have a list of the 10 most sale products, that you want to show to a customer that just come in your big retail store, you can just save that list of type PRODUCT with a key top10product on the fly. You may be able to save a specific parameters or any value type like the normale cache. I did not had a threat safe scenario by locking and unlocking but it is also easy since a lot of people have already done this for you..

 

Insert code here...  public static class RWCache
    {
        //create a static object for storing the cache
        private static Hashtable _items = new Hashtable();

       

        /// <summary>
        /// Insert value into the cache using
        /// appropriate name/value pairs
        /// </summary>
        /// <typeparam name="T">Type of cached item</typeparam>
        /// <param name="o">Item to be cached</param>
        /// <param name="key">Name of item</param>
        public static void Add<T>(T o, string key)
        {
          
            _items[key] = o;
       
        }

        /// <summary>
        /// Remove item from cache
        /// </summary>
        /// <param name="key">Name of cached item</param>
        public static void Clear(string key)
        {
            _items.Remove(key);          
        }

        /// <summary>
        /// Check for item in cache
        /// </summary>
        /// <param name="key">Name of cached item</param>
        /// <returns></returns>
        public static bool Exists(string key)
        {
            return _items.ContainsKey(key);
        }

        /// <summary>
        /// Retrieve cached item
        /// </summary>
        /// <typeparam name="T">Type of cached item</typeparam>
        /// <param name="key">Name of cached item</param>
        /// <param name="value">Cached value. Default(T) if item doesn't exist.</param>
        /// <returns>Cached item as type</returns>
        public static bool Get<T>(string key, out T value)
        {
            try
            {
                if (!Exists(key))
                {
                    value = default(T);
                    return false;
                }

                value = (T)_items[key];
            }
            catch
            {
                value = default(T);
                return false;
            }

            return true;
        }

        public static void ClearAllCache()
        {
            try
            {
                foreach (DictionaryEntry dEntry in _items)
                {
                    _items.Remove(dEntry.Key.ToString());
                }
            }
            catch (Exception ex)
            {

            }
        }
    }

 

You can see that this class will do everything you want for you.  

Let  add a Data file to test this :

public class DataAccess
    {
internal static List<Parameter> GetParameterList()
        {
            return
               new List<Parameter>
                   {
                       new Parameter {Name = "storecode", Value = "sc001"},
                       new Parameter {Name = "terminal", Value = "zt2"}
                   };
        }
}

 public class Parameter
    {
        public string Value { get; set; }
        public string Name { get; set; }
       
    }
What you can nnow do is just add a website to your project to test this.. 
add a button BtnGetCache, and a label lblMessage, a GridView GridView1 in your aspx page then this in the click event 
protected void BtnGetCache_Click(object sender, EventArgs e)
        {
            string key = "oneParameter";
            List<Parameter> parameters;

            if (!CacheHelper.RWCache.Get(key, out parameters))
            {
                parameters = DataAccess.GetParameterList();
                CacheHelper.RWCache.Add(parameters, key);
                lblMessage.Text =
                    string.Format("Parameters [Reference Type Count = {0}] We did not found but retrieved and now added item to cache.",
                                  parameters.Count);
            }
            else
            {
                lblMessage.Text = string.Format("Parameters [Reference Type Count = {0}] pulled  item from cache.",
                                                   parameters.Count);
            }

            GridView1.DataSource = parameters;
            GridView1.DataBind();
        }
Hope you enjoy this.

Rhomobile:Getting the MAC OS Lion in my windows 7 in order to run XCode

by daz 12/11/2011 5:58:00 AM

 

Rhomobile seems to be the future for App development.A new project has been started months ago with mobile’s application development. Exciting right? I love Microsoft, reason why I am a MCAD; however, when it comes to mobile, Microsoft is left behind.I have tried windows mobile 6.x but disappointed with the heaviness of the all thing. I guess the reason is that you can beat the whole world one day but never beat it all time. The technology is growing fast. So, i have the opportunity to be involved in this new project with RhoStudio, which is an Eclipse installation that facilitates development of native extension. Thus, RhoMobile is the solution for the app we need. It is a Cross-Platform Mobile App Development technology. In fact Motorola has his hands on this technology; It uses html5 which seems to be the future of the web and app; For the hec of it, as i said, i love Microsoft and never use any apple product before, I downloaded the Rhostudio and installed it in my windows7 computer. The app development seems to be pretty straight forward; When  it come to native extension I need to compile with Xcode: a challenge. The best scenarios will be to get a Mac. Apple being expensive i found this cool link that will put you on the road with Rhomobile and xcode. I need Xcode to build my application for Iphone, ipode oe any other supported device. This link will show you step by step how to install the latest version of Mac OS X Lion in your Windows Computer. Work around, is a short term solution. If you have money, get yourself a Mac.!

http://www.windows7hacker.com/index.php/2011/09/how-to-install-fully-functional-mac-os-x-lion-virtual-machine-inside-windows-7/

 

Store locator with google V3 API ;

by daz 5/20/2011 12:36:00 AM

This is the workflow for the new store locator comming soon. it consist of 2 pages you can add easily to you asp.net storefront website or any website using microsoft technology(.Net Framework)

Stay tune for the next post...

Tags:

About the author

Name of author
DavidZ
.Net Programmer
E-mail me Send mail
aspdotnetstorefront hosting

Partners

Repertoire canada

Recent comments

Archive

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012

Sign in