ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis


目录

ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis

项目创建

StackExchange.Redis操作示例

引包【using StackExchange.Redis;】

ConnectionMultiplexer

RedisDBHelper

使用RedisDBHelper测试

有存储时间的String

Redis删除Key:KeyDelete

Redis的List操作

redis list范围查询

redis HashSet操作

redis Set操作

Redis ZSET


项目创建

为了测试方便,使用控制台应用程序即可。

这里创建.NET Core 3.1(长期支持)

可以看到,非常干净

StackExchange.Redis操作示例

打开NuGet包管理器,搜索【StackExchange.Redis】,本示例使用的是2.6.66版本

引包【using StackExchange.Redis;】

ConnectionMultiplexer

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            ConnectionMultiplexer conn = ConnectionMultiplexer.Connect("127.0.0.1:6379");
            IDatabase db = conn.GetDatabase();
            db.StringSet("userName","admin");
            RedisValue redisValue = db.StringGet("userName");
            Console.WriteLine(redisValue);
        }
    }
}

输出结果,可以看到admin是咱们拿到的值。 

RedisDBHelper

using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;

namespace demo_20221010_redis
{
    class RedisDBHelper
    {
        private ConnectionMultiplexer redis { get; set; }
        public IDatabase db { get; set; }
        /// <summary>
        /// 其实有个链接就行,一般都是默认本地。如果不是就再传
        /// </summary>
        /// <param name="connection"></param>
        public RedisDBHelper(string connection)
        {
            redis = ConnectionMultiplexer.Connect(connection);
            db = redis.GetDatabase();
        }
    }
}

使用RedisDBHelper测试

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            bool isf = redis.db.StringSet("userName","admin");
            if (isf)
            {
                RedisValue redisValue = redis.db.StringGet("userName");
                Console.WriteLine(redisValue.ToString());
            }
            else {
                Console.WriteLine("写入失败");
            }
        }
    }
}

有存储时间的String

这里输入的时间必须是TimeSpan类型。故而使用TimeSpan.FromSeconds(double);来输入时间。

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            bool isf = redis.db.StringSet("haveTime","我是存储时间",TimeSpan.FromSeconds(120));
            if (isf) {
                Console.WriteLine("存储值:"+redis.db.StringGet("haveTime").ToString()) ;
                Console.WriteLine("剩余时间:"+redis.db.KeyTimeToLive("haveTime"));
            }
        }
    }
}

 

Redis删除Key:KeyDelete

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            bool isf = redis.db.StringSet("haveTime","我是存储时间",TimeSpan.FromSeconds(120));
            if (isf) {
                //删除后再查询,肯定是空
                redis.db.KeyDelete("havaTime");
                Console.WriteLine("是否有值:"+redis.db.StringGet("havaTime"));
            }
        }
    }
}

 

没有值就是正确的,这个删除可以删除任何的可以,都是通用的。 

Redis的List操作

向左添加,其中ListRange是读取。

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            redis.db.KeyDelete("mylist");
            RedisValue[] arr = new RedisValue[10];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i]=  new RedisValue("正确"+i);
            }
            long isf = redis.db.ListLeftPush("mylist",arr);
            RedisValue[] list = redis.db.ListRange("mylist");
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }
    }
}

向右添加

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            redis.db.KeyDelete("mylist");
            RedisValue[] arr = new RedisValue[10];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i]=  new RedisValue("正确"+i);
            }
            long isf = redis.db.ListRightPush("mylist",arr);
            RedisValue[] list = redis.db.ListRange("mylist");
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }
    }
}

redis list范围查询

一般的时候list存储的值都是成千上万的,甚至几十上百万,所以我们需要好好处理一下。一般的情况下也就获取最后一千条左右的数据,如果都返回去了不就能让别人推算出后面的大致数据走向了吗?

这里我专门匹配的写了RedisValue,其实写字符串数组也行的。

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            redis.db.KeyDelete("mylist");
            RedisValue[] arr = new RedisValue[10];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = new RedisValue("正确" + i);
            }
            long isf = redis.db.ListRightPush("mylist", arr);
            RedisValue[] list = redis.db.ListRange("mylist",0,5);
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }
    }
}

redis HashSet操作

如果替换file的话直接写名字替换值即可。

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            redis.db.KeyDelete("myHashSet");
            redis.db.HashSet("myHashSet","id","9527");
            redis.db.HashSet("myHashSet","userName","admin");
            redis.db.HashSet("myHashSet","sex","男");
            redis.db.HashSet("myHashSet","age","22");
            redis.db.HashDelete("myHashSet","sex");
            HashEntry[] hashEntries = redis.db.HashGetAll("myHashSet");
            foreach (var item in hashEntries)
            {
                Console.WriteLine(item);
            }
        }
    }
}

redis Set操作

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            redis.db.KeyDelete("mySet");
            redis.db.SetAdd("mySet","a1");
            redis.db.SetAdd("mySet","a2");
            redis.db.SetAdd("mySet","a3");
            redis.db.SetAdd("mySet","a4");
            redis.db.SetAdd("mySet","a5");
            RedisValue[] redisValues = redis.db.SetMembers("mySet");
            foreach (var item in redisValues)
            {
                Console.WriteLine(item);
            }
        }
    }
}

