码迷,mamicode.com
首页 > 其他好文 > 详细

Redis连接方式

时间:2017-10-24 17:19:09      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:log   info   base   scribe   添加   gap   date   span   user   

  ConfigurationOptions op = new ConfigurationOptions();
            op.EndPoints.Add("192.168.1.202", 6379);
            op.Password = "mdsd";
            op.AbortOnConnectFail = false;

 ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(op);

第二种,使用用字符串

 

 ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.1.202:6379,password=mdsd");

            IDatabase db = redis.GetDatabase();

 随便写点用例

 //设置数据对象
            //db.StringSet("User", "{Name:\"TOM\"}");
            //添加数据对象
            //db.StringAppend("User", ",{Name:\"JACK\"}");
            //取得数据对象
            //string user = db.StringGet("User");
            //Console.WriteLine(user);

            db.StringSet("User", "{Name:\"TOM\"}", new TimeSpan(1, 0, 0));

            db.ListLeftPush("userinfo", "{Name:\"LALI\"}");
            db.ListSetByIndex("userinfo", 0, "{Name:\"TOM\"}");
            db.ListLeftPush("userinfo", "{Name:\"LALI\"}");
            db.ListRightPush("userinfo", "{Name:\"JACK\"}");

            var len = db.ListLength("userinfo");
            Console.WriteLine("data leng:" + len.ToString());

            for (int i = 0; i < len; i++)
            {
                Console.WriteLine(i.ToString() + ":" + db.ListGetByIndex("userinfo", i));
            }
            db.KeyDelete("userinfo");

订阅和发布

 

 ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(op);
            ISubscriber sub = redis.GetSubscriber();

            sub.Subscribe("*", (channel, message) =>
            {

                Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss fff    ") + (string)message);
            });

            sub.Publish("messages", "hello");

            //sub.PublishAsync("messages", "hello");
            //sub.PublishAsync("messages", "hello");
            //sub.PublishAsync("messages", "hello");
            //sub.PublishAsync("messages", "hello");
            //sub.PublishAsync("messages", "hello");
            //sub.PublishAsync("messages", "hello");
            //sub.PublishAsync("messages", "hello");

 

Redis连接方式

标签:log   info   base   scribe   添加   gap   date   span   user   

原文地址:http://www.cnblogs.com/mapy/p/7724361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!