Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#

How to: Cache Objects Simply using System.Runtime.Caching.MemoryCache

Rate me:
Please Sign up or sign in to vote.
4.80/5 (29 votes)
8 Apr 2014CPOL2 min read 140.8K   2.5K   56   17
This article offers a way to simply the use of System.Runtime.Caching.MemoryCache for caching application objects

Image 1

Introduction

This article shows how to properly use System.Runtime.Caching.MemoryCache by unifying an inherited Singleton structure.

Background

It is very common for an application to cache objects, whether it is a server side application such as WCF service (Per-Call), a web application (server-side) or even a simple standalone Windows application (WPF/Win-Forms).

.NET 4.0 provides a very simple caching object called System.Runtime.Caching.MemoryCache.

But how do we properly use this object?

From my experience, it is better to unify & simplify these objects into a structured utility that can be used across the application for the following reasons:

  1. Avoids code duplication across the application
  2. MemoryCache configuration is unified and can be easily changed
  3. Ease of access to the use of the object for the less experienced programmers on the team

Caching Provider (Base)

Let's wrap the System.Runtime.Caching.MemoryCache using an abstract base class that contains:

  1. MemoryCache instance
  2. Locking mechanism
  3. Errors log
C#
public abstract class CachingProviderBase
{
    public CachingProviderBase()
    {
        DeleteLog();
    }

    protected MemoryCache cache = new MemoryCache("CachingProvider");

    static readonly object padlock = new object();

    protected virtual void AddItem(string key, object value)
    {
        lock (padlock)
        {
            cache.Add(key, value, DateTimeOffset.MaxValue);
        }
    }

    protected virtual void RemoveItem(string key)
    {
        lock (padlock)
        {
            cache.Remove(key);
        }
    }

    protected virtual object GetItem(string key, bool remove)
    {
        lock (padlock)
        {
            var res = cache[key];

            if (res != null)
            {
                if (remove == true)
                    cache.Remove(key);
            }
            else
            {
                WriteToLog("CachingProvider-GetItem: Don't contains key: " + key);
            }

            return res;
        }
    }

    #region Error Logs

    string LogPath = System.Environment.GetEnvironmentVariable("TEMP");

    protected void DeleteLog()
    {
        System.IO.File.Delete(string.Format("{0}\\CachingProvider_Errors.txt", LogPath));
    }

    protected void WriteToLog(string text)
    {
        using (System.IO.TextWriter tw = System.IO.File.AppendText(string.Format("{0}\\CachingProvider_Errors.txt", LogPath)))
        {
            tw.WriteLine(text);
            tw.Close();
        }
    }

    #endregion
} 

Global Caching Provider

Next, I will create an example of a global application cache, which is used for common caching activities of simply caching an object and fetching and removing the object.

C#
public interface IGlobalCachingProvider
{
    void AddItem(string key, object value);
    object GetItem(string key);
} 

The Global Caching Provider inherits from the CachingProviderBase, and exposes an interfaced singleton:

C#
public class GlobalCachingProvider : CachingProviderBase, IGlobalCachingProvider
{
    #region Singleton 

    protected GlobalCachingProvider()
    {
    }

    public static GlobalCachingProvider Instance
    {
        get
        {
            return Nested.instance;
        }
    }

    class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        } 
        internal static readonly GlobalCachingProvider instance = new GlobalCachingProvider();
    }

    #endregion

    #region ICachingProvider

    public virtual new void AddItem(string key, object value)
    {
        base.AddItem(key, value);
    }

    public virtual object GetItem(string key)
    {
        return base.GetItem(key, true);//Remove default is true because it's Global Cache!
    }

    public virtual new object GetItem(string key, bool remove)
    {
        return base.GetItem(key, remove);
    }

    #endregion

} 

Using the Code

For the sake of simplicity, I will demonstrate the use of System.Runtime.Caching.MemoryCache by a basic WPF application.

The application is caching a text message and fetching the message only when the presentation object is created. The presentation object is completely disconnected from the object storing the message, and even does not have to exist when the message is being stored.

Image 2

The process is as follows:

Image 3

The code behind is as follows:

