2

I'm creating a POC to see if we can use the azure redis cache for our next project. I had a look at this MSDN documentation http://azure.microsoft.com/en-us/documentation/articles/cache-dotnet-how-to-use-azure-redis-cache/#connect-to-cache.

And have following questions/doubts:

  1. Only way (elegant) to connect to redis is via code below? Is it not possible to do same as Redis Cache where you configure and just works?

ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("contoso5.redis.cache.windows.net,ssl=true,password=...");

  1. We need to call above and connect to redis for every Get and Set? If not how can we call only once and re-use?

  2. All I can see is StringGet and StringSet. Is it possible to set the complex .NET Types as well? Example would be good. Below is what I've done in past using dedicate azure cache or ent.Lib cache.

public class CacheManager {

    public void AddToCache(string Id, T value)
    {
        string Key = this.MakeKey(Id);

        if (this.m_Cache.Contains(Key))
        {
            this.m_Cache.Remove(Key);
        }
        ------
        this.m_Cache.Add(Key, value,CacheItemPriority.Normal, null, expireTime);
    }         }
Nil Pun
  • 17,035
  • 39
  • 172
  • 294

2 Answers2

1

You can have a single instance of the ConnectionMultiplexer and use it for your entire application. You do not need to create one for each Get/Set call. You can find an example http://azure.microsoft.com/blog/2014/06/05/mvc-movie-app-with-azure-redis-cache-in-15-minutes/. I am copying the relevant code below

  private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
      {

      var config = new ConfigurationOptions();
            config.EndPoints.Add(cachename);
            config.Password = password;
            config.Ssl = true;
            config.SyncTimeout = 150;

           return ConnectionMultiplexer.Connect(config);
      });

You can store objects in the Cache as well. The same link I mentioned above shows how can you serialize .NET Objects into the Cache.

pranav rastogi
  • 4,124
  • 23
  • 23
1

Yes you want to reuse the ConnectionMultiplexer instance which is created by Connect. So the static variable approach should be good to go.

Regarding saving Poco objects, yes of course that's possible, but for anything else than some basic Types, you have to serialize and deserialze the objects. Which can be very slow. Especially if you use the binary formatter as shown in the example of the answer from pranav rastogi.

I would either store the values of the object in cache, so that you only store non complex types in the cache, like string, byte array, integers etc... Or use a faster serialization method. Even Json serialization is often much faster.

If you don't want to implement all this on your own and just want a strongly typed cache system, you can also use my CacheManager, which supports Redis, too, based on the StackExchange.Redis client...

Although I have to admit that I'm also using BinaryFormatter for serialization if it is an unknown type you want to store. But this might change and I might support Protobuf and Json as well...

MichaC
  • 13,104
  • 2
  • 44
  • 56