NET Core中使用Redis


NET Core中使用Redis

注:本文提到的代碼示例下載地址> https://code.msdn.microsoft.com/How-to-use-Redis-in-ASPNET-0d826418

Redis 是一個開源的內存中的數據結構存儲系統,可以用作數據庫、緩存和消息中間件。它支持多種類型的數據結構:字符串,哈希表,列表,集合,有序集等等。

Redis 官方沒有推出Windows版本,倒是由Microsoft Open Tech提供了Windows 64bit 版本支持。

如何在Windows機器上安裝Redis=>下載安裝文件Redis-x64-3.2.100.msi,安裝完畢之后,打開service管理器,找到Redis服務,並將其啟動。

 

前期准備:

1.推薦使用Visual Studio 2015 Update3作為你的IDE,下載地址:www.visualstudio.com

2.你需要安裝.NET Core的運行環境以及開發工具,這里提供VS版:www.microsoft.com/net/core

創建項目:

打開VS 2015,新建->項目->C#模板->Web->ASP.NET Core Web Application(.NET Core)

 

 選擇好路徑,項目名為CSCoreRedis,確定后選擇Web Application,身份驗證選擇無。

項目創建完之后,在CSCoreRedis項目上右鍵選擇管理NuGet包,搜索StackExchange.Redis並安裝。

我們將用這個庫提供的接口去操作Redis。

代碼:

首先要在HomeController.cs中添加Redis的連接,如果你不是用的本地Redis服務,請自行修改連接字符串。   

復制代碼
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
    return ConnectionMultiplexer.Connect("localhost,abortConnect=false");
});

public static ConnectionMultiplexer Connection
{
    get
    {
        return lazyConnection.Value;
    }
}
復制代碼

添加構造函數,初始化database和List。這里使用ListLeftPush是為了在后面用ListRange的時候從左到右取能取到最新的數據。

復制代碼
public static string ListKeyName = "MessageList";
public HomeController()
{
    db = Connection.GetDatabase();
    if (db.IsConnected(ListKeyName) && (!db.KeyExists(ListKeyName) || !db.KeyType(ListKeyName).Equals(RedisType.List)))
    {
        //Add sample data.
        db.KeyDelete(ListKeyName);
        //Push data from the left
        db.ListLeftPush(ListKeyName, "TestMsg1");
        db.ListLeftPush(ListKeyName, "TestMsg2");
        db.ListLeftPush(ListKeyName, "TestMsg3");
        db.ListLeftPush(ListKeyName, "TestMsg4");
    }
}
復制代碼

修改Index.cshtml文件,添加輸入框及按鈕

<form action="/Home/SendMessage" method="post">
    <input type="text" name="message" style="width:250px" />
    <input name="btnSend" value="Send" type="submit" style="margin-left:5px" />
</form>

在controller中添加SendMessage方法

復制代碼
[HttpPost]
public ActionResult SendMessage(string message)
{   
    if (db.IsConnected(ListKeyName))
    {
        db.ListLeftPush(ListKeyName, message);
    }
    return RedirectToAction("Index");
}
復制代碼

顯示錯誤信息或信息列表

復制代碼
@if (@ViewData["Error"] != null)
{
    <h2>@ViewData["Error"]</h2>
}
else
{
    <div id="MessageList">
    <h3>Latest messages</h3>
    @foreach (var msg in Model)
    {
        <div>@Html.DisplayFor(modelItem => msg) </div>
    }
    </div> 
}
復制代碼

 我們來看一下運行結果

在輸入框中輸入字符,按下Send按鈕,頁面上將會顯示最新的5條信息。

 

下載完整的代碼,請訪問 https://code.msdn.microsoft.com/How-to-use-Redis-in-ASPNET-0d826418

要了解更多關於Redis的信息,請訪問Redis官方網站http://www.redis.io/

關於StackExchange.Redis的詳細文檔,請訪問https://github.com/StackExchange/StackExchange.Redis

如需了解微軟Azure Redis 緩存,請訪問https://azure.microsoft.com/zh-cn/services/cache/

更多腳本樣例, 訪問微軟One Code樣例庫:http://aka.ms/onescriptsamples 更多代碼樣例, 訪問微軟One Script樣例庫:http://aka.ms/onecodesamples


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM