zoukankan      html  css  js  c++  java
  • 使用redis

    通过 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);

  • 相关阅读:
    windows下编译及使用libevent
    安装和使用memcached
    BroadcastReceiver插件化解决方案
    Service插件化解决方案
    Activity插件化解决方案
    换肤-插件化
    资源的插件化
    startActivity进行Hook
    代理模式
    对反射的封装
  • 原文地址:https://www.cnblogs.com/81/p/9544087.html
Copyright © 2011-2022 走看看