标签:
1. 开始
对于分布式的缓存,平常的session的处理是一个用户对应一台分布式的机器,如果这台机器中途挂机或者不能处理这个用户session的情况发生,则此用户的session会丢失,会发生不可预知的错误。如下图:
如果用Redis的分布式缓存,则能避免上面的情况。因为session是保存在Redis中,不会有丢失的情况,就算中途有服务器A挂掉。如下图:
2. 代码
1)在包管理器中,输入下面的包,安装
Install-Package Microsoft.Web.RedisSessionStateProvider
2)可以看到web.config中自动添加了以下代码:
<sessionState mode="Custom" customProvider="MySessionStateStore" timeout="30"> <providers> <!-- 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‘. --> <!-- ‘throwOnError‘,‘retryTimeoutInMilliseconds‘,‘databaseId‘ and ‘applicationName‘ can be used with both options. --> <!-- <add name="MySessionStateStore" host = "127.0.0.1" [String] port = "" [number] accessKey = "" [String] ssl = "false" [true|false] throwOnError = "true" [true|false] retryTimeoutInMilliseconds = "5000" [number] 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="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="ceswebchat.redis.cache.chinacloudapi.cn" accessKey="m1JuDEstaqA+xxxxxxxxxxx" ssl="false" throwOnError = "true" retryTimeoutInMilliseconds = "5000" connectionTimeoutInMilliseconds = "5000" operationTimeoutInMilliseconds = "1000" databaseId = "0" /> </providers>
</sessionState>
上面可以看到,输入你所需的信息就可以了。很简单
{<Application Name>_<Session ID>}_Data
,如果多个应用程序共享相同的key,则这个参数是可选的。 如果不设置该参数将使用默认值。C# Azure 存储-分布式缓存Redis在session中的配置
标签:
原文地址:http://www.cnblogs.com/alunchen/p/5796473.html