close
文章出處

前言

上篇博文介紹了怎么樣在 asp.net core 中使用中間件,以及如何自定義中間件。項目中剛好也用到了Redis,所以本篇就介紹下怎么樣在 asp.net core 中使用 Redis 進行資源緩存和Session緩存。 如果你覺得對你有幫助的話,不妨點個【推薦】。

目錄

  • Redis 介紹
  • asp.net core Session 介紹
  • Redis & Session 實例講解
  • Session的使用
  • 使用 Protobuf 給 Session添加擴展方法

Redis 介紹

下面是Redis官網的介紹:

Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

Redis 是一個開源的(基于BSD許可),內存數據存儲結構,常用作數據庫,緩存,和消息隊列。它支持如字符串、哈希表、列表、集合、排序集范圍查詢、位圖、hyperloglogs半徑和地理空間索引與查詢。Redis內置主從連接,Lua腳本、LRU回收,事務和不同級別文件持久化,并提供了利用集群的主從切換和自動分區等來保證高可用性。

Redis的深入介紹我就直接開傳送門了,不是本篇的重點,但是我給你們整理了一下,你們還是要感謝我滴:

redis 高可用部署及監控:http://blog.sina.com.cn/s/blog_75ad98f30101fwqj.html
redis 主從連接:http://www.tuicool.com/articles/fAnYFb
redis 事務: http://redisbook.readthedocs.io/en/latest/feature/transaction.html
redis 內存回收LRU:http://www.open-open.com/lib/view/open1427547789059.html
redis 數據持久化:http://qifuguang.me/2015/10/13/Redis%E6%8C%81%E4%B9%85%E5%8C%96/

以上知識學習完,使用和面試時應付Redis的提問應該不成問題了。

asp.net core session 介紹

session本身是一個很難解釋的名詞,在http中session代表服務器與瀏覽器的一次會話過程,這個過程可能一直,也可能間斷的。

asp.net core中的session以中間件的方式提供給我們來使用。

下面來看一下使用方法:
首先,添加session的NuGet包Microsoft.AspNetCore.Http.Abstractions到項目中,在startup.cs文件的ConfigureServices(IServiceCollection services)函數中,使用app.UseSession()app.UseCaching()來使用session.

//在使用session之前要注入cacheing,因為session依賴于cache進行存儲
services.AddCaching();

services.AddSession();

添加了session之后就需要有存儲session的地方,可以使用內存存儲,也可以使用其他自定義存儲,比如redis或者SQL Server等。

// 重要: session的注冊必須在UseMvc之前,因為MVC里面要用 
app.UseSession();

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller}/{action}/{id?}",
        defaults: new { controller = "Home", action = "Index" });
});

Redis & Session 實例講解

要在Session中使用Redis,只需要把services.AddCaching();換成services.AddDistributedRedisCache();就可以了,如下:

services.AddDistributedRedisCache(option => 
    {
    //redis 數據庫連接字符串
        option.Configuration = Configuration.GetConnectionString("RedisConnection");
        
        //redis 實例名
        option.InstanceName = "master";
    }
);

Session的使用

在 asp.net core 的 MVC Controller 中,你可以HttpContext.Session來獲取Session對象。

如果不是在 Controller 中需要使用 Session 的話,可以使用IHttpContextAccessor這個接口通過注入的方式來獲取Session。

以下是在 Controller 中使用Session,需要引入Microsoft.AspNetCore.Http空間:

public class HomeController : Controller
{
    public IActionResult Index()
    { 
        HttpContext.Session.SetString("Test", "Ben Rules!");
        return View();
    }

    public IActionResult About()
    {
        ViewBag.Message = HttpContext.Session.GetString("Test");

        return View();
    }
}

以下是在除了 Controller 的其他地方使用 Session:

public class SomeOtherClass
{
    private readonly IHttpContextAccessor _httpContextAccessor;
    private ISession _session => _httpContextAccessor.HttpContext.Session;

    public SomeOtherClass(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public void TestSet()
    {
        _session.SetString("Test", "Ben Rules!");
    }

    public void TestGet()
    {
        var message = _session.GetString("Test");
    }
}

使用 Protobuf 給 Session添加擴展方法

默認情況下,我們只能存儲byte[]到我們的Session當中,這讓我們使用起來很不方便,在Microsoft.HttpCore.Extension中 Microsoft 給提供了一個SetString,SetInt32 和GetString,GetInt32的方法,但是在很多情況下,我們是需要使用Session來存儲一個對象的,那么此時就需要給Session添加一個擴展方法。

為了追求效率和性能,我們選擇Google的Protobuf序列化組件,而不使用Json.Net,在性能方面,Protobuf要比XML或者Json效率高很多。

在Nuget包中引入protobuf-net

public static class SessionExtensions
{
        public static T Get<T>(this ISession session, string key) where T : class {
            byte[] byteArray = null;
            if (session.TryGetValue(key, out byteArray)) {
                using (var memoryStream = new MemoryStream(byteArray)) {
                    var obj = ProtoBuf.Serializer.Deserialize<T>(memoryStream);
                    return obj;
                }
            }
            return null;
        }

        public static void Set<T>(this ISession session, string key, T value) where T : class {
            try {
                using (var memoryStream = new MemoryStream()) {
                    ProtoBuf.Serializer.Serialize(memoryStream, value);
                    byte[] byteArray = memoryStream.ToArray();
                    session.Set(key, byteArray);
                }
            }
            catch (Exception) {
                throw;
            }
           
        }
}

使用Protobuf-net序列化,需要在序列化的對象打上[ProtoContract][ProtoMember]等標記。

Ps:目前Redis的擴展Microsoft.Extensions.DependencyInjection下面的AddDistributedRedisCache還不支持RC2,可以去github上搜索源代碼,添加到項目中,也可以留下郵箱,我會發給你。


本文地址:http://www.cnblogs.com/savorboard/p/5592948.html
作者博客:Savorboard
歡迎轉載,請保留出處


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜

    AutoPoster 發表在 痞客邦 留言(0) 人氣()