Skip to content
July 6, 2012 / chadmeyercodez

Redis Session State Provider for ASP.NET

Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets. This makes it a perfect solution for session state caching. With it having expiration built in, there is no external jobs that need to run to clean up old sessions.

Using the MSDN article for a custom session-state store provider as a template, I created a way to interact with Redis for all session data. I have done some testing on the solution but not extensive yet. I hope to get it implemented into Fellowship One within the next couple of days which uses session state database currently for some real testing. I created the redis solution as a github repo.

Microsoft Open Tech has created a Windows supported Redis solution on github which is what I used to test the redis based session state provider. You can compile the solutions into executables to run the redis server and other redis monitoring tools.

to implement the solution, take the RedisSessionProvider.cs file from the SessionProvider folder and put this into your project. Next we have to tell our application to use a custom session state provider by inserting the following into the web.config.

<sessionState mode="Custom" customProvider="RedisSessionStateProvider" cookieless="false" timeout="1">
    <providers>
        <add name="RedisSessionStateProvider"
             type="RedisProvider.SessionProvider.CustomServiceProvider"
             server="localhost"
             port="6379"
             password=""
             writeExceptionsToEventLog="false">
    </providers>
</sessionState>

The values presented currently are the default redis values. These will probably change as you move your solution to production. The timeout is set to 1 currently so I could prove that session items were really dropping off the list. Also note that you can change the namespace of the custom service provider. If you do, just remember to change the type in the web.config to that namespace.

TODO List:

  • It is recommended that exceptions from session to captured in the application event log. This has yet to be implemented
  • I want to compiled this solution to a single dll so enhancements will be simplified. Instead of copying files and code, a quick reference to the dll will move session to redis.
About these ads

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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: