Redis開發學習(1)_Windows上安裝教學_VisualStudio C# StackExchange.Redis套件配置教學




Redis 特點
Redis是屬於緩存性質資料庫而非永久儲存用的資料庫

1.高效能的key-value NoSQL資料庫
2.屬於內存資料庫
內存資料庫
優點:所有資料紀錄存於內存記憶體當中,避免緩慢磁碟I/O。(內存操作是遠遠快於磁碟中操作的)
缺點:存於內存的資料具有易失性
(當機房電腦跳電或是process crash的時候,此時資料就會遺失)
3.Redis對於資料易失性的優化機制
->備份資料是採用將資料寫入至文件中
主要分為兩種機制:
3-1.RDB
3-2.AOF
4.事件驅動機制
5.發布訂閱者(觀察者)的設計模式
6.key過期(資料過期查檢 for 緩存性資料)

有支援哪些程式語言(多種)
https://redis.io/clients



有在使用Redis技術的公司
https://redis.io/topics/whos-using-redis
https://en.wikipedia.org/wiki/Redis
(1) 2012年被FACEBOOK 以10億美元收購的
    Instagram(規模加老闆總計6人)
(2) Twitter
(3) Pinterest
(4) Github
(5) Snapchat
(6) Flickr
(7) Stackoverflow


在Redis剛出來時由於底層是用C寫的而且官網一般提供的
是Linux環境的配置教學和相應資源要重新編譯過才能使用
官方說法是Redis尚未正式支援Windows
之後由微軟負責提供Windows作業系統相應Redis軟體導入的維護,而釋出安裝包。
但好像維護到2016年就沒有在同步更新
所以目前GitHub尚安裝包最新只到2016年七月份最後釋出的穩定成熟版 3.2.100
一些更版釋出功能報告


Step1.Windows尚下載和配置Redis
官網連到Github的 Link: https://github.com/MicrosoftArchive/redis/releases

方式有兩種
(1)可以載.msi檔案做安裝執行
(2)也可以直接載壓縮包(十分建議!!!!)


安裝包方式










那之後可以再到預設下載的C Disk Program File路徑
去點開執行檔
如果不想藉由預設msi配置下載路徑做安裝則可以用壓縮包方式
你下載完後解壓縮完可以進到相應路徑做指令執行




這裡要先跟大家說明一下
在Windows版的 Redis是有兩個配置檔案的
一個是 redis.windows.conf
一個是 redis.windows-service.conf (默認指向的config檔案)

我們可以開啟服務後
去看redis欄位->右鍵內容



以解壓縮後自己手動載入
在執行檔鎖在路徑中所指向的conf檔案
目前指向是對的(因為已經重新做過service的啟動與載入)

若是直接用安裝版的
在下載安裝完時指向的默認檔案是redis.windows-service.conf
可能會導致你開不了因為已經在背景執行了
更有可能造成指向設置檔無效

conf檔中是可以讓你去設置ip port 或密碼等東西的




如下是切換到解壓縮完的路徑後執行redis-server.exe
這裡的話不要直接快點兩下會閃退!!!!
Step1-1. 啟動Redis Server


開命令提示字元
輸入: cd /d 壓縮完之存放目錄


輸入: redis-server redis.windows.conf
Windows下若透過安裝版(msi)直接安裝會自動啟動Redis Service
但不會綁定redis.windows.conf



當有顯示Redis方塊的icon畫面且有顯示The server is now ready的時候
就代表你的Redis Server已經開啟來了且預設port為6379
目前暫時以Local端作為Server
此外如果你是有要讓其他電腦端連線進來
還要確認FireWall的設定不會鎖到它(或者關閉)



Step1-2. Redis Server軟體佈署作業

此時我們可以再去另起一個命令提示字元
切換到相應目錄後
輸入 : redis-server --service-install redis.windows.conf

做 Redis Service的配置檔加載


Step1-3 開啟客戶端
輸入 : redis-cli.exe




ping 後頭接的回傳回來都會自動包雙引號

那也可以嘗試使用所謂的key-value插入的方式
塞一筆record

這裡
key塞入name
value塞入 Jack
當塞入成功(換行)則會顯示OK代表成功塞入
 接著再透過key獲取相應value



那以上就是一些小暖身跟前置測試

Step2. VisualStudio Redis StackExchange.Redis
開發套件設置教學
(請注意務必要先做上一動確認Radis Server有啟動
兩塊是密不可分的)

->上一動沒做
你接下來的程式碼就會報這種錯誤(不是語法也不是套件安裝問題!!!然後就會鬼打牆~~~~)
It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. SocketFailure on PING

