由於session基於本地cache,之前咱們本身寫分佈式緩存,或者數據庫存儲,或者cookie加密存儲,來保存用戶狀態信息,但較少的直接經過建立一個繼承 SessionStateStoreProviderBase 類,來實現自定義會話狀態存儲提供程序。但有ASP.NET官方的福利,咱們就不能放過。html
Microsoft.Web.RedisOutputCacheProvider,沒錯就是它,此庫底層用的正是StackExchange來訪問redisgit
固然,不止MS寫了,這個也是能夠滴:https://github.com/alex-simonov/RedisAspNetProvidersgithub
redis戳此入門:http://www.cnblogs.com/NotAnEmpty/p/5441127.htmlweb
研究內部實現可參考其下redis
參考:https://github.com/Azure/aspnet-redis-providerssql
https://msdn.microsoft.com/zh-cn/library/ms178587(VS.80).aspx數據庫
天色還沒有晚 媳婦還未還,還有時間 乾點正事吧...緩存
Install-Package Microsoft.Web.RedisSessionStateProvider
webconfig出現一小坨,若是用的是Azure的Redis,那你須要填一下host和accesskey,不然也沒啥好改的cookie
<sessionState mode="Custom" customProvider="MySessionStateStore"> <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] /> --> <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" connectionString="127.0.0.1:6379,allowadmin=true" accessKey="" ssl="true" /> </providers> </sessionState>
接着咱們看看它能幹啥?session
//Action Index中,就兩句,view直接輸出Model Session["UserAgent"] = Request.UserAgent; ViewData.Model = "訪問狀態已分佈式存儲session"; //而後咱們訪問另外一個About,而後直接輸出Session:@Model //不出意料的出現了:Session:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36 //固然大家是看不到,我反正看到了,但看不到也不是重點 ViewData.Model = Session["UserAgent"];
重點來了,有圖有真相,分佈式Session完畢
這麼屌,是否能搞OutputCacheProvider?搞搞不要錢
Install-Package Microsoft.Web.RedisOutputCacheProvider
依舊免費給的一大坨
<caching> <outputCache defaultProvider="MyRedisOutputCache"> <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'. --> <!-- 'databaseId' and 'applicationName' can be used with both options. --> <!-- <add name="MyRedisOutputCache" host = "127.0.0.1" [String] port = "" [number] accessKey = "" [String] ssl = "false" [true|false] 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] /> --> <add name="MyRedisOutputCache" type="Microsoft.Web.Redis.RedisOutputCacheProvider" connectionString="127.0.0.1:6379,allowadmin=true" accessKey="" ssl="true" /> </providers> </outputCache> </caching>
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
[OutputCache(Duration = 60, VaryByParam = "*")] public ActionResult RedisCacheIndex() { ViewData.Model = DateTime.Now.ToString(); return View(); }
固然訪問此頁面怎麼刷新都是同一個時間,但咱們要學會尋找重點
雖然真相就在眼前
歡迎轉載,來爬我啊:http://www.cnblogs.com/NotAnEmpty/p/5441127.html