写点什么

Redis:在 windows 环境安装 Redis

作者:喀拉峻
  • 2022 年 3 月 21 日
  • 本文字数:2122 字

    阅读完需:约 7 分钟

第一步:

下载 windows 版本的 Redis:https://github.com/MSOpenTech/Redis

第二步:

在命令行执行:D:\redis-2.6\redis-server.exe。



第三步:

这里有教程:https://github.com/ServiceStack/ServiceStack.Redis

C#版本的客户端类库



Write、Read 和 Remove 测试

代码下载:http://yunpan.cn/QtNrcGxnPRVdV


1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Threading; 7  8 using ServiceStack.Redis; 9 using ServiceStack.Text;10 using ServiceStack.Redis.Generic;11 12 namespace RedisStudy13 {14     public class User15     {16         public string Name { get; set; }17     }18 19     class Program20     {21         static void Main(string[] args)22         {23             Write();24             Read();25             Remove();26         }27 28         private static void Write()29         {30             using (var redisClient = new RedisClient())31             {32                 IRedisTypedClient<User> redis = redisClient.As<User>();33 34                 var users = redis.Lists["urn:users"];35 36                 users.Add(new User { Name = "段光伟" });37                 users.Add(new User { Name = "段光宇" });38 39                 redis.Save();40             }41         }42 43         private static void Read()44         {45             using (var redisClient = new RedisClient())46             {47                 IRedisTypedClient<User> redis = redisClient.As<User>();48 49                 var users = redis.Lists["urn:users"];50 51                 Console.WriteLine(users.Count);52 53                 redis.Save();54             }55         }56 57         private static void Remove()58         {59             using (var redisClient = new RedisClient())60             {61                 IRedisTypedClient<User> redis = redisClient.As<User>();62 63                 var users = redis.Lists["urn:users"];64 65                 redis.RemoveEntry(users);66             }67         }68     }69 }
复制代码


发布订阅测试


1         static void Main(string[] args) 2         { 3             var messagesReceived = 0; 4             var maxMessage = 5; 5             var channelName = "幸福框架"; 6  7             using (var redisConsumer = new RedisClient()) 8             { 9                 using (var subscription = redisConsumer.CreateSubscription())10                 {11                     subscription.OnSubscribe = channel =>12                     {13                         Console.WriteLine(String.Format("订阅频道:'{0}'", channel));14                     };15                     subscription.OnUnSubscribe = channel =>16                     {17                         Console.WriteLine(String.Format("取消订阅频道:'{0}'", channel));18                     };19                     subscription.OnMessage = (channel, msg) =>20                     {21                         Console.WriteLine(String.Format("从频道:'{0}'获取了消息:'{1}'", channel, msg));22 23                         if (++messagesReceived == maxMessage)24                         {25                             subscription.UnSubscribeFromAllChannels();26                         }27                     };28 29                     ThreadPool.QueueUserWorkItem(x =>30                     {31                         Thread.Sleep(200);32                         Console.WriteLine("开始发布消息");33 34                         using (var redisPublisher = new RedisClient())35                         {36                             for (var i = 1; i <= 5; i++)37                             {38                                 var message = "段光伟:" + DateTime.Now;39                                 Console.WriteLine(String.Format("发布消息:'{0}'到频道:'{1}'", message, channelName));40                                 redisPublisher.PublishMessage(channelName, message);41                             }42                         }43                     });44 45                     Console.WriteLine(String.Format("开始监听频道:'{0}'", channelName));46                     subscription.SubscribeToChannels(channelName); //blocking47                 }48             }49         }
复制代码



用户头像

喀拉峻

关注

左手Java右手Python,中间纹个C++ 2021.06.26 加入

还未添加个人简介

评论

发布
暂无评论
Redis:在windows环境安装Redis_redis_喀拉峻_InfoQ写作平台