其實不只一種Redis Client端套件可以使用
目前觀察資源較多且成熟的是這兩個而且也較常在更新代表還有在維護
(目前先介紹StackExchange這個套件)
https://github.com/StackExchange/StackExchange.Redis
https://github.com/ServiceStack/ServiceStack.Redis




這次我們使用的開發套件StackExchange.Redis 是由StackOverflow鎖開發維護的Tool
使程式開發者可以有便利API來針對Redis進行DB存取的一系列操作。

Step2-1.新增一Console Application專案


Step2-2. TOOLS -> NuGet Package Manager -> Package Manager Console



這裡參考Nuget網站上
https://www.nuget.org/packages/StackExchange.Redis/


輸入: Install-Package StackExchange.Redis -Version 2.0.588
(也可只Key Install-Package StackExchange.Redis 默認就是抓目前最新的版本 2.0.588)
這裡經過多個版本的測試後
如果是用.NET Framework4.0~.5的 要用的是 1.2.6的版本
自2開頭後的版本都要用到更高的.NET Framework (4.6.1~以上)

官方release說明
https://stackexchange.github.io/StackExchange.Redis/ReleaseNotes



若你的project是 .NET Framework4.5
而用2字頭去Install時會報的錯誤




改用1.2.6下載後就可以成功下載並加入參考了


接著撰寫一段簡單的測試code

連接到Redis Database的示範


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;

namespace MyRedisApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //ConnectionMultiplexer redisConn = ConnectionMultiplexer.Connect("localhost,allowAdmin=true,abortConnect=true");

            ConnectionMultiplexer redisConn = ConnectionMultiplexer.Connect("127.0.0.1:6379");
            IDatabase redisDb = redisConn.GetDatabase();
            redisDb.StringSet("key1", "value1");
            Console.WriteLine(redisDb.StringGet("key1"));
            Console.ReadKey();
        }
    }
}

執行結果就是跟剛剛一樣獲得value





Reference:

Redis介紹

資料庫的好夥伴:Redis
https://blog.techbridge.cc/2016/06/18/redis-introduction/?fbclid=IwAR1E57JIAl0sWhqNi81LhCGqrITCve7CRJdg5I6ULiygrjwLMc4LfwrjtK8



配置的參考

影片教學:
Redis Beginner Tutorial 3 - How to install REDIS on windows (step-by-step)
https://www.youtube.com/watch?v=ncFhlv-gBXQ

Installing Redis Cache On Windows
https://www.c-sharpcorner.com/article/installing-redis-cache-on-windows/

Redis 3.2 – Install Redis on Windows
https://dingyuliang.me/redis-3-2-install-redis-windows/

GitHub 上 window版本的Redis-x64-3.2.100.zip 下载没网速 以及安装
https://blog.csdn.net/qwer123456u/article/details/78693469

Windows Redis默認配置文件,Redis配置不生效解決方案
https://kknews.cc/other/6peo8mm.html

windows Redis绑定ip无效,Redis设置密码无效,Windows Redis 配置不生效,
Windows Redis requirepass不生效
https://fanshuyao.iteye.com/blog/2384074
Redis学习笔记
https://www.cnblogs.com/taoshengyujiu/p/6251630.html

使用VisualStudio編譯Redis
https://kknews.cc/tech/gqp5bqe.html

一直遇到的錯誤(剛開始不太懂怎麼配置順序搞錯~~)
StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail

https://blog.csdn.net/weixin_41392824/article/details/82756648

Connecting to Redis Cache
https://social.msdn.microsoft.com/Forums/azure/en-US/96887103-8ac8-4d9e-8b8f-67540dbd87cb/connecting-to-redis-cache?forum=azurecache


It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. SocketFailure on PING

https://stackoverflow.com/questions/29040223/it-was-not-possible-to-connect-to-the-redis-servers-to-create-a-disconnected

https://stackoverflow.com/questions/29040223/it-was-not-possible-to-connect-to-the-redis-servers-to-create-a-disconnected/29837042






StackExchange.Redis C#程式開發資源參考

https://github.com/StackExchange/StackExchange.Redis/blob/master/docs/Basics.md

https://stackexchange.github.io/StackExchange.Redis/Configuration.html

 NugetPackage StackExchange.Redis
https://www.nuget.org/packages/StackExchange.Redis/

Redis Cache Implementation using .NET

Redis for .NET Developer – Connecting with C#
http://taswar.zeytinsoft.com/redis-for-net-developer-connecting-with-c/

ASP.NET Core play with Redis and StackExchange.Redis
https://blackie1019.github.io/2017/04/16/ASP-NET-Core-play-with-Redis-and-StackExchange-Redis/

How to work with Redis Cache in .Net
https://www.infoworld.com/article/3187905/how-to-work-with-redis-cache-in-net.html



留言

這個網誌中的熱門文章

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

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

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