Redis总结笔记:C#连接Redis简单例子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis总结笔记:C#连接Redis简单例子相关的知识,希望对你有一定的参考价值。

转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/113.html?1455860686

注:C#在调用Redis是不要使用ServiceStack.Redis驱动的4.0版本?,因为这个版本已经商业化了,会出现每小时6000条数据的限制

1、引用驱动

代码如下:


    using ServiceStack.Redis;


2、数据库连接

代码如下:


    RedisClient client;
            //连接服务器   6379是redis的默认端口
            client = new RedisClient("127.0.0.1", 6379);
    client.Password = "";//设置密码 没有可以注释

          //10万条数据测试,我发现使用set的效率明显比使用store的效率高,而且在测试过程中我发现store会丢失7-80条左右的数而set却一条都没有丢
          Stopwatch sw = new Stopwatch();
            sw.Start();

            for (int i = 0; i < 100000; i++)
            {
                client.Set<GPS>(Guid.NewGuid().ToString(), new GPS
                {
                    direction = 287,
                    gps_time = "1417622213418",
                    lati = 29.310586,
                    longi = 120.125143,
                    pla_no = "浙A12345",#p#分页标题#e#
                    pla_type = 1,
                    speed = 23.5,
                    state = 0,
                    carstate = 0,
                    upload_time = "1417622088418"
                });

                client.Store<GPS>(
                    new GPS
                {
                    direction = 287,
                    gps_time = "1417622213418",
                    lati = 29.310586,
                    longi = 120.125143,
                    pla_no = "浙A12345",
                    pla_type = 1,
                    speed = 23.5,
                    state = 0,
                    carstate = 0,
                    upload_time = "1417622088418"
                });
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);

 decimal price = client.Get<decimal>("price");//获取数据

 

以上是关于Redis总结笔记:C#连接Redis简单例子的主要内容,如果未能解决你的问题,请参考以下文章

redis in c#

c#程序怎么调用redis集群

C# BS消息推送 负载均衡-SignalR&Redis的配置

(21)C# 访问Redis的测试

7000字 Redis 超详细总结笔记总 | 收藏必备!

Ridis学习笔记