中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

我(wo)心中(zhong)的核心組件(jian)(可插拔的AOP)~分布式Session組件(jian)

回到目錄

對于(yu)目前(qian)的(de)(de)(de)網(wang)站(zhan)來說,為了(le)(le)滿足高(gao)可(ke)用(yong),高(gao)并發(fa),高(gao)負(fu)載,一(yi)(yi)(yi)臺(tai)WEB服(fu)(fu)務器(qi)已(yi)經遠(yuan)遠(yuan)不夠用(yong)了(le)(le),以(yi)(yi)后(hou)的(de)(de)(de)WEB應(ying)用(yong)服(fu)(fu)務器(qi)應(ying)該(gai)是(shi)一(yi)(yi)(yi)種集群的(de)(de)(de)環境,它們(men)之(zhi)(zhi)間(jian)使用(yong)一(yi)(yi)(yi)些工具進(jin)(jin)行數(shu)據(ju)的(de)(de)(de)同步,在由1臺(tai)變成(cheng)多臺(tai)服(fu)(fu)務器(qi)時,有一(yi)(yi)(yi)個(ge)問題是(shi)我(wo)們(men)必須要考慮的(de)(de)(de),Session機(ji)制(zhi),我(wo)們(men)都知道Session被用(yong)來存(cun)(cun)儲用(yong)戶的(de)(de)(de)一(yi)(yi)(yi)些憑(ping)證(zheng)信息,持久化到服(fu)(fu)務器(qi)上,這(zhe)在安全性(xing)方面得到了(le)(le)保證(zheng)(比(bi)(bi)存(cun)(cun)儲到客戶端的(de)(de)(de)cookies),而當你的(de)(de)(de)WEB應(ying)用(yong)服(fu)(fu)務器(qi)是(shi)多臺(tai)時(多臺(tai)之(zhi)(zhi)間(jian)做(zuo)了(le)(le)負(fu)載均衡),這(zhe)種Session機(ji)制(zhi)就有問題了(le)(le),因為你無法實(shi)現(xian)(xian)(xian)(xian)從一(yi)(yi)(yi)臺(tai)服(fu)(fu)務器(qi)到別一(yi)(yi)(yi)臺(tai)服(fu)(fu)務器(qi)的(de)(de)(de)內存(cun)(cun)(緩存(cun)(cun))共(gong)享,這(zhe)也(ye)是(shi)不安全的(de)(de)(de),所以(yi)(yi),在出(chu)(chu)現(xian)(xian)(xian)(xian)緩存(cun)(cun)中(zhong)間(jian)件(jian)出(chu)(chu)現(xian)(xian)(xian)(xian)之(zhi)(zhi)后(hou),就有了(le)(le)一(yi)(yi)(yi)種新的(de)(de)(de)技術也(ye)跟著出(chu)(chu)現(xian)(xian)(xian)(xian)了(le)(le),即(ji)Session中(zhong)間(jian)件(jian),比(bi)(bi)較成(cheng)熟(shu)的(de)(de)(de)組件(jian)是(shi)StackExchange.Redis+RedisSessionStateProvider,即(ji)使用(yong)Redis實(shi)現(xian)(xian)(xian)(xian)的(de)(de)(de)Session機(ji)制(zhi),它將(jiang)session數(shu)據(ju)存(cun)(cun)儲到了(le)(le)一(yi)(yi)(yi)臺(tai)(可(ke)以(yi)(yi)是(shi)redis集群)redis服(fu)(fu)務器(qi)上,這(zhe)時,多臺(tai)WEB應(ying)用(yong)服(fu)(fu)務器(qi)之(zhi)(zhi)間(jian)的(de)(de)(de)數(shu)據(ju)就是(shi)唯一(yi)(yi)(yi)的(de)(de)(de)了(le)(le),不需要進(jin)(jin)行同步了(le)(le)。

下面介紹實現步驟

