.NET Core实现简单的Redis Client框架

2022-04-17 05:36:38
目录
0,关于RedisRESP1,定义数据类型2,定义异步消息状态机3,定义命令发送模板4,定义RedisClient5,实现简单的RESP解析6,实现命令发送客户端7,如何使用8,更多客户端9,更多测试10,性能测试

0,关于 Redis RESP

sk<bool> LPush(string key, string value) { await SendCommand($"{StringCommand.LPUSH} {key} {value}", out MessageStraceAnalysis<string> strace); var result = await strace.Task; return IsOk(result); } public async Task<string> LRange(string key, int start, int end) { await SendCommand($"{StringCommand.LRANGE} {key} {start} {end}", out MessageStraceAnalysis<string> strace); var result = await strace.Task; return result; } private static class StringCommand { public const string LPUSH = "LPUSH"; public const string LRANGE = "LRANGE"; // ... ... 更多 字符串的命令 } }}

集合:

using System;using System.Collections.Generic;using System.Text;using System.Threading.Tasks;namespace CZGL.RedisClient{    public class SetClient : CommandClient<SetClient>    {        internal SetClient() { }        internal SetClient(RedisClient client) : base(client)        {        }        public async Task<bool> SAdd(string key, string value)        {            await SendCommand($"{StringCommand.SADD} {key} {value}", out MessageStraceAnalysis<string> strace);            var result = await strace.Task;            return IsOk(result);        }        public async Task<string> SMembers(string key)        {            await SendCommand($"{StringCommand.SMEMBERS} {key}", out MessageStraceAnalysis<string> strace);            var result = await strace.Task;            return result;        }        private static class StringCommand        {            public const string SADD = "SADD";            public const string SMEMBERS = "SMEMBERS";            // ... ... 更多 字符串的命令        }    }}

有序集合:

using System;using System.Collections.Generic;using System.Text;using System.Threading.Tasks;namespace CZGL.RedisClient{    public class SortedClient : CommandClient<SortedClient>    {        internal SortedClient(RedisClient client) : base(client)        {        }        public async Task<bool> ZAdd(string key, string value)        {            await SendCommand($"{StringCommand.ZADD} {key} {value}", out MessageStraceAnalysis<string> strace);            var result = await strace.Task;            return IsOk(result);        }        private static class StringCommand        {            public const string ZADD = "ZADD";            public const string SMEMBERS = "SMEMBERS";            // ... ... 更多 字符串的命令        }    }}

这样,我们就有一个具有简单功能的 RedisClient 框架了。

9,更多测试

为了验证功能是否可用,我们写一些示例:

        static RedisClient client = new RedisClient("127.0.0.1", 6379);        static async Task Main(string[] args)        {            var a = await client.ConnectAsync();            if (!a)            {                Console.WriteLine("连接服务器失败");                Console.ReadKey();                return;            }            Console.WriteLine("连接服务器成功");            await StringSETGET();            await StringGETRANGE();            await StringGETSET();            await StringMGet();            Console.ReadKey();        }        static async Task StringSETGET()        {            var stringClient = client.GetStringClient();            var b = await stringClient.Set("seta", "6666");            var c = await stringClient.Get("seta");            if (c == "6666")            {                Console.WriteLine("true");            }        }        static async Task StringGETRANGE()        {            var stringClient = client.GetStringClient();            var b = await stringClient.Set("getrance", "123456789");            var c = await stringClient.GetRance("getrance", 0, -1);            if (c == "123456789")            {                Console.WriteLine("true");            }            var d = await stringClient.GetRance("getrance", 0, 3);            if (d == "1234")            {                Console.WriteLine("true");            }        }        static async Task StringGETSET()        {            var stringClient = client.GetStringClient();            var b = await stringClient.Set("getrance", "123456789");            var c = await stringClient.GetSet("getrance", "987654321");            if (c == "123456789")            {                Console.WriteLine("true");            }        }        static async Task StringMGet()        {            var stringClient = client.GetStringClient();            var a = await stringClient.Set("stra", "123456789");            var b = await stringClient.Set("strb", "123456789");            var c = await stringClient.Set("strc", "123456789");            var d = await stringClient.MGet("stra", "strb", "strc");            if (d.Where(x => x == "123456789").Count() == 3)            {                Console.WriteLine("true");            }        }

10,性能测试

因为只是写得比较简单,而且是单线程,并且内存比较浪费,我觉得性能会比较差。但真相如何呢?我们来测试一下:

        static RedisClient client = new RedisClient("127.0.0.1", 6379);        static async Task Main(string[] args)        {            var a = await client.ConnectAsync();            if (!a)            {                Console.WriteLine("连接服务器失败");                Console.ReadKey();                return;            }            Console.WriteLine("连接服务器成功");            var stringClient = client.GetStringClient();            Stopwatch watch = new Stopwatch();            watch.Start();            for (int i = 0; i < 3000; i++)            {                var guid = Guid.NewGuid().ToString();                _ = await stringClient.Set(guid, guid);                _ = await stringClient.Get(guid);            }            watch.Stop();            Console.WriteLine($"总共耗时:{watch.ElapsedMilliseconds} ms");            Console.ReadKey();        }

耗时:

总共耗时:1003 ms

大概就是 1s,3000 个 SET 和 3000 个 GET 共 6000 个请求。看来单线程性能也是很强的。

本文教程源码 github 地址:https://github.com/whuanle/RedisClientLearn

以上所述是小编给大家介绍的.NET Core实现简单的Redis Client框架,希望对大家有所帮助。在此也非常感谢大家对我们网站的支持!