Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
.NET Zone is brought to you in partnership with:

Punit has over 5 years of professional experience and currently working with one of the largest IT service providers. He is an expert on Performance oriented design and development, and RIA based systems involving Silverlight, WPF and Windows Phone. He is an open-source contributor of CInject on Codeplex, has 6 applications in MarketPlace, and a freeware application 'myTracker' on Softpedia. He writes on various topics on his website & has spoken in various forums in UK and India. Punit is a DZone MVB and is not an employee of DZone and has posted 10 posts at DZone. You can read more from them at their website. View Full User Profile

In-memory Cache Implementation in C#

02.02.2012
Email
Views: 1906
  • submit to reddit
The .NET Zone is presented by JNBridge to keep you updated on all the latest news, tips, and tools in the .NET community.  Check out today's top .NET content and read about JNBridge's innovative tools for .NET and Java interoperability.  

The simplest in-memory cache implementation should support

  • Addition of objects into cache either via key-value, or via object creation mechanism
  • Deletion of objects from cache based on key, or object type
  • Querying cache store to check existence of an object

There are several ways to achieve this using multiple design patterns.  But if we were to implement those design patterns in our applications, we would end up designing a framework similar to Enterprise Library Caching block.  So to keep things fairly simple – we need a simple implementation of caching objects in-memory and this cache to be thread-safe for multi-threading applications.

So for that, you can just copy this piece of code into your application and you should be all set with an in-memory cache.

