Caching and memory constraints

Caching can be a great way of increasing the performance of an application. I often use the HttpRuntime.Cache just for this purpose, even in WinForm applications as it is fast and easy to use. However as usual there is a tradeoff. In the case of caching the catch is memory consumption. Instead of querying or calculating some data again just keep all results around in the hope they are used again. But that uses up memory and while coding you probably have a fast machine with plenty of memory the case at runtime might be a little different.

One easy way to solve the problem is to the WeakReference object. Using this WeakReference you keep sort of a hold on an object but allow the .NET garbage collector to remove it from memory when required. The following code is a quick sample of how to do this. Keep in mind that this is a quick demo and not fully tested so use at your own risk [:)]

using System;

using System.Web;

   

namespace MesFileImporterShared.Utils

{

    static class Cache

    {

   

        static public void Insert(string key, object value)

        {

            CacheItem item = GetCacheItem(key, value);

   

            HttpRuntime.Cache[key] = item;

        }

   

   

        private static CacheItem GetCacheItem(string key, object value)

        {

            CacheItem result = new CacheItem();

            result.Key = key;

            result.Value = new WeakReference(value, false);

            return result;

        }

   

        private static bool Exists(string key)

        {

            bool result = false;

            CacheItem item = HttpRuntime.Cache[key] as CacheItem;

   

            if (item != null)

            {

                result = item.Value.IsAlive;

            }

   

            return result;

        }

   

        public static object Get(string key)

        {

            object result = null;

            CacheItem item = HttpRuntime.Cache[key] as CacheItem;

   

            if (item != null)

                if (item.Value.IsAlive)

                    result = item.Value.Target;

                else

                    throw new NullReferenceException(“Item is no longer alive”);

   

            return result;

        }

   

        public static bool TryGet(string key, out object value)

        {

            bool result = false;

            value = null;

            CacheItem item = HttpRuntime.Cache[key] as CacheItem;

            if (item != null && item.Value.IsAlive)

            {

                value = item.Value.Target;

                result = true;

            }

   

            return result;

        }

   

        private class CacheItem

        {

            public string Key;

            public WeakReference Value;

        }

    }

}


 

Enjoy!

One thought on “Caching and memory constraints

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>