C#
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        pesenterCombobox.Items.Add("Message-Box");
        pesenterCombobox.Items.Add("Text-Block");
        pesenterCombobox.Items.Add("List-Box");
        pesenterCombobox.SelectedIndex = 0;
    }

    private void StoreBtn_Click(object sender, RoutedEventArgs e)
    {
        string text = new TextRange(inputTextBox.Document.ContentStart,
            inputTextBox.Document.ContentEnd).Text;

        //Store the message in the cache:
        GlobalCachingProvider.Instance.AddItem("Message", text);
    }

    private void PresentBtn_Click(object sender, RoutedEventArgs e)
    {
        //fetch the message from the cache:
        var message = GlobalCachingProvider.Instance.GetItem("Message") as string;

        switch (pesenterCombobox.SelectedIndex)
        {
            //"Message-Box"
            case 0:
                {
                    MessageBox.Show(message);
                    break;
                }
            //"Text-Block"
            case 1:
                {
                    contentPresenter.Content = new TextBlock() { Text = message };
                    break;
                }
            //"List-Box"
            case 2:
                {
                    if (message != null)
                    {
                        var listbox = new ListBox();
                        var lines = message.Split('\n');
                        foreach (var ln in lines)
                            listbox.Items.Add(new ListViewItem() { Content = ln, Height = 16 });
                        contentPresenter.Content = listbox;
                    }
                    break;
                }
        }
    }
}

Further Discussion

The idea behind the article is offering a way unify & simplify your code using application utilities. When we are planning an application, we should modularize our code in order to achieve:

  1. Flexibility in case of future changes
  2. Easy maintenance

Modularize code is unified, simple and easy to change, it's also easy to maintain because every module is a small part of the system. Instead of searching for the bug in, say 1,000 lines of code (LOC) you only need to check ~100 LOC.

Modularize application should often be using strong, unified & simplified utilities. For example:

  • ThreadInvoker - enables us to simplify and change the use of threads.
  • LoggerManager - enables us to simplify and change the use of loggers.
  • DataAccessManager - enables us to simplify and change the access to data: sometimes it could be access to Database and sometimes WCF services or both.... and we can change it without touching the business-logic code!
  • Etc.

History

  • 11 May 2014 - Adding "Further Discussion" section

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer GalilCS
Israel Israel
My name is Shai Vashdi and I’m the CTO & Co-Founder of GalilCS.
GalilCS provides industrialized, low-cost IT services (ILCS) to corporations around the globe. We specialize in code modernization projects (VB6 to C# WinForms/HTML5, for example) and code refactoring projects.
Most of my work revolves around the management of these projects and the creation of new tools for refactoring code. As a bonus, I also get to lecture about Microsoft programming languages.
For more information, visit GalilCS.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Juan José De Arana27-Dec-16 20:50
Juan José De Arana27-Dec-16 20:50 
QuestionInterfaced Singleton ? Pin
Member 103728068-Nov-16 13:54
Member 103728068-Nov-16 13:54 
QuestionHow to invalidate cache data Pin
Tridip Bhattacharjee19-Sep-16 21:48
professionalTridip Bhattacharjee19-Sep-16 21:48 
QuestionHow to add multiple item and get data by filter Pin
Tridip Bhattacharjee19-Sep-16 21:44
professionalTridip Bhattacharjee19-Sep-16 21:44 
Generalmy vote of 5 Pin
Southmountain2-Apr-16 10:34
Southmountain2-Apr-16 10:34 
GeneralMy vote of 2 Pin
AndrewB-UK21-Jan-15 6:25
AndrewB-UK21-Jan-15 6:25 
SuggestionDistributed Caching using System.Runtime.Caching.ObjectCache Pin
sameer@alachisoft.com28-Dec-14 0:51
sameer@alachisoft.com28-Dec-14 0:51 
QuestionWhy are you locking the object? PinPopular
aprajay16-Oct-14 2:01
aprajay16-Oct-14 2:01 
AnswerRe: Why are you locking the object? Pin
Juan José De Arana27-Dec-16 20:48
Juan José De Arana27-Dec-16 20:48 
GeneralMy vote of 5 Pin
Shai Vashdi17-Apr-14 5:13
Shai Vashdi17-Apr-14 5:13 
GeneralRe: My vote of 5 Pin
John B Oliver29-Apr-14 11:53
John B Oliver29-Apr-14 11:53 
GeneralRe: My vote of 5 Pin
Shai Vashdi29-Apr-14 13:40
Shai Vashdi29-Apr-14 13:40 
GeneralRe: My vote of 5 Pin
Nelek11-May-14 6:18
protectorNelek11-May-14 6:18 
GeneralMessage Closed Pin
8-Apr-14 20:17
professionalАslam Iqbal8-Apr-14 20:17 
GeneralRe: My vote of 1 PinPopular
John Brett9-Apr-14 1:22
John Brett9-Apr-14 1:22 
I can't condone a vote of 1 without comment.
There are things to say about this article, aspects to like and aspects to dislike.

However, voting 1 without an explanation is disrespectful.

John
GeneralRe: My vote of 1 PinPopular
Paolo Senigagliesi9-Apr-14 1:58
Paolo Senigagliesi9-Apr-14 1:58 
GeneralRe: My vote of 1 Pin
Shai Vashdi9-Apr-14 7:25
Shai Vashdi9-Apr-14 7:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.