DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Momento Migrates Object Cache as a Service to Ampere® Altra®
  • Writing an Interpreter: Implementation
  • Generics in Java and Their Implementation
  • Simple Sophisticated Object Cache Service Using Azure Redis

Trending

  • Beyond @Transactional: Solving the Dual-Write Problem in Distributed Microservices
  • How to Format Articles for DZone
  • Java 8 Optional - Avoid Null and NullPointerException Altogether - and Keep It Pretty
  • Building a Real Pipeline With senro: Monorepos, Caching, and Triggers

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. In-memory Cache Implementation in C#

In-memory Cache Implementation in C#

By 
Punit Ganshani
·
Feb. 02, 12 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
78.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Cache (computing) Implementation Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Momento Migrates Object Cache as a Service to Ampere® Altra®
  • Writing an Interpreter: Implementation
  • Generics in Java and Their Implementation
  • Simple Sophisticated Object Cache Service Using Azure Redis

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook