自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

解讀ASP.NET 5 & MVC6系列(8):Session與Caching

移動(dòng)開發(fā) Android
在之前的版本中,Session存在于System.Web中,新版ASP.NET 5中由于不在依賴于System.Web.dll庫了,所以相應(yīng)的,Session也就成了ASP.NET 5中一個(gè)可配置的模塊(middleware)了。

 在之前的版本中,Session存在于System.Web中,新版ASP.NET 5中由于不在依賴于System.Web.dll庫了,所以相應(yīng)的,Session也就成了ASP.NET 5中一個(gè)可配置的模塊(middleware)了。

配置啟用Session

ASP.NET 5中的Session模塊存在于Microsoft.AspNet.Session類庫中,要啟用Session,首先需要在project.json中的dependencies節(jié)點(diǎn)中添加如下內(nèi)容:

  1. "Microsoft.AspNet.Session""1.0.0-beta3" 
  2.  
  3. 然后在ConfigureServices中添加Session的引用(并進(jìn)行配置): 
  4.  
  5. services.AddCaching();  // 這兩個(gè)必須同時(shí)添加,因?yàn)镾ession依賴于Caching 
  6. services.AddSession(); 
  7. //services.ConfigureSession(null); 可以在這里配置,也可以再后面進(jìn)行配置

最后在Configure方法中,開啟使用Session的模式,如果在上面已經(jīng)配置過了,則可以不再傳入配置信息,否則還是要像上面的配置信息一樣,傳入Session的配置信息,代碼如下:

  1. app.UseInMemorySession(configure:s => { s.IdleTimeout = TimeSpan.FromMinutes(30); }); 
  2. //app.UseSession(o => { o.IdleTimeout = TimeSpan.FromSeconds(30); }); 
  3. //app.UseInMemorySession(null, null);   //開啟內(nèi)存Session 
  4. //app.UseDistributedSession(null, null);//開啟分布式Session,也即持久化Session 
  5. //app.UseDistributedSession(new RedisCache(new RedisCacheOptions() { Configuration = "localhost" })); 

對(duì)于UseInMemorySession方法,接收2個(gè)可選參數(shù),分別是:IMemoryCache可用于修改Session數(shù)據(jù)的默認(rèn)保存地址;Action委托則可以讓你修改默認(rèn)選項(xiàng),比如Session cookie的路徑、默認(rèn)的過期時(shí)間等。本例中,我們修改默認(rèn)過期時(shí)間為30分鐘。

注意:該方法必須在app.UseMvc之前調(diào)用,否則在Mvc里獲取不到Session,而且會(huì)出錯(cuò)。

獲取和設(shè)置Session

獲取和設(shè)置Session對(duì)象,一般是在Controller的action里通過this.Context.Session來獲取的,其獲取的是一個(gè)基于接口ISessionCollection的實(shí)例。該接口可以通過索引、Set、TryGetValue等方法進(jìn)行Session值的獲取和設(shè)置,但我們發(fā)現(xiàn)在獲取和設(shè)置Session的時(shí)候,我們只能使用byte[]類型,而不能像之前版本的Session一樣可以設(shè)置任意類型的數(shù)據(jù)。原因是因?yàn)?,新版本的Session要支持在遠(yuǎn)程服務(wù)器上存儲(chǔ),就需要支持序列化,所以才強(qiáng)制要求保存為byte[]類型。所以我們?cè)诒4鍿ession的時(shí)候,需要將其轉(zhuǎn)換為byte[]才能進(jìn)行保存,并且獲取以后要再次將byte[]轉(zhuǎn)換為自己的原有的類型才行。這種形式太麻煩了,好在微軟在Microsoft.AspNet.Http命名空間(所屬M(fèi)icrosoft.AspNet.Http.Extensions.dll中)下,為我們添加了幾個(gè)擴(kuò)展方法,分別用于設(shè)置和保存byte[]類型、int類型、以及string類型,代碼如下:

  1. public static byte[] Get(this ISessionCollection session, string key); 
  2. public static int? GetInt(this ISessionCollection session, string key); 
  3. public static string GetString(this ISessionCollection session, string key); 
  4. public static void Set(this ISessionCollection session, string key, byte[] value); 
  5. public static void SetInt(this ISessionCollection session, string key, int value); 
  6. public static void SetString(this ISessionCollection session, string key, string value); 