public static class CacheStore

    {

        /// <summary>

        /// In-memory cache dictionary

        /// </summary>

        private static Dictionary<string, object> _cache;

        private static object _sync;

 

 

        /// <summary>

        /// Cache initializer

        /// </summary>

        static CacheStore()

        {

            _cache = new Dictionary<string, object>();

            _sync = new object();

        }

 

        /// <summary>

        /// Check if an object exists in cache

        /// </summary>

        /// <typeparam name="T">Type of object</typeparam>

        /// <param name="key">Name of key in cache</param>

        /// <returns>True, if yes; False, otherwise</returns>

        public static bool Exists<T>(string key) where T : class

        {

            Type type = typeof(T);

            lock (_sync)

            {

                return _cache.ContainsKey(type.Name + key);

            }

        }

 

        /// <summary>

        /// Check if an object exists in cache

        /// </summary>

        /// <typeparam name="T">Type of object</typeparam>

        /// <returns>True, if yes; False, otherwise</returns>

        public static bool Exists<T>() where T : class

        {

            Type type = typeof(T);

            lock (_sync)

            {

                return _cache.ContainsKey(type.Name);

            }

        }

 

        /// <summary>

        /// Get an object from cache

        /// </summary>

        /// <typeparam name="T">Type of object</typeparam>

        /// <returns>Object from cache</returns>

        public static T Get<T>() where T : class

        {

            Type type = typeof(T);

            lock (_sync)

            {

                if (_cache.ContainsKey(type.Name) == false)

                    throw new ApplicationException("An object of the desired type does not exist: " + type.Name);

 

                lock (_sync)

                {

                    return (T)_cache[type.Name];

                }

            }

        }

 

        /// <summary>

        /// Get an object from cache

        /// </summary>

        /// <typeparam name="T">Type of object</typeparam>

        /// <param name="key">Name of key in cache</param>

        /// <returns>Object from cache</returns>

        public static T Get<T>(string key) where T : class

        {

            Type type = typeof(T);

            lock (_sync)

            {

                if (_cache.ContainsKey(key + type.Name) == false)

                    throw new ApplicationException(String.Format("An object with key '{0}' does not exists", key));

                lock (_sync)

                {

                    return (T)_cache[key + type.Name];

                }

            }

        }

 

        /// <summary>

        /// Create default instance of the object and add it in cache

        /// </summary>

        /// <typeparam name="T">Class whose object is to be created</typeparam>

        /// <returns>Object of the class</returns>

        public static T Create<T>(string key, params object[] constructorParameters) where T : class

        {

            Type type = typeof(T);

            T value = (T)Activator.CreateInstance(type, constructorParameters);

            lock (_sync)

            {

                if (_cache.ContainsKey(key + type.Name))

                    throw new ApplicationException(String.Format("An object with key '{0}' already exists", key));

                lock (_sync)

                {

                    _cache.Add(key + type.Name, value);

                }

            }

            return value;

        }

 

        /// <summary>

        /// Create default instance of the object and add it in cache

        /// </summary>

        /// <typeparam name="T">Class whose object is to be created</typeparam>

        /// <returns>Object of the class</returns>

        public static T Create<T>(params object[] constructorParameters) where T : class

        {

            Type type = typeof(T);

            T value = (T)Activator.CreateInstance(type, constructorParameters);

 

            lock (_sync)

            {

                if (_cache.ContainsKey(type.Name))

                    throw new ApplicationException(String.Format("An object of type '{0}' already exists", type.Name));

                lock (_sync)

                {

                    _cache.Add(type.Name, value);

                }

            }

 

            return value;

        }

 

        public static void Add<T>(string key, T value)

        {

            Type type = typeof(T);

 

            if (value.GetType() != type)

                throw new ApplicationException(String.Format("The type of value passed to cache {0} does not match the cache type {1} for key {2}", value.GetType().FullName, type.FullName, key));

            lock (_sync)

            {

                if (_cache.ContainsKey(key + type.Name))

                    throw new ApplicationException(String.Format("An object with key '{0}' already exists", key));

                lock (_sync)

                {

                    _cache.Add(key + type.Name, value);

                }

            }

        }

 

        /// <summary>

        /// Remove an object type from cache

        /// </summary>

        /// <typeparam name="T">Type of object</typeparam>

        public void Remove<T>()

        {

            Type type = typeof(T);

 

            lock (_sync)

            {

                if (_cache.ContainsKey(type.Name) == false)

                    throw new ApplicationException(String.Format("An object of type '{0}' does not exists in cache", type.Name));

                lock (_sync)

                {

                    _cache.Remove(type.Name);

                }

            }

        }

 

        /// <summary>

        /// Remove an object stored with a key from cache

        /// </summary>

        /// <typeparam name="T">Type of object</typeparam>

        /// <param name="key">Key of the object</param>

        public void Remove<T>(string key)

        {

            Type type = typeof(T);

 

            lock (_sync)

            {

                if (_cache.ContainsKey(key + type.Name) == false)

                    throw new ApplicationException(String.Format("An object with key '{0}' does not exists in cache", key));

                lock (_sync)

                {

                    _cache.Remove(key + type.Name);

                }

            }

        }

    }

Every method has 2 overloads

With Key as a parameter:  This method adds a new key-value in the cache store for a particular object type.  This also means that for a particular object (say Employee), you can have multiple cached-objects (say, multiple employees in an organization)

Without Key as a parameter – This method adds a new key (type of the object) and value in the cache store.  This means, for a particular object type (say ConfigurationSettings) there will single object in the cache (say, configuration value)

Implementation example using CacheStore is:

MonoAssemblyResolver targetAssembly = null;

if (CacheStore.Exists<MonoAssemblyResolver>(projMapping.TargetAssemblyPath))

{

    targetAssembly = CacheStore.Get<MonoAssemblyResolver>(projMapping.TargetAssemblyPath);

}

else

{

    targetAssembly = new MonoAssemblyResolver(projMapping.TargetAssemblyPath);

    CacheStore.Add<MonoAssemblyResolver>(projMapping.TargetAssemblyPath, targetAssembly);

}

Since this uses plain-C# and is light weight, this can be used in ASP.NET MVC, Silverlight, WPF, or Windows Phone applications.  So happy coding!

 

Source: http://www.ganshani.com/2012/01/31/in-memory-cache-implementation-in-c

Tags:
Published at DZone with permission of Punit Ganshani, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

This content was brought to you in partnership with JNBridge.  JNBridge specializes in .NET and Java interoperability.  Be sure to view JNBridge's brief video series on accessing Java from .NET.