Simple Web CacheUtils Class

This is a simple helper\utility Class is used for Caching within a web environment. It uses HttpContext.Current.Cache to Get, Set and Delete objects.

Here is the example code


namespace Utility
{
    public class CacheUtils
    {
        public static object GetCachedItem(string name)
        {
            if (HttpContext.Current == null)
                return null;

            if (string.IsNullOrEmpty(name))
                return null;

            if (!Exists(name))
                return null;

            return HttpContext.Current.Cache[name];
        }

        public static bool Exists(string name)
        {
            if ((HttpContext.Current == null))
                return false;

            if (string.IsNullOrEmpty(name))
                return false;

            return (HttpContext.Current.Cache[name] != null) ? true : false;
        }

        public static bool SetCachedItem(string name, object value)
        {
            return SetCachedItem(name, value, 20);
        }

        public static bool SetCachedItem(string name, object value, double minutes)
        {
            if ((HttpContext.Current == null))
                return false;

            if (string.IsNullOrEmpty(name))
                return false;

            HttpContext.Current.Cache.Insert(name, value, null, DateTime.Now.AddMinutes(minutes), TimeSpan.Zero);

            return true;
        }

        public static bool RemoveCachedItem(string name)
        {
            if (HttpContext.Current == null)
                return false;

            if (string.IsNullOrEmpty(name))
                return false;

            if (!Exists(name))
                return true;

            //Remove
            HttpContext.Current.Cache.Remove(name);

            return true;
        }

        public static void RemoveAllCacheItems(string prefix)
        {
            IDictionaryEnumerator cacheEnum = HttpContext.Current.Cache.GetEnumerator();

            while(cacheEnum.MoveNext())
            {
                if (cacheEnum.Key.ToString().StartsWith(prefix))
                    HttpContext.Current.Cache.Remove(cacheEnum.Key.ToString());
            }
        }
    }
}

Here is an example of how to use it


           Object myExampleObject = new Object();

            //Set the object into the cache
            if (!CacheUtils.Exists("myExampleObject"))
                CacheUtils.SetCachedItem("myExampleObject", myExampleObject);

            Object cachedObject = new Object();

            //Get the object from the cache
            if (CacheUtils.Exists("myExampleObject"))
                cachedObject = CacheUtils.GetCachedItem("myExampleObject");

            //Delete the object from the cache
            CacheUtils.RemoveCachedItem("myExampleObject");



Comments

No comments yet.

Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.