Skip to content

Redis 数据结构 .NET Core 版代码示例(StackExchange.Redis)

🏷️ Redis .NET Core

Redis 数据结构 中简单介绍了 Redis 的 5 种数据结构及常用命令。
其中的示例是在命令行窗口执行的。

下面的代码则是 .NET Core 中通过 StackExchange.Redis 包实现的相同功能的示例。

其中最重要的一点区别就是很多命令(如 删除)在命令行中返回的是删除的元素数,而在 StackExchange.Redis 中返回则是 bool 类型。这点需要注意。

示例代码

csharp
using StackExchange.Redis;
using System;

namespace RedisDataTypes
{
    class Program
    {
        static void Main(string[] args)
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
            IDatabase db = redis.GetDatabase();

            Console.WriteLine("1. 字符串");
            Console.WriteLine($"db.StringSet(\"hello\", \"world\"): {db.StringSet("hello", "world")}"); // True
            Console.WriteLine($"db.StringGet(\"hello\"): {db.StringGet("hello")}"); // world
            Console.WriteLine($"db.KeyDelete(\"hello\"): { db.KeyDelete("hello")}"); // 1
            Console.WriteLine($"db.StringGet(\"hello\"): {db.StringGet("hello")}"); // [空]
            Console.WriteLine();

            Console.WriteLine("2. 列表");
            Console.WriteLine($"db.ListRightPush(\"list-key\", \"item\"): { db.ListRightPush("list-key", "item")}"); // 1
            Console.WriteLine($"db.ListRightPush(\"list-key\", \"item2\"): { db.ListRightPush("list-key", "item2")}"); // 2
            Console.WriteLine($"db.ListRightPush(\"list-key\", \"item\"): { db.ListRightPush("list-key", "item")}"); // 3
            Console.WriteLine("db.ListRange(\"list-key\", 0, -1): ");
            var listKeyValues = db.ListRange("list-key", 0, -1);
            for (int i = 0; i < listKeyValues.Length; i++)
            {
                Console.WriteLine($" {i+1}) {listKeyValues[i]}");
            }
            Console.WriteLine($"db.ListGetByIndex(\"list-key\", 1): {db.ListGetByIndex("list-key", 1)}"); // item2
            Console.WriteLine($"db.ListLeftPop(\"list-key\"): {db.ListLeftPop("list-key")}"); // item
            Console.WriteLine("db.ListRange(\"list-key\", 0, -1): ");
            listKeyValues = db.ListRange("list-key", 0, -1);
            for (int i = 0; i < listKeyValues.Length; i++)
            {
                Console.WriteLine($" {i + 1}) {listKeyValues[i]}");
            }
            Console.WriteLine($"db.KeyDelete(\"list-key\"): {db.KeyDelete("list-key")}"); // 1
            Console.WriteLine();

            Console.WriteLine("3. 集合");
            Console.WriteLine($"db.SetAdd(\"set-key\", \"item\"): { db.SetAdd("set-key", "item")}"); // 1
            Console.WriteLine($"db.SetAdd(\"set-key\", \"item2\"): { db.SetAdd("set-key", "item2")}"); // 1
            Console.WriteLine($"db.SetAdd(\"set-key\", \"item3\"): { db.SetAdd("set-key", "item3")}"); // 1
            Console.WriteLine($"db.SetAdd(\"set-key\", \"item\"): { db.SetAdd("set-key", "item")}"); // 0
            Console.WriteLine("db.SetMembers(\"set-key\"): ");
            var setMembers = db.SetMembers("set-key");
            for (int i = 0; i < setMembers.Length; i++)
            {
                Console.WriteLine($" {i + 1}) {setMembers[i]}");
            }
            Console.WriteLine($"db.SetContains(\"set-key\", \"item4\"): {db.SetContains("set-key", "item4")}"); // 0
            Console.WriteLine($"db.SetContains(\"set-key\", \"item2\"): {db.SetContains("set-key", "item2")}"); // 1
            Console.WriteLine($"db.SetRemove(\"set-key\", \"item2\"): {db.SetRemove("set-key", "item2")}"); // 1
            Console.WriteLine($"db.SetRemove(\"set-key\", \"item2\"): {db.SetRemove("set-key", "item2")}"); // 0
            Console.WriteLine("db.SetMembers(\"set-key\"): ");
            setMembers = db.SetMembers("set-key");
            for (int i = 0; i < setMembers.Length; i++)
            {
                Console.WriteLine($" {i + 1}) {setMembers[i]}");
            }
            Console.WriteLine($"db.KeyDelete(\"set-key\"): {db.KeyDelete("set-key")}"); // 1
            Console.WriteLine();

            Console.WriteLine("4. 散列");
            Console.WriteLine($"db.HashSet(\"hash-key\", \"sub-key1\", \"value1\"): { db.HashSet("hash-key", "sub-key1", "value1") }"); // 1
            Console.WriteLine($"db.HashSet(\"hash-key\", \"sub-key2\", \"value2\"): { db.HashSet("hash-key", "sub-key2", "value2") }"); // 1
            Console.WriteLine($"db.HashSet(\"hash-key\", \"sub-key1\", \"value1\"): { db.HashSet("hash-key", "sub-key1", "value1") }"); // 0
            Console.WriteLine("db.HashGetAll(\"hash-key\"): ");
            var hashMembers = db.HashGetAll("hash-key");
            for (int i = 0; i < hashMembers.Length; i++)
            {
                Console.WriteLine($" {i + 1}) {hashMembers[i]}");
            }
            Console.WriteLine($"db.HashDelete(\"hash-key\", \"sub-key2\"): {db.HashDelete("hash-key", "sub-key2")}"); // 1
            Console.WriteLine($"db.HashDelete(\"hash-key\", \"sub-key2\"): {db.HashDelete("hash-key", "sub-key2")}"); // 0
            Console.WriteLine($"db.HashGet(\"hash-key\", \"sub-key1\"): {db.HashGet("hash-key", "sub-key1")}"); // value1
            Console.WriteLine("db.HashGetAll(\"hash-key\"): ");
            hashMembers = db.HashGetAll("hash-key");
            for (int i = 0; i < hashMembers.Length; i++)
            {
                Console.WriteLine($" {i + 1}) {hashMembers[i]}");
            }
            Console.WriteLine($"db.KeyDelete(\"hash-key\"): {db.KeyDelete("hash-key")}"); // 1
            Console.WriteLine();

