StackExchange.Redis 是 StackExchange 提供的 redis client 實作。
該套件具有以下特點:
- High performance multiplexed design, allowing for efficient use of shared connections from multiple calling threads
- Abstraction over redis node configuration: the client can silently negotiate multiple redis servers for robustness and availability
- Convenient access to the full redis feature-set
- Full dual programming model both synchronous and asynchronous usage, without requiring “sync over async” usage of the TPL
- Support for redis “cluster”
使用上需先透過 NuGet 套件管理視窗安裝套件,他有提供兩種套件,一種是不含強命名簽章的套件 StackExchange.Redis,一種是有強命名簽章的套件 StackExchange.Redis.StrongName。
或是透過 Package Manager Console 輸入下列命令安裝套件。
Install-Package StackExchange.Redis
Install-Package StackExchange.Redis.StrongName
套件安裝完後,就可進行 Redis 存取部分的撰寫,程式撰寫起來會像下面這樣:
1 | using StackExchange.Redis; |
程式一開始會先設定 Configuration,相當於一般我們在用的 DB 連線字串,用以指定 Redis 位置等資訊。 再來會用 ConnectionMultiplexer.Connect 將 Configuration 帶入開啟連線,連線開啟後可對 Server 進行操作,像是取得 Server 的資訊,或是運行 Server 的命令。
1 | using StackExchange.Redis; |
可以對指定的 DB 進行操作。
1 | using StackExchange.Redis; |
抑或是處理訂閱都可以。
1 | using StackExchange.Redis; |
最後記得要將當初開啟的連線關閉就可以了(用 using 讓它自動處理也可以)。