我们公司每天有大量的订单要处理,项目也是老项目了,几乎所有的存值方式都用Session[" "]的方式,经常会报溢出的错误
最近领导决定加一个服务器,专门用来存缓存数据 redis 上场,那么配置方法五花八门,配置完了怎么用呢?
其实很简单:
只需要引用一两个文件:
StackExchange.Redis.StrongName
如果有依赖引用,就再引用一个
Microsoft.Web.RedisSessionStateProvider
注意(.netFromwork 3.5 或以下没有这个引用,通过其他途径也可以实现(比如:状态服务、数据库等方式))
引用完在webconfig自动生成了如下代码:
<sessionState mode="Custom" customProvider="MySessionStateStore" timeout="120" stateConnectionString="">
<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="" accessKey="" ssl="true" />
</providers>
</sessionState>
把其中的
<add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="" accessKey="" ssl="true" />
这段代码替换成这个就OK了
<add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" connectionString="10.90.13.7:10001,10.90.13.7:10002,10.90.13.7:10003" applicationName="PMSNet" ssl="false" />
其中:10.90.13.7 为ip 100002 是 端口号
完成以上傻瓜操作即可自动将实现redis, 如果想要看到效果可以到我上传的文件中找 redis Desktop Manager 工具进行查看key value
关注私聊从博主要视频亲自受教