1 使用nuget安裝redis緩存 StackExchange.Redis
2 使用nuget安裝RedisSession服務  RedisSessionStateProvider
3 從nuget添加RedisSession之后,它會在你的config文件中寫入以下內容,主要是對session進行持久化設置的
  

 <sessionState mode="Custom" 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" [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]
            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" accessKey="" ssl="false" />
      </providers>
    </sessionState>

4 下載是新版本的redis服務端,可以是windows版的,我用的是2.6.13,低版本的redis會出現Eval命令無法識別的問題
5 處理完成,可以測(ce)試(shi)你的session了,默認過期時(shi)間為1200秒

前臺使用Session時和原來沒有任何分別

     public ActionResult TestSession()
        {
            Session["username"] = "zzl";
            Session["test"] = "hello world!";
            return Content("完(wan)成..." + Session["test"]);
        }

這種不修(xiu)改以(yi)有代碼就(jiu)可以(yi)實現不同(tong)功能策略(lve)的(de)方(fang)式(shi)就(jiu)是我們的(de)IoC,控制反轉技術(也(ye)是依賴注入DI的(de)范疇),這也(ye)是編(bian)程語言發展到一定階段的(de)必然(ran)產物,即解決復雜(za)的(de)業務的(de)應對之(zhi)道!

下面是網友(you)對rediscache進行(xing)的擴(kuo)展,讓它支(zhi)持(chi)復(fu)雜類型(xing),默(mo)認支(zhi)持(chi)簡單類型(xing),如果希望支(zhi)持(chi)復(fu)雜類型(xing),需要為類型(xing)添加“可(ke)序列化”的特(te)性,因為我們在redis里存儲(chu)時,都是以二進制字節數(shu)組的形式存儲(chu)的。

  /// <summary>
    /// 對RedisCache的(de)擴(kuo)展,讓它支持(chi)復雜類(lei)型、
    /// RedisValue 類型可以直接使用(yong)字節數組,因此,
    /// 調用 Get 幫助程序(xu)方法時,它會將對象序(xu)列化為字節流(liu),然后再緩存該對象。
    /// 檢索項目(mu)(mu)時,項目(mu)(mu)會(hui)重新序列化(hua)為對象,然后(hou)返回給調(diao)用程(cheng)序。
    /// </summary>
    public static class SampleStackExchangeRedisExtensions
    {
        public static T Get<T>(this IDatabase cache, string key)
        {
            return Deserialize<T>(cache.StringGet(key));
        }

        public static object Get(this IDatabase cache, string key)
        {
            return Deserialize<object>(cache.StringGet(key));
        }

        public static void Set(this IDatabase cache, string key, object value)
        {
            cache.StringSet(key, Serialize(value));
        }

        static byte[] Serialize(object o)
        {
            if (o == null)
            {
                return null;
            }

            BinaryFormatter binaryFormatter = new BinaryFormatter();
            using (MemoryStream memoryStream = new MemoryStream())
            {
                binaryFormatter.Serialize(memoryStream, o);
                byte[] objectDataAsStream = memoryStream.ToArray();
                return objectDataAsStream;
            }
        }

        static T Deserialize<T>(byte[] stream)
        {
            if (stream == null)
            {
                return default(T);
            }

            BinaryFormatter binaryFormatter = new BinaryFormatter();
            using (MemoryStream memoryStream = new MemoryStream(stream))
            {
                T result = (T)binaryFormatter.Deserialize(memoryStream);
                return result;
            }
        }


    }

對復雜類型進(jin)行存儲和(he)讀取的(de)方法(fa)如(ru)下(xia)

   cache.Set("zzlList", new List<TestCache> { new TestCache { ID = 1, Name = "占占", AddTime = DateTime.Now } });
  var o = cache.Get("zzlList");

 回到目錄

posted @ 2015-07-29 10:06  張占嶺  閱讀(1778)  評論(0)    收藏  舉報