所以,在Controller里引用Microsoft.AspNet.Http命名空間以后,我們就可以通過如下代碼進(jìn)行Session的設(shè)置和獲取了:

  1. Context.Session.SetString("Name""Mike"); 
  2. Context.Session.SetInt("Age"21); 
  3. ViewBag.Name = Context.Session.GetString("Name"); 
  4. ViewBag.Age = Context.Session.GetInt("Age"); 

自定義類型的Session設(shè)置和獲取

前面我們說了,要保存自定義類型的Session,需要將其類型轉(zhuǎn)換成byte[]數(shù)組才行,在本例中,我們對(duì)bool類型的Session數(shù)據(jù)進(jìn)行設(shè)置和獲取的代碼,示例如下:

  1. public static class SessionExtensions 
  2.     public static bool? GetBoolean(this ISessionCollection session, string key) 
  3.     { 
  4.         var data = session.Get(key); 
  5.         if (data == null
  6.         { 
  7.             return null
  8.         } 
  9.         return BitConverter.ToBoolean(data, 0); 
  10.     }  
  11.  
  12.     public static void SetBoolean(this ISessionCollection session, string key, bool value) 
  13.     { 
  14.         session.Set(key, BitConverter.GetBytes(value)); 
  15.     } 

定義bool類型的擴(kuò)展方法以后,我們就可以像SetInt/GetInt那樣進(jìn)行使用了,示例如下:

  1. Context.Session.SetBoolean("Liar"true); 
  2. ViewBag.Liar = Context.Session.GetBoolean("Liar"); 

另外,ISessionCollection接口上還提供了Remove(string key)和Clear()兩個(gè)方法分別用于刪除某個(gè)Session值和清空所有的Session值的功能。但同時(shí)也需要注意,該接口并沒提供之前版本中的Abandon方法功能。

基于Redis的Session管理

使用分布式Session,其主要工作就是將Session保存的地方從原來的內(nèi)存換到分布式存儲(chǔ)上,本節(jié),我們以Redis存儲(chǔ)為例來講解分布式Session的處理。

先查看使用分布式Session的擴(kuò)展方法,示例如下,我們可以看到,其Session容器需要是一個(gè)支持IDistributedCache的接口示例。

public static IApplicationBuilder UseDistributedSession([NotNullAttribute]this IApplicationBuilder app, IDistributedCache cache, Action configure = null);

該接口是緩存Caching的通用接口,也就是說,只要我們實(shí)現(xiàn)了緩存接口,就可以將其用于Session的管理。進(jìn)一步查看該接口發(fā)現(xiàn),該接口中定義的Set方法還需要實(shí)現(xiàn)一個(gè)ICacheContext類型的緩存上下文(以便在調(diào)用的時(shí)候讓其它程序進(jìn)行委托調(diào)用),接口定義分別如下:

public interface IDistributedCache
{
    void Connect();
    void Refresh(string key);
    void Remove(string key);
    Stream Set(string key, object state, Action create);     bool TryGetValue(string key, out Stream value); }  public interface ICacheContext {     Stream Data { get; }     string Key { get; }     object State { get; }      void SetAbsoluteExpiration(TimeSpan relative);     void SetAbsoluteExpiration(DateTimeOffset absolute);     void SetSlidingExpiration(TimeSpan offset); }

 

接下來,我們基于Redis來實(shí)現(xiàn)上述功能,創(chuàng)建RedisCache類,并繼承IDistributedCache,引用StackExchange.Redis程序集,然后實(shí)現(xiàn)IDistributedCache接口的所有方法和屬性,代碼如下:

using Microsoft.Framework.Cache.Distributed;
using Microsoft.Framework.OptionsModel;
using StackExchange.Redis;
using System;
using System.IO;

namespace Microsoft.Framework.Caching.Redis
{
    public class RedisCache : IDistributedCache
    {
        // KEYS[1] = = key
        // ARGV[1] = absolute-expiration - ticks as long (-1 for none)
        // ARGV[2] = sliding-expiration - ticks as long (-1 for none)
        // ARGV[3] = relative-expiration (long, in seconds, -1 for none) - Min(absolute-expiration - Now, sliding-expiration)
        // ARGV[4] = data - byte[]
        // this order should not change LUA script depends on it
        private const string SetScript = (@"
                redis.call('HMSET', KEYS[1], 'absexp', ARGV[1], 'sldexp', ARGV[2], 'data', ARGV[4])
                if ARGV[3] ~= '-1' then
                  redis.call('EXPIRE', KEYS[1], ARGV[3]) 
                end
                return 1");
        private const string AbsoluteExpirationKey = "absexp";
        private const string SlidingExpirationKey = "sldexp";
        private const string DataKey = "data";
        private const long NotPresent = -1;

        private ConnectionMultiplexer _connection;
        private IDatabase _cache;

        private readonly RedisCacheOptions _options;
        private readonly string _instance;

        public RedisCache(IOptions optionsAccessor)         {             _options = optionsAccessor.Options;             // This allows partitioning a single backend cache for use with multiple apps/services.             _instance = _options.InstanceName ?? string.Empty;         }          public void Connect()         {             if (_connection == null)             {                 _connection = ConnectionMultiplexer.Connect(_options.Configuration);                 _cache = _connection.GetDatabase();             }         }          public Stream Set(string key, object state, Action create)         {             Connect();              var context = new CacheContext(key) { State = state };             create(context);             var value = context.GetBytes();             var result = _cache.ScriptEvaluate(SetScript, new RedisKey[] { _instance + key },                 new RedisValue[]                 {                     context.AbsoluteExpiration?.Ticks ?? NotPresent,                     context.SlidingExpiration?.Ticks ?? NotPresent,                     context.GetExpirationInSeconds() ?? NotPresent,                     value                 });             // TODO: Error handling             return new MemoryStream(value, writable: false);         }          public bool TryGetValue(string key, out Stream value)         {             value = GetAndRefresh(key, getData: true);             return value != null;         }          public void Refresh(string key)         {             var ignored = GetAndRefresh(key, getData: false);         }          private Stream GetAndRefresh(string key, bool getData)         {             Connect();              // This also resets the LRU status as desired.             // TODO: Can this be done in one operation on the server side? Probably, the trick would just be the DateTimeOffset math.             RedisValue[] results;             if (getData)             {                 results = _cache.HashMemberGet(_instance + key, AbsoluteExpirationKey, SlidingExpirationKey, DataKey);             }             else             {                 results = _cache.HashMemberGet(_instance + key, AbsoluteExpirationKey, SlidingExpirationKey);             }             // TODO: Error handling             if (results.Length >= 2)             {                 // Note we always get back two results, even if they are all null.                 // These operations will no-op in the null scenario.                 DateTimeOffset? absExpr;                 TimeSpan? sldExpr;                 MapMetadata(results, out absExpr, out sldExpr);                 Refresh(key, absExpr, sldExpr);             }             if (results.Length >= 3 && results[2].HasValue)             {                 return new MemoryStream(results[2], writable: false);             }             return null;         }          private void MapMetadata(RedisValue[] results, out DateTimeOffset? absoluteExpiration, out TimeSpan? slidingExpiration)         {             absoluteExpiration = null;             slidingExpiration = null;             var absoluteExpirationTicks = (long?)results[0];             if (absoluteExpirationTicks.HasValue && absoluteExpirationTicks.Value != NotPresent)             {                 absoluteExpiration = new DateTimeOffset(absoluteExpirationTicks.Value, TimeSpan.Zero);             }             var slidingExpirationTicks = (long?)results[1];             if (slidingExpirationTicks.HasValue && slidingExpirationTicks.Value != NotPresent)             {                 slidingExpiration = new TimeSpan(slidingExpirationTicks.Value);             }         }          private void Refresh(string key, DateTimeOffset? absExpr, TimeSpan? sldExpr)         {             // Note Refresh has no effect if there is just an absolute expiration (or neither).             TimeSpan? expr = null;             if (sldExpr.HasValue)             {                 if (absExpr.HasValue)                 {                     var relExpr = absExpr.Value - DateTimeOffset.Now;                     expr = relExpr <= sldExpr.Value ? relExpr : sldExpr;                 }                 else                 {                     expr = sldExpr;                 }                 _cache.KeyExpire(_instance + key, expr);                 // TODO: Error handling             }         }          public void Remove(string key)         {             Connect();              _cache.KeyDelete(_instance + key);             // TODO: Error handling         }     } }

 

在上述代碼中,我們使用了自定義類RedisCacheOptions作為Redis的配置信息類,為了實(shí)現(xiàn)基于POCO的配置定義,我們還繼承了IOptions接口,該類的定義如下:

public class RedisCacheOptions : IOptions {     public string Configuration { get; set; }      public string InstanceName { get; set; }      RedisCacheOptions IOptions.Options     {         get { return this; }     }      RedisCacheOptions IOptions.GetNamedOptions(string name)     {         return this;     } }

 

第三部,定義委托調(diào)用時(shí)使用的緩存上下文類CacheContext,具體代碼如下:

  1. using Microsoft.Framework.Cache.Distributed; 
  2. using System; 
  3. using System.IO; 
  4.  
  5. namespace Microsoft.Framework.Caching.Redis 
  6.     internal class CacheContext : ICacheContext 
  7.     { 
  8.         private readonly MemoryStream _data = new MemoryStream(); 
  9.  
  10.         internal CacheContext(string key) 
  11.         { 
  12.             Key = key; 
  13.             CreationTime = DateTimeOffset.UtcNow; 
  14.         } 
  15.  
  16.         ///          /// The key identifying this entry.         ///  
  17.         public string Key { get; internal set; } 
  18.  
  19.         ///          /// The state passed into Set. This can be used to avoid closures.         ///  
  20.         public object State { get; internal set; } 
  21.  
  22.         public Stream Data { get { return _data; } } 
  23.  
  24.         internal DateTimeOffset CreationTime { get; set; } // 可以讓委托設(shè)置創(chuàng)建時(shí)間 
  25.  
  26.         internal DateTimeOffset? AbsoluteExpiration { get; private set; } 
  27.  
  28.         internal TimeSpan? SlidingExpiration { get; private set; } 
  29.  
  30.         public void SetAbsoluteExpiration(TimeSpan relative) // 可以讓委托設(shè)置相對(duì)過期時(shí)間 
  31.         { 
  32.             if (relative <= TimeSpan.Zero) 
  33.             { 
  34.                 throw new ArgumentOutOfRangeException("relative", relative, "The relative expiration value must be positive."); 
  35.             } 
  36.             AbsoluteExpiration = CreationTime + relative; 
  37.         } 
  38.  
  39.         public void SetAbsoluteExpiration(DateTimeOffset absolute) // 可以讓委托設(shè)置絕對(duì)過期時(shí)間 
  40.         { 
  41.             if (absolute <= CreationTime) 
  42.             { 
  43.                 throw new ArgumentOutOfRangeException("absolute", absolute, "The absolute expiration value must be in the future."); 
  44.             } 
  45.             AbsoluteExpiration = absolute.ToUniversalTime(); 
  46.         } 
  47.  
  48.         public void SetSlidingExpiration(TimeSpan offset) // 可以讓委托設(shè)置offset過期時(shí)間 
  49.         { 
  50.             if (offset <= TimeSpan.Zero) 
  51.             { 
  52.                 throw new ArgumentOutOfRangeException("offset", offset, "The sliding expiration value must be positive."); 
  53.             } 
  54.             SlidingExpiration = offset; 
  55.         } 
  56.  
  57.         internal long? GetExpirationInSeconds() 
  58.         { 
  59.             if (AbsoluteExpiration.HasValue && SlidingExpiration.HasValue) 
  60.             { 
  61.                 return (long)Math.Min((AbsoluteExpiration.Value - CreationTime).TotalSeconds, SlidingExpiration.Value.TotalSeconds); 
  62.             } 
  63.             else if (AbsoluteExpiration.HasValue) 
  64.             { 
  65.                 return (long)(AbsoluteExpiration.Value - CreationTime).TotalSeconds; 
  66.             } 
  67.             else if (SlidingExpiration.HasValue) 
  68.             { 
  69.                 return (long)SlidingExpiration.Value.TotalSeconds; 
  70.             } 
  71.             return null
  72.         } 
  73.  
  74.         internal byte[] GetBytes() 
  75.         { 
  76.             return _data.ToArray(); 
  77.         } 
  78.     } 

最后一步定義,RedisCache中需要的根據(jù)key鍵獲取緩存值的快捷方法,代碼如下:

  1. using StackExchange.Redis; 
  2. using System; 
  3.  
  4. namespace Microsoft.Framework.Caching.Redis 
  5.     internal static class RedisExtensions 
  6.     { 
  7.         private const string HmGetScript = (@"return redis.call('HMGET', KEYS[1], unpack(ARGV))"); 
  8.  
  9.         internal static RedisValue[] HashMemberGet(this IDatabase cache, string key, params string[] members) 
  10.         { 
  11.             var redisMembers = new RedisValue[members.Length]; 
  12.             for (int i = 0; i < members.Length; i++) 
  13.             { 
  14.                 redisMembers[i] = (RedisValue)members[i]; 
  15.             } 
  16.             var result = cache.ScriptEvaluate(HmGetScript, new RedisKey[] { key }, redisMembers); 
  17.             // TODO: Error checking? 
  18.             return (RedisValue[])result; 
  19.         } 
  20.     } 

至此,所有的工作就完成了,將該緩存實(shí)現(xiàn)注冊(cè)為Session的provider的代碼方法如下:

  1. app.UseDistributedSession(new RedisCache(new RedisCacheOptions() 
  2.     Configuration = "此處填寫 redis的地址"
  3.     InstanceName = "此處填寫自定義實(shí)例名" 
  4. }), options => 
  5.     options.CookieHttpOnly = true
  6. }); 

參考:http://www.mikesdotnetting.com/article/270/sessions-in-asp-net-5

關(guān)于Caching

默認(rèn)情況下,本地緩存使用的是IMemoryCache接口的示例,可以通過獲取該接口的示例來對(duì)本地緩存進(jìn)行操作,示例代碼如下:

  1. var cache = app.ApplicationServices.GetRequiredService(); var obj1 = cache.Get("key1"); bool obj2 = cache.Get("key2"); 
  2.  
  3. 對(duì)于,分布式緩存,由于AddCaching,默認(rèn)將IMemoryCache實(shí)例作為分布式緩存的provider了,代碼如下: 
  4.  
  5. public static class CachingServicesExtensions 
  6.     public static IServiceCollection AddCaching(this IServiceCollection collection) 
  7.     { 
  8.         collection.AddOptions(); 
  9.         return collection.AddTransient

所以,要使用新的分布式Caching實(shí)現(xiàn),我們需要注冊(cè)自己的實(shí)現(xiàn),代碼如下:


  1. services.AddTransient 

基本的使用方法如下:

var cache = app.ApplicationServices.GetRequiredService(); cache.Connect(); var obj1 = cache.Get("key1"); //該對(duì)象是流,需要將其轉(zhuǎn)換為強(qiáng)類型,或自己再編寫擴(kuò)展方法 var bytes = obj1.ReadAllBytes();

 

責(zé)任編輯:chenqingxiang 來源: 湯姆大叔的博客
相關(guān)推薦

2015-06-18 16:29:14

ASP.NET

2015-06-17 14:42:04

ASP.NET

2015-06-17 17:01:48

ASP.NET

2015-06-29 10:00:02

ASP.NETMVC6

2015-06-16 15:01:59

ASP.NET 5

2015-06-17 16:45:28

ASP.NET

2015-06-17 16:01:30

ASP.NET

2015-06-18 14:13:36

ASP.NET

2015-06-18 17:04:22

ASP.NET

2015-06-18 16:39:17

ASP.NET

2015-06-17 10:16:30

ASP.NET

2015-06-17 14:16:50

ASP.NET

2015-06-17 14:09:36

ASP.NET

2015-06-18 17:37:19

ASP.NET

2015-06-17 14:18:50

ASP.NET

2015-06-18 17:34:25

ASP.NET

2015-06-18 14:25:56

ASP.NET

2014-08-26 09:22:40

ASP.NET MVCRouting

2015-06-30 14:58:20

技術(shù)周刊

2009-07-24 13:20:44

MVC框架ASP.NET
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)