Redis backed ASP.NET providers written in C# using StackExchange.Redis. Includes the following providers:
OutputCacheProvider;SessionStateStoreProvider.
To use RedisAspNetProviders in your project it must meet the following requirements:
- .NET 4.0 or later.
- Redis 2.6.0 or later.
To build RedisAspNetProviders and run tests you will need Visual Studio 2013.
-
RedisAspNetProviderscan be installed via the nuget UI (as RedisAspNetProviders) or via the nuget package manager console:PM> Install-Package RedisAspNetProviders -
Alternatively, you can grab sources from here and build.
Add a connection string for Redis and specify the connectionStringName for provider:
<connectionStrings>
<add name="RedisConnectionString" connectionString="192.168.0.120,connectTimeout=5000" />
</connectionStrings>
<system.web>
...
<sessionState mode="Custom" customProvider="RedisSessionStateStoreProvider">
<providers>
<add name="RedisSessionStateStoreProvider"
type="RedisAspNetProviders.SessionStateStoreProvider, RedisAspNetProviders"
connectionStringName="RedisConnectionString" />
</providers>
</sessionState>
...
<caching>
<outputCache defaultProvider="RedisOutputCacheProvider">
<providers>
<add name="RedisOutputCacheProvider"
type="RedisAspNetProviders.OutputCacheProvider, RedisAspNetProviders"
connectionStringName="RedisConnectionString" />
</providers>
</outputCache>
</caching>
...
</system.web>... or specify the connectionString directly:
<system.web>
...
<sessionState mode="Custom" customProvider="RedisSessionStateStoreProvider">
<providers>
<add name="RedisSessionStateStoreProvider"
type="RedisAspNetProviders.SessionStateStoreProvider, RedisAspNetProviders"
connectionString="192.168.0.120,connectTimeout=10000" />
</providers>
</sessionState>
...
<caching>
<outputCache defaultProvider="RedisOutputCacheProvider">
<providers>
<add name="RedisOutputCacheProvider"
type="RedisAspNetProviders.OutputCacheProvider, RedisAspNetProviders"
connectionString="192.168.0.121,connectTimeout=10000" />
</providers>
</outputCache>
</caching>
...
</system.web>For additional information about configuring connection to Redis you can read `StackExchange.Redis``s documentation.
All providers support several optional parameters:
dbNumberallows you to specify a number of the database which will store sessions; by defaultdbNumber=0;keyPrefixallows you to specify a prefix for theRedisKeywhich will be used to store session state in Redis; by defaultkeyPrefix="".
<system.web>
...
<sessionState mode="Custom" customProvider="RedisSessionStateStoreProvider">
<providers>
<add name="RedisSessionStateStoreProvider"
type="RedisAspNetProviders.SessionStateStoreProvider, RedisAspNetProviders"
connectionString="192.168.0.120:6379"
dbNumber="1"
keyPrefix="MyWebApplication/SessionState/" />
</providers>
</sessionState>
...
<caching>
<outputCache defaultProvider="RedisOutputCacheProvider">
<providers>
<add name="RedisOutputCacheProvider"
type="RedisAspNetProviders.OutputCacheProvider, RedisAspNetProviders"
connectionString="192.168.0.121:6379,connectTimeout=10000"
dbNumber="1"
keyPrefix="MyWebApplication/OutputCache/" />
</providers>
</outputCache>
</caching>
...
</system.web>Note:
If you want to implement your own rules for choosing a Redis database or formatting a RedisKey you can override SessionStateStoreProvider.GetSessionStateStorageDetails(httpContext, sessionId) and OutputCacheProvider.GetCacheEntryStorageDetails(cacheEntryKey) methods. By default these methods return configured via dbNumber Redis database proxy and RedisKey which is generated as keyPrefix + sessionId for SessionStateStoreProvider and keyPrefix + cacheEntryKey for OutputCacheProvider.
All providers have protected virtual methods Serialize***() and Deserialize***(). Feel free to inherit from necessary provider and implement your own serialization mechanism.