            Console.WriteLine("5. 有序集合");
            Console.WriteLine($"db.SortedSetAdd(\"zset-key\", \"member1\", 728): { db.SortedSetAdd("zset-key", "member1", 728) }"); // 1
            Console.WriteLine($"db.SortedSetAdd(\"zset-key\", \"member0\", 982): { db.SortedSetAdd("zset-key", "member0", 982) }"); // 1
            Console.WriteLine($"db.SortedSetAdd(\"zset-key\", \"member0\", 982): { db.SortedSetAdd("zset-key", "member0", 982) }"); // 0
            Console.WriteLine("db.SortedSetRangeByRankWithScores(\"zset-key\", 0, -1): ");
            var sortedSetMembers = db.SortedSetRangeByRankWithScores("zset-key", 0, -1);
            for (int i = 0; i < sortedSetMembers.Length; i++)
            {
                Console.WriteLine($" {i + 1}) {sortedSetMembers[i]}");
            }
            Console.WriteLine("db.SortedSetRangeByScoreWithScores(\"zset-key\", 0, 800): ");
            sortedSetMembers = db.SortedSetRangeByScoreWithScores("zset-key", 0, 800);
            for (int i = 0; i < sortedSetMembers.Length; i++)
            {
                Console.WriteLine($" {i + 1}) {sortedSetMembers[i]}");
            }
            Console.WriteLine($"db.SortedSetRemove(\"zset-key\", \"member1\"): {db.SortedSetRemove("zset-key", "member1")}"); // 1
            Console.WriteLine($"db.SortedSetRemove(\"zset-key\", \"member1\"): {db.SortedSetRemove("zset-key", "member1")}"); // 0
            Console.WriteLine("db.SortedSetRangeByRankWithScores(\"zset-key\", 0, -1): ");
            sortedSetMembers = db.SortedSetRangeByRankWithScores("zset-key", 0, -1);
            for (int i = 0; i < sortedSetMembers.Length; i++)
            {
                Console.WriteLine($" {i + 1}) {sortedSetMembers[i]}");
            }
            Console.WriteLine($"db.KeyDelete(\"zset-key\"): {db.KeyDelete("zset-key")}"); // 1
            Console.WriteLine();

            Console.WriteLine("press <enter> to exit.");
            Console.ReadLine();
        }
    }
}

打印结果

csharp
1. 字符串
db.StringSet("hello", "world"): True
db.StringGet("hello"): world
db.KeyDelete("hello"): True
db.StringGet("hello"): 

1. 列表
db.ListRightPush("list-key", "item"): 1
db.ListRightPush("list-key", "item2"): 2
db.ListRightPush("list-key", "item"): 3
db.ListRange("list-key", 0, -1): 
 1) item
 2) item2
 3) item
db.ListGetByIndex("list-key", 1): item2
db.ListLeftPop("list-key"): item
db.ListRange("list-key", 0, -1): 
 1) item2
 2) item
db.KeyDelete("list-key"): True

1. 集合
db.SetAdd("set-key", "item"): True
db.SetAdd("set-key", "item2"): True
db.SetAdd("set-key", "item3"): True
db.SetAdd("set-key", "item"): False
db.SetMembers("set-key"): 
 1) item
 2) item3
 3) item2
db.SetContains("set-key", "item4"): False
db.SetContains("set-key", "item2"): True
db.SetRemove("set-key", "item2"): True
db.SetRemove("set-key", "item2"): False
db.SetMembers("set-key"): 
 1) item
 2) item3
db.KeyDelete("set-key"): True

1. 散列
db.HashSet("hash-key", "sub-key1", "value1"): True
db.HashSet("hash-key", "sub-key2", "value2"): True
db.HashSet("hash-key", "sub-key1", "value1"): False
db.HashGetAll("hash-key"): 
 1) sub-key1: value1
 2) sub-key2: value2
db.HashDelete("hash-key", "sub-key2"): True
db.HashDelete("hash-key", "sub-key2"): False
db.HashGet("hash-key", "sub-key1"): value1
db.HashGetAll("hash-key"): 
 1) sub-key1: value1
db.KeyDelete("hash-key"): True

1. 有序集合
db.SortedSetAdd("zset-key", "member1", 728): True
db.SortedSetAdd("zset-key", "member0", 982): True
db.SortedSetAdd("zset-key", "member0", 982): False
db.SortedSetRangeByRankWithScores("zset-key", 0, -1): 
 1) member1: 728
 2) member0: 982
db.SortedSetRangeByScoreWithScores("zset-key", 0, 800): 
 1) member1: 728
db.SortedSetRemove("zset-key", "member1"): True
db.SortedSetRemove("zset-key", "member1"): False
db.SortedSetRangeByRankWithScores("zset-key", 0, -1): 
 1) member0: 982
db.KeyDelete("zset-key"): True

press <enter> to exit.