Below you will find pages that utilize the taxonomy term “StackExchange.Redis”
Posts
StackExchange.Redis - Scripting
使用 StackExchange.Redis 開發 Redis 的 Lua Scripting 程式,只要簡單的透過 Database 物件的 ScriptEvaluateAsync 方法,將 Lua script 及所需的 keys 與 values 帶入即可。
using StackExchange.Redis; ... var configuration = GetConfiguration(); using (var conn = ConnectionMultiplexer.Connect(configuration)) { var db = conn.GetDatabase(); var script = GetScript(); var keys = GetKeys(); var values = GetValues(); var result = db.ScriptEvaluate(script, keys, values); ... } ... 像是下面這邊筆者用 Lua script 實作了類似 Redis 的 MSET 命令,可同時設定三組 Key/Value,寫起來會像下面這樣:
using System; using StackExchange.Redis; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { var configuration = "localhost:6379"; using (var conn = ConnectionMultiplexer.
read morePosts
StackExchange.Redis - Pub/Sub Demo
使用 StackExchange.Redis 開發 Redis 的 Pub/Sub 程式,需先調用 GetSubscriber 方法取得 Subscriber 物件,再透過該 Subscriber 物件去設定事件訂閱以及發佈訂閱即可。
像是下面這邊筆者訂閱了 MemberOnLine 的事件,當事件發生時會將上線資訊顯示在主控台,然後發佈 LarryNung 上線的事件給訂閱者:
using System; using StackExchange.Redis; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { var configuration = "localhost:6379"; using (var conn = ConnectionMultiplexer.Connect(configuration)) { var sub = conn.GetSubscriber(); var key = "MemberOnLine"; sub.Subscribe(key, (c, v) => { Console.WriteLine("{0} Online...", v); }); sub.PublishAsync(key, "LarryNung", CommandFlags.FireAndForget); } } } } 運行結果如下:
{% img /images/posts/PubSubInStackExchange.
read morePosts
StackExchange.Redis - Server Command Demo
要使用 StackeExchange.Redis 取得 Server 的資訊,或是運行 Server 的命令。要先調用 GetServer 方法取得 Server 物件,再透過該 Server 成員屬性或方法去操作即可。
像是下面這邊筆者遍巡了每台 Server,並將其相關的資訊顯示到主控台:
using System; using StackExchange.Redis; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { var configuration = "localhost:6379"; using (var conn = ConnectionMultiplexer.Connect(configuration)) { var endPoints = conn.GetEndPoints(); foreach (var endPoint in endPoints) { var server = conn.GetServer(endPoint); Console.WriteLine("Server: {0}", server.Multiplexer); Console.WriteLine("IsSlave: {0}", server.IsSlave); Console.WriteLine("AllowSlaveWrites: {0}", server.AllowSlaveWrites); Console.WriteLine("ServerType: {0}", server.ServerType); Console.WriteLine("Version: {0}", server.Version); Console.WriteLine("ServerTime: {0}", server.
read morePosts
StackExchange.Redis - Configuration
欲連線至 Redis,需先設定 Configutaion,在 StackExchange.Redis 提供兩種設定方式,一種是用 ConfigurationOptions 物件直接宣告設定:
using StackExchange.Redis; ... var configuration = new ConfigurationOptions() { EndPoints = { {"localhost", 6379}, {"localhost", 6380} }, Password = "LarryNung" }; using (var conn = ConnectionMultiplexer.Connect(configuration)) { ... } ... 一種是透過字串的方式設定:
using StackExchange.Redis; ... var configuration = "localhost:6379,localhost:6380,password=LarryNung"; using (var conn = ConnectionMultiplexer.Connect(configuration)) { ... } ... 如果不清楚有哪些可供設定,可參閱下表:
{% img /images/posts/ConfigurationInStackExchange.Redis/1.png %}
{% img /images/posts/ConfigurationInStackExchange.Redis/2.png %}
read morePosts
StackExchange.Redis - A high performance general purpose redis client for .NET languages
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.
read more