Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

In-memory Cache Implementation in C#

DZone's Guide to

In-memory Cache Implementation in C#

· Database Zone
Free Resource

Learn how to move from MongoDB to Couchbase Server for consistent high performance in distributed environments at any scale.

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

Want to deliver a whole new level of customer experience? Learn how to make your move from MongoDB to Couchbase Server.

Topics:
dotnet ,c-sharp

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}