• Redis:在windows环境安装Redis


    Redis:在windows环境安装Redis

    第一步:

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

    第二步:

    在命令行执行:D: edis-2.6 edis-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 RedisStudy
    13 {
    14     public class User
    15     {
    16         public string Name { get; set; }
    17     }
    18 
    19     class Program
    20     {
    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); //blocking
    47                 }
    48             }
    49         }
    复制代码

    备注

    听说很多人用Redis做缓存和消息队列,就想尝试一下,这次先把安装环境弄好,有个基本了解了,找个时间继续学习:缓存和消息队列。

     
    分类: Redis
  • 相关阅读:
    对Spring <context:annotation-config/>的理解
    Javascript this指针
    go 打造世界最快的go模板引擎gorazor 2.0
    swagger 部署(Mac )
    Ab测试
    Nginx tcp限制并发、IP、记日志
    Nginx proxy_protocol协议与realip模块
    数据结构之回溯
    数据结构之分治
    数据结构之二分查找
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3197910.html
Copyright © 2020-2023  润新知