儘管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