尽管Session在某些方面,并不尽如人意,关于它的种种利弊,也都是各类云(人云亦云、众说纷纭)。可在面对一些项目的时候,我不得不采用它来存储一些信息,来实现某些功能。在某项项目里,我采用Redis缓存服务,实现自定义Session。 git
在这里,我推荐两款基于Redis的Session框架,一种是Harbour.RedisSessionStateStore,可是它好像有段时间未更新了。咱们能够在这里https://www.nuget.org/packages/Harbour.RedisSessionStateStore/ github
经过nuget工具来安装到工程里。 缓存
gitHub的下载地址:https://github.com/TheCloudlessSky/Harbour.RedisSessionStateStore cookie
另外一种,是微软提供的 session
RedisSessionStateProvider 组件。安装方法以下图: app
RedisSessionStateProvider的安装方式,很是简单。安装完成后,只须要在你的Web.config中,添加下配置 框架
<sessionState mode="Custom" cookieName="TiKuSession" customProvider="MySessionStateStore"> <providers> <!-- Either use 'connectionString' and provide all parameters as string 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" port = "6379" 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] 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="127.0.0.1" port="6379" accessKey="" ssl="false" /> </providers> </sessionState>接下来,让咱们一块儿来感觉下。
预览下: less
看下咱们的Redis服务里存储了什么? ide