C# Exercises

Home AgriMetSoft About Contact

Save and Retrieve data in Global Memory Caching in WinForms

	    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Global_Memory_Caching
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void save_Click(object sender, EventArgs e)
    {
      GlobalCachingProvider.Instance.AddItem("Val", richTextBox1.Text);
      MessageBox.Show("Values are stored!");
    }

    private void retrieve_Click(object sender, EventArgs e)
    {
      var message = GlobalCachingProvider.Instance.GetItem("Val", false) as string;
      richTextBox1.Text = message;
    }

    private void clear_Click(object sender, EventArgs e)
    {
      richTextBox1.Text = "";
    }

    private void button1_Click(object sender, EventArgs e)
    {
      GlobalCachingProvider.Instance.RemoveItem("Val");
    }
  }
}

//===============================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


public class GlobalCachingProvider : CachingProviderBase, IGlobalCachingProvider
{
  #region Singelton (inheriting enabled)

  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 new void RemoveItem(string key)
  {
    base.RemoveItem(key);
  }
  public virtual object GetItem(string key)
  {
    return base.GetItem(key, true);//Remove defulat is true because it's Global Cache!
  }

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

  #endregion

}

//===============================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;

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
}

//===============================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


public interface IGlobalCachingProvider
{
  void AddItem(string key, object value);

  object GetItem(string key);
  object GetItem(string key, bool remove);
}
		
	 


Download the project of Visual Studio 2013 in DropBox Download


How to Save Retrieve data in Global Memory Caching in C# .Net


List of Exercises