获取Set长度

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            redis.db.KeyDelete("mySet");
            redis.db.KeyDelete("mySet1");
            redis.db.SetAdd("mySet","a1");
            redis.db.SetAdd("mySet","a2");
            redis.db.SetAdd("mySet","a3");
            redis.db.SetAdd("mySet","a4");
            redis.db.SetAdd("mySet","a5");
            redis.db.SetAdd("mySet1","a1");
            redis.db.SetAdd("mySet1","a2");
            redis.db.SetAdd("mySet1","a6");
            redis.db.SetAdd("mySet1","a7");
            long v1 = redis.db.SetLength("mySet");
            long v2 = redis.db.SetLength("mySet1");
            Console.WriteLine("v1:"+v1);
            Console.WriteLine("v2:"+v2);
        }
    }
}

Set查询交集

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            redis.db.KeyDelete("mySet");
            redis.db.KeyDelete("mySet1");
            redis.db.SetAdd("mySet","a1");
            redis.db.SetAdd("mySet","a2");
            redis.db.SetAdd("mySet","a3");
            redis.db.SetAdd("mySet","a4");
            redis.db.SetAdd("mySet","a5");
            redis.db.SetAdd("mySet1","a1");
            redis.db.SetAdd("mySet1","a2");
            redis.db.SetAdd("mySet1","a6");
            redis.db.SetAdd("mySet1","a7");
            RedisValue[] redisValues = redis.db.SetCombine(SetOperation.Intersect, "mySet", "mySet1");
            foreach (var item in redisValues)
            {
                Console.WriteLine(item);
            }
        }
    }
}

Set查询差集

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            redis.db.KeyDelete("mySet");
            redis.db.KeyDelete("mySet1");
            redis.db.SetAdd("mySet","a1");
            redis.db.SetAdd("mySet","a2");
            redis.db.SetAdd("mySet","a3");
            redis.db.SetAdd("mySet","a4");
            redis.db.SetAdd("mySet","a5");
            redis.db.SetAdd("mySet1","a1");
            redis.db.SetAdd("mySet1","a2");
            redis.db.SetAdd("mySet1","a6");
            redis.db.SetAdd("mySet1","a7");
            RedisValue[] redisValues = redis.db.SetCombine(SetOperation.Difference, "mySet", "mySet1");
            foreach (var item in redisValues)
            {
                Console.WriteLine(item);
            }
        }
    }
}

 

Set查询并集

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            redis.db.KeyDelete("mySet");
            redis.db.KeyDelete("mySet1");
            redis.db.SetAdd("mySet","a1");
            redis.db.SetAdd("mySet","a2");
            redis.db.SetAdd("mySet","a3");
            redis.db.SetAdd("mySet","a4");
            redis.db.SetAdd("mySet","a5");
            redis.db.SetAdd("mySet1","a1");
            redis.db.SetAdd("mySet1","a2");
            redis.db.SetAdd("mySet1","a6");
            redis.db.SetAdd("mySet1","a7");
            RedisValue[] redisValues = redis.db.SetCombine(SetOperation.Union, "mySet", "mySet1");
            foreach (var item in redisValues)
            {
                Console.WriteLine(item);
            }
        }
    }
}

Redis ZSET

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            redis.db.KeyDelete("myZset");
            redis.db.SortedSetAdd("myZset","a1",69);
            redis.db.SortedSetAdd("myZset","a2",79);
            redis.db.SortedSetAdd("myZset","a3",85);
            redis.db.SortedSetAdd("myZset","a4",82);
            redis.db.SortedSetAdd("myZset","a5",92);
            //分数范围
            RedisValue[] redisValues = redis.db.SortedSetRangeByScore("myZset", 70, 90);
            foreach (var item in redisValues)
            {
                Console.WriteLine(item);
            }
        }
    }
}

正序

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            redis.db.KeyDelete("myZset");
            redis.db.SortedSetAdd("myZset","a1",69);
            redis.db.SortedSetAdd("myZset","a2",79);
            redis.db.SortedSetAdd("myZset","a3",85);
            redis.db.SortedSetAdd("myZset","a4",82);
            redis.db.SortedSetAdd("myZset","a5",92);
            //正序
            SortedSetEntry[] sortedSetEntries = redis.db.SortedSetRangeByRankWithScores("myZset",0,-1,Order.Ascending);
            foreach (var item in sortedSetEntries)
            {
                Console.WriteLine(item);
            }
        }
    }
}

 

倒序

using StackExchange.Redis;
using System;

namespace demo_20221010_redis
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
            redis.db.KeyDelete("myZset");
            redis.db.SortedSetAdd("myZset","a1",69);
            redis.db.SortedSetAdd("myZset","a2",79);
            redis.db.SortedSetAdd("myZset","a3",85);
            redis.db.SortedSetAdd("myZset","a4",82);
            redis.db.SortedSetAdd("myZset","a5",92);
            //正序
            SortedSetEntry[] sortedSetEntries = redis.db.SortedSetRangeByRankWithScores("myZset",0,-1,Order.Descending);
            foreach (var item in sortedSetEntries)
            {
                Console.WriteLine(item);
            }
        }
    }
}

 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