Working With Caching in C#

Introduction

This article introduces implementation of caching with the C# language, before starting the discussion about code and the implementation of caching, I would like to explain about Caches and Caching.

Caches

A cache is the most valuable feature that Microsoft provides. It is a type of memory that is relatively small but can be accessed very quickly. It essentially stores information that is likely to be used again. For example, web browsers typically use a cache to make web pages load faster by storing a copy of the webpage files locally, such as on your local computer.

Starting


Caching is the process of storing data into cache.Working. Caching with the C# language is very easy. System.Runtime.Caching.dll provides the feature for working with caching in C#. In this illustration I am using the following classes:

  1. ObjectCache
  2. MomoryCache
  3. CacheItemPolicy

ObjectCache: The CacheItem class provides a logical representation of a cache entry, that can include regions using the RegionName property. It exists in the System.Runtime.Caching.

MomoryCache: This class also comes under System.Runtime.Caching and it represents the type that implements an in-cache memory.

CacheItemPolicy: Represents a set of eviction and expiration details for a specific cache entry.

Code Work: Storying information into a cache.

In this illustration I have kept some stock items in the cache memory for further use that is to understand the availability of a stock item in the future. When the application does not find any cached data in cache memory, it will redirect to the Get default stock function. The following code is for storing information into cache memory.

  1. private const string CacheKey = "availableStocks";    
  2. CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();    
  3. cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddHours(1.0);    
  4. cache.Add(CacheKey, availableStocks, cacheItemPolicy);    
Code above the line number 02 tells the system to create a policy for storing information in the cache, Line 03 specifies which policy to use. There are the following two types of policies available in the cashitempolicy class.
  1. AbsoluteExpiration: Indicating whether sliding expiration is enabled.
  2. SlidingExpiration: Policy specifies when a cache entry should be evicted after a specified duration.

I have given one hour duration to expire cache memory and line number 04 stores information with a unique key and cache policy. The Add() method stores information into memory and the Add() method is three types based its parameter. They are as in the following:

  1. Add(CacheItem Item,CacheItemPolicy policy)
  2. Add(string key,object value,CacheItemPolicy policy, string retionname)
  3. Add(string key,object value,DateTimeOffset absoluteExpiration, string retionname)
CacheKey is my unique key by which I can query in the future. The CacheItem class represents an individual cache entry in the cache, in other words this class has a structure to store information such as Region name, Key and value to store in memory. absoluteExpiration is like a time duration for expiring or releasing cached information.

Retrive Information from Cache


Getting or querying information from cache memory is as easy as storing into memory. The Get function of the ObjectCache class helps to retrieve information from cache memory. This Get() method takes two parameters for querying data from cache memory. Key and region name. See the following code that helps to retrieve information from memory.

Code
  1. ObjectCache cache = MemoryCache.Default;    
  2. Enumerable information=(IEnumerable)cache.Get(CacheKey);   
Illustrated Code
  1. namespace CachingWithConsoleApplication    
  2. {    
  3.     class Program    
  4.     {    
  5.         static void Main(string[] args)    
  6.         {    
  7.             StockItems PS = new StockItems();    
  8.          List<string> Pizzas= (List<string>)  PS.GetAvailableStocks();    
  9.          Pizzas = (List<string>)PS.GetAvailableStocks();    
  10.         }    
  11.     }    
  12.     
  13.     public class StockItems    
  14.     {    
  15.         private const string CacheKey = "availableStocks";    
  16.     
  17.         public IEnumerable GetAvailableStocks()    
  18.         {    
  19.             ObjectCache cache = MemoryCache.Default;    
  20.     
  21.             if (cache.Contains(CacheKey))    
  22.                 return (IEnumerable)cache.Get(CacheKey);    
  23.             else    
  24.             {    
  25.                 IEnumerable availableStocks = this.GetDefaultStocks();    
  26.     
  27.                 // Store data in the cache    
  28.                 CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();    
  29.                 cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddHours(1.0);    
  30.                 cache.Add(CacheKey, availableStocks, cacheItemPolicy);    
  31.     
  32.                 return availableStocks;    
  33.             }    
  34.         }    
  35.         public IEnumerable GetDefaultStocks()    
  36.         {    
  37.             return new List<string>() { "Pen""Pencil""Eraser" };    
  38.         }    
  39.     }    
  40.     
  41. }  
Summary

In the preceding discussion we came to understand how information can be stored in system memory using caching.

Up Next
    Ebook Download
    View all
    Learn
    View all