#Redis Cache in Azure-MVC

What is Redis Cache and Why do we need to move cache into separate server?

1.If you have a enterprise web application which is responsible for serving several thousand of request,it is advisable that you maintain a separate server for maintaining the cache,so that your cache does not eats up your server memory.(Azure)

2.Traditional Cache can operate only on Key/Value Pair,where as Redis Cache you have built-in data types like string,hash,list.It is more of a useful library which has several custom function which can be used over cache.

Creating an Sample Redis Cache Service in Azure.

1.Setting up Redis Service in Azure:

This is pretty easy one. You would need the Primary Connection String,make sure you add it to the web config of your file.



2.Setting up connection for your project.

1.Copy the Connection String from the Portal and add it to your web config

2.Make use of ConnectionMultiplexer class provided by .Net for connecting to the redis cache.

public class CacheInstance
private static ConnectionMultiplexer _connection;

public CacheInstance()
var constring = ConfigurationManager.ConnectionStrings["RedisCache"].ConnectionString;
_connection = ConnectionMultiplexer.Connect(constring);

public void PutItem(String Key,String Value)
IDatabase cache = _connection.GetDatabase();

public String GetItem(String Key)
IDatabase cache = _connection.GetDatabase();
string redisValue = cache.StringGet(Key);
return redisValue;

In the above sample,i have created two sample methods Put and GetItem which would add and retrieve items from cache.

3.Using  Redis Cache in your Application.

This is Sample controller which makes use of redis cache in Azure,this application will cache the time when it first runs and will never update the time from then.

public class HomeController : Controller
public ActionResult Index()
var cache = new CacheInstance();

var res = cache.GetItem("Datetime");
if (String.IsNullOrEmpty(res))
return Content(res);

In the web page you can see that time is cached and never updated.


and here is the proof that we are hitting the azure redis cache,






Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s