Sliding cache with Redis in .Net Web

For sliding cache & timeout with Redis Cache, here is a verification in web application, see result from a web page:

The web session was configured to use azure Redis Cache as session storage, its timeout was set to 1 minute.

First a session data was added at 2016/3/30 15:30:13, it supposed to expire at 2016/3/30 15:31:13, before its expiration, there were 2 read requests to the cached data with timestamp 2016/3/30 15:30:20 and 2016/3/30 15:30:32, later at 2016/3/30 15:31:35, another same read request didn’t get any data, that proved the cached data got expired.

Second another session data was added at 2016/3/30 15:31:42, after that there were several continuous read requests were made and succeeded, the time gap between those read requests were less than 1 minutes, then the cached data got still available at 2016/3/30 15:34:28, that was around 3 minutes later than it was created. So this proved sliding expiration.

  • How to go with your project
    Steps to implement sliding expiration of Redis Cache based session provider:
    1. Add latest Microsoft.Web.RedisSessionStateProvider by Visual Studio  Tools  Nuget Package Manager  Package Manage Console  select your project and input command to execute PM> Install-Package Microsoft.Web.RedisSessionStateProvider
    (you can also install this package by Nuget UI in your target project, see reference

2. Modify your web.config

<sessionState mode=”InProc” customProvider=”DefaultSessionProvider”>
<add name=”DefaultSessionProvider” type=”System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ connectionStringName=”DefaultConnection” />
</sessionState>–>   comment out this part of InProc Session State and add values as high lightened below with your redis cache connection info

<sessionState timeout=”1″ mode=”Custom” customProvider=”MySessionStateStore”>

<add name=”MySessionStateStore”
host = “”
port = “6380”
accessKey = “b/SIH24dU1aqByui01+tvjfiZ9J9t54NrijsZT/bAd4=”
ssl = “true”
throwOnError = “true”
retryTimeoutInMilliseconds = “5000”
databaseId = “0”
applicationName = “”
connectionTimeoutInMilliseconds = “5000”
operationTimeoutInMilliseconds = “1000”
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]
        <add name=”MySessionStateStore” type=”Microsoft.Web.Redis.RedisSessionStateProvider” host=”” accessKey=”b/SIH24*******d4=” ssl=”true” />   

3. Do your code part

The simple code I did in my sample is below, one for add session, the other is for read session
protected void Button1_Click(object sender, EventArgs e)
Session[“test”] = “hello, world from ” + DateTime.Now.ToString();
TextBox1.Text += “\r\n” + DateTime.Now.ToString() + ”  Add session –  ” + Session[“test”];

protected void Button2_Click(object sender, EventArgs e)
TextBox1.Text += “\r\n” + DateTime.Now.ToString() + ”  Read Session –  ” + Session[“test”];





Leave a Reply

Your email address will not be published. Required fields are marked *