## Sunday, 14 May 2017

### Redis Image Cache Provider

Caching images in web sites and web applications is possible to achieve using many methods. This article presents an image cache provider to use in MVC applications using Redis as a caching server.

First off, create a new MVC web application and add the Nuget package RedisImageCacher (created by me):
RedisImageCacher

 Install-Package RedisImageCacher 

This Nuget package will also add in the dependencies for libraries ServiceStack.Redis and Microsoft.Web.RedisOutputCacheProvider.

Next off, one will need a Redis server. It is possible to install a local Redis server in Windows using the binaries here:

Redis 2.6 Windows (32/64 bits) on GitHub from MSOpenTech.

ASP.NET and Redis

Download Redis 2.6 and start it up on your local developer box. Start the server from a command-line for example :

 C:\redis26\redis-server 

Start a Redis client also from the commandline e.g. :
 C:\redis26\redis-cli 

  <caching>
<outputCache defaultProvider="MyRedisOutputCache">
<providers>
<!-- Either use 'connectionString' and provide all parameters as string OR use 'host','port','accessKey','ssl','connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds'. -->
<!-- 'databaseId' and 'applicationName' can be used with both options. -->
<!--
host = "127.0.0.1" [String]
port = "" [number]
accessKey = "" [String]
ssl = "false" [true|false]
databaseId = "0" [number]
applicationName = "" [String]
connectionTimeoutInMilliseconds = "5000" [number]
operationTimeoutInMilliseconds = "1000" [number]
connectionString = "<Valid StackExchange.Redis connection string>" [String]
loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String]
loggingMethodName = "<Logging method should be defined in loggingClass. It should be public, static, does not take any parameters and should have a return type of System.IO.TextWriter.>" [String]
/>
-->
<!-- For more details check https://github.com/Azure/aspnet-redis-providers/wiki -->
<!-- Either use 'connectionString' OR 'settingsClassName' and 'settingsMethodName' OR use 'host','port','accessKey','ssl','connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds'. -->
<!-- 'databaseId' and 'applicationName' can be used with both options. -->
<!--
host = "127.0.0.1" [String]
port = "" [number]
accessKey = "" [String]
ssl = "false" [true|false]
databaseId = "0" [number]
applicationName = "" [String]
connectionTimeoutInMilliseconds = "5000" [number]
operationTimeoutInMilliseconds = "1000" [number]
connectionString = "<Valid StackExchange.Redis connection string>" [String]
settingsClassName = "<Assembly qualified class name that contains settings method specified below. Which basically return 'connectionString' value>" [String]
settingsMethodName = "<Settings method should be defined in settingsClass. It should be public, static, does not take any parameters and should have a return type of 'String', which is basically 'connectionString' value.>" [String]
loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String]
loggingMethodName = "<Logging method should be defined in loggingClass. It should be public, static, does not take any parameters and should have a return type of System.IO.TextWriter.>" [String]
redisSerializerType = "<Assembly qualified class name that implements Microsoft.Web.Redis.ISerializer>" [String]
/> -->
<add name="MyRedisOutputCache" type="Microsoft.Web.Redis.RedisOutputCacheProvider" host="localhost" accessKey="" applicationName="RedisCache" port="6379" ssl="false" />
</providers>
</outputCache>
</caching>



It is also possible to control how long time Image Data is to be kept in the cache in seconds. In web.config:
  <add key="RedisImageCacheTimeout" value="10" />



This sets a timeout of caching image data to 10 seconds for example. It is possible to change this of course, using 600 will cache image data for 10 minutes for example.

To cache an image, the following razor code show an example of its use:
 <img src="@Url.Action("ShowImage", "Images", new { id = Url.Encode("Croatia6-jpg") })" width="500" />

Using the syntax "filename"-"extension" like "myfile1-jpg" and using the embedded controller ImageController and action method ShowImage will also cache the image to Redis. A parameter of id is provided to the file to cache and load or retrieve from cache directly. This will be taken care of by the embedded images controller in the library. It is possible to use different image file formats, such as JPG, GIF, PNG and BMP.

Inside redis-cli Redis Client we can see that loading the image in the example will add a cache item in Redis:
 redis 127.0.0.1:6379> keys * 1) "RedisCache/IMAGEBANK/Content/Images/Croatia6.jpg" 

 Note that images must be put into the Content/Images folder of your MVC solution! A configuration of changing this can be added later to the library. We can also ask Redis how long the cache item will exist until it expires and is removed by using TTL (Time-To-Live): redis 127.0.0.1:6379> ttl "RedisCache/IMAGEBANK/Content/Images/Croatia6.jpg" (integer) 2 

Note that TTL will show -1 if the cache item is expired. Redis client got many other handful commands to control the Redis memory cache, see Redis documentation here: Redis documentation The way the RedisImageCacher is saving data is actually done very generic and can be extended to support other data and cache this. For implementation details, see: ASP.NET and Redis

RedisImageCacher Bitbucket repo

Or if you have Mercurial (hg) installed, from a command-line issue:
 hg clone https://bitbucket.org/toreaurstad/redisimagecacher