Redis開發學習(3)_StackExchange.Redis_打印Server相關資訊




這次我們回到C#程式這邊使用StackExchange.Redis API進行開發

簡單來講其實就是把第二堂學習中指令的方式轉成用程式來進行操作而已


或許有人覺得上次介紹的安裝方式有點難
那我們在分享第二種方式導入StackExchange.Redis

Step1. Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution



Step2. 左側切換到 Online頁簽後
在去右上角搜尋欄查看看 Redis


一鍵點擊Install (一些描述在右側可以查看的到)


如果跳了窗顯示如下描述
就切換你的.NET Framework version再重試一次


對你的project右鍵進到屬性後的Application
將 default設的4.5改為4.6.1 再Try一次





還是不行調整成 4.7.2再Try一次
有缺framework就再自行下載
https://dotnet.microsoft.com/download/visual-studio-sdks?utm_source=getdotnetsdk&utm_medium=referral

這裡就不用這麼新的版本了
暫時使用1.2.6搭配.NET  Framework4.5
(在某些公司中可能會固定使用特定穩定版當在串其他庫時就要小心連鎖反應)
這種安裝方式會有個壞處就是只能抓最新版

這裡記得當你對一個方案進行過Install command執行後
之後方案中其他的project(可能後來才new的)要手動在自己 Add Reference





當你已經開啟Redis Server
而且也已經New好一個專案並導入StackExchange.Redis之後

就可以嘗試看看如下有關於Server作業的程式



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;

namespace RedisProject1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("啟動資料庫連線");
            ConnectionMultiplexer myConn = ConnectionMultiplexer.Connect("localhost:6379");
            var myEndpoints = myConn.GetEndPoints();
            var myServer = myConn.GetServer(myEndpoints.First());
            Console.WriteLine(new string('=', 50));
            Console.WriteLine("資料庫連線的DB 編號");//預設不寫就是直接返回Index:0的默認DB
            //Console.WriteLine(myConn.GetDatabase().Database);//The numeric identifier of this database
            Console.WriteLine(myConn.GetDatabase(0).Database);//The numeric identifier of this database
            Console.WriteLine(myConn.GetDatabase(1).Database);//The numeric identifier of this database
            Console.WriteLine(new string('=', 50));
            Console.WriteLine("資料庫連線狀態獲取");
            Console.WriteLine(myConn.GetStatus());
            Console.WriteLine(new string('=', 50));
            Console.WriteLine("資料庫連線Server 所有endpoint獲取");
            foreach(var endpoint in myConn.GetEndPoints()){
                Console.WriteLine(endpoint.ToString());
            }
            
            Console.WriteLine("資料庫連線Server endpoint總數");
            Console.WriteLine(myConn.GetEndPoints().Count());
            Console.WriteLine(new string('=', 50));
            Console.WriteLine("資料庫連線Server keys獲取");
            foreach(var key in myConn.GetServer(myConn.GetEndPoints().First()).Keys()){
                Console.WriteLine(key);
            }

            
            Console.WriteLine(new string('=',50));

            Console.WriteLine("Server連線時的狀況");
            Console.WriteLine("Server Current Time:{0}", myServer.Time());
            Console.WriteLine("Latency:{0}", myServer.Ping());
            Console.WriteLine("version:{0}" , myServer.Version);
            Console.WriteLine("Keys:{0}", string.Join(",", myServer.Keys().ToArray()));
            Console.WriteLine("IsSlave:{0}" , myServer.IsSlave);
            Console.WriteLine("IsConnected:{0}" , myServer.IsConnected);
            Console.WriteLine("ServerType:{0}" , myServer.ServerType);
            Console.WriteLine("OperationCount:{0}" , myServer.Multiplexer.OperationCount);
            Console.WriteLine("AllowSlaveWrites:{0}" , myServer.AllowSlaveWrites);


            Console.WriteLine(new string('=', 50));
            

            Console.ReadKey();
        }
    }
}

輸出












留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題