首页 > 其他 > 详细

使用redis

时间:2018-08-27 20:14:47      阅读:198      评论:0      收藏:0      [点我收藏+]

通过 Nuget获取包StackExchange.Redis

写数据:

  ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:6379,password=CeshiPassword1");
   IDatabase db = redis.GetDatabase();
   db.StringSet("name", "我的名称", TimeSpan.FromSeconds(10));   //10s过期,也可不写

 

写数据:

  ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:6379,password=CeshiPassword1");
  IDatabase db = redis.GetDatabase();
 string str=  db.StringGet("name");   // key不存在时返回null
        db.KeyExpire("name", TimeSpan.FromSeconds(10));  //读操作不会对数据延时,此句重新延时10s,不是增加10s
            if (str == null)
                this.textBox1.Text = "null";
            else
                this.textBox1.Text = str;

 

查看还有多时生存时间:

 TimeSpan? sp=  db.KeyTimeToLive("name");

   

对于其他类型,可以序列化为string后写入。

  System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string json = serializer.Serialize(dict);

使用redis

原文:https://www.cnblogs.com/81/p/9544087.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!