zoukankan      html  css  js  c++  java
  • Redis安装使用

    Redis下载地址:https://github.com/MicrosoftArchive/redis/releases

    安装参考步骤参考:https://www.cnblogs.com/jaign/articles/7920588.html

    Redis初始化类RedisUtils:

      1 public class RedisUtils
      2     {
      3         private ConnectionMultiplexer _conn;
      4         private IDatabase _db;
      5 
      6         public RedisUtils(int dbNum)
      7         {
      8             _conn = RedisConnection.Current;
      9             _db = _conn.GetDatabase(dbNum);
     10         }
     11 
     12         public bool Exit(string key)
     13         {
     14             return _db.KeyExists(key);
     15         }
     16 
     17         public bool Exit(string haskKey, string key)
     18         {
     19             return _db.HashExists(haskKey, key);
     20         }
     21 
     22         public bool Set(string key, string value, int min = 0)
     23         {
     24             TimeSpan? time = null;
     25             if (min > 0) time = new TimeSpan(0, min, 0);
     26             return _db.StringSet(key, value, time);
     27         }
     28 
     29         public bool Set<T>(string key, T value, int min = 0)
     30         {
     31             TimeSpan? time = null;
     32             if (min > 0) time = new TimeSpan(0, min, 0);
     33             return _db.StringSet(key, Serialize(value), time);
     34         }
     35 
     36         public bool Set<T>(string hashKey, string key, T value)
     37         {
     38             return _db.HashSet(hashKey, key, Serialize(value));
     39         }
     40         /// <summary>
     41         /// 原始值存入 不自动转换
     42         /// </summary>
     43         /// <param name="hashKey"></param>
     44         /// <param name="key"></param>
     45         /// <param name="value"></param>
     46         /// <returns></returns>
     47         public bool Set(string hashKey, string key, string value)
     48         {
     49             return _db.HashSet(hashKey, key, value);
     50         }
     51 
     52         public string Get(string key)
     53         {
     54             if(string.IsNullOrEmpty(key)) return "";
     55 
     56             if (!Exit(key)) return "";
     57             return _db.StringGet(key);
     58         }
     59 
     60         public string Get(string hashKey, string key)
     61         {
     62             if (string.IsNullOrEmpty(hashKey)) return "";
     63             if (string.IsNullOrEmpty(key)) return "";
     64 
     65             if (!Exit(hashKey, key)) return "";
     66             return _db.HashGet(hashKey, key);
     67         }
     68 
     69         public T Get<T>(string key)
     70         {
     71             if (string.IsNullOrEmpty(key)) return default(T);
     72 
     73             if (!Exit(key)) return default(T);
     74             return Deserialize<T>(_db.StringGet(key));
     75         }
     76 
     77         public T Get<T>(string hashKey, string key)
     78         {
     79             if (string.IsNullOrEmpty(hashKey) || string.IsNullOrEmpty(key)) return default(T);
     80 
     81             if (!Exit(hashKey, key)) return default(T);
     82             return Deserialize<T>(_db.HashGet(hashKey, key));
     83         }
     84 
     85         public bool Remove(string key)
     86         { 
     87             if(string.IsNullOrEmpty(key)) return false;
     88 
     89             if (!Exit(key)) return true;
     90             return _db.KeyDelete(key);
     91         }
     92 
     93         public bool Remove(string HashKey, string key)
     94         {
     95             if (string.IsNullOrEmpty(HashKey) || string.IsNullOrEmpty(key)) return false;
     96 
     97             if (!Exit(HashKey, key)) return true;
     98             return _db.HashDelete(HashKey, key);
     99         }
    100 
    101         public void MultiRemove(string like)
    102         {
    103             _db.ScriptEvaluate(LuaScript.Prepare(
    104                 @"
    105                     local ks = redis.call('KEYS', @keyPara) 
    106                     for i=1,#ks,5000 do 
    107                     redis.call('del', unpack(ks, i, math.min(i+4999, #ks))) 
    108                     end 
    109                     return true 
    110                 "), new { keyPara = like }
    111             );
    112         }
    113 
    114         public void MultiRemove(string hashKey, string like)
    115         { 
    116             _db.ScriptEvaluate(LuaScript.Prepare(
    117                 @"
    118                     local ks = redis.call('hkeys', @hKey) 
    119                     local fkeys = {} 
    120                     for i=1,#ks do 
    121                     if string.find(ks[i], @keyPara) then 
    122                     fkeys[#fkeys + 1] = ks[i] 
    123                     end 
    124                     end 
    125                     for i=1,#fkeys,5000 do 
    126                     redis.call('hdel', @hKey, unpack(fkeys, i, math.min(i+4999, #fkeys))) 
    127                     end 
    128                     return true 
    129                 "
    130                 ), new { hKey = hashKey, keyPara = like }
    131             );
    132         }
    133 
    134         /// <summary>
    135         /// 序列化对象
    136         /// </summary>
    137         /// <param name="o"></param>
    138         /// <returns></returns>
    139         private byte[] Serialize(object o)
    140         {
    141             if (o == null)
    142             {
    143                 return null;
    144             }
    145             BinaryFormatter binaryFormatter = new BinaryFormatter();
    146             binaryFormatter.Binder = new UBinder();
    147             using (MemoryStream memoryStream = new MemoryStream())
    148             {
    149                 binaryFormatter.Serialize(memoryStream, o);
    150                 byte[] objectDataAsStream = memoryStream.ToArray();
    151                 return objectDataAsStream;
    152             }
    153         }
    154 
    155         /// <summary>
    156         /// 反序列化对象
    157         /// </summary>
    158         /// <typeparam name="T"></typeparam>
    159         /// <param name="stream"></param>
    160         /// <returns></returns>
    161         private T Deserialize<T>(byte[] stream) 
    162         {
    163             if (stream == null)
    164             {
    165                 return default(T);
    166             }
    167             BinaryFormatter binaryFormatter = new BinaryFormatter();
    168             binaryFormatter.Binder = new UBinder();
    169             using (MemoryStream memoryStream = new MemoryStream(stream))
    170             {
    171                 T result = (T)binaryFormatter.Deserialize(memoryStream);
    172                 return result;
    173             }
    174         }
    175 
    176         private class UBinder : SerializationBinder
    177         {
    178             public override Type BindToType(string assemblyName, string typeName)
    179             {
    180                 if (typeName.Contains("International.Web"))
    181                 {
    182                     typeName = typeName.Replace("Web", "PolicyUpload");
    183                     assemblyName = assemblyName.Replace("Web", "PolicyUpload");
    184                 }
    185                 else if (typeName.Contains("Models"))
    186                 {
    187                     typeName = typeName.Replace("International.Models", "PolicyUpload");
    188                     assemblyName = assemblyName.Replace("International.Models", "PolicyUpload");
    189                 }
    190 
    191                 return Type.GetType(string.Format("{0}, {1}", typeName, assemblyName));
    192             }
    193         }
    194     }
    View Code

    RedisConnection类:

     1  public class RedisConnection
     2     {
     3         private static string _conn = ConfigurationManager.AppSettings["RedisConn"]; //连接字符串
     4 
     5         private static Lazy<ConnectionMultiplexer> lazyCon = new Lazy<ConnectionMultiplexer>(() =>
     6         {
     7             return ConnectionMultiplexer.Connect(_conn);
     8         });
     9 
    10         public static ConnectionMultiplexer Current
    11         {
    12             get
    13             {
    14                 return lazyCon.Value;
    15             }
    16         }
    17     }
    View Code

    Redis可视化工具Redis Desktop Manager下载地址:https://github.com/uglide/RedisDesktopManager (参考资料:https://www.cnblogs.com/lbky/p/9861270.html)

     免费工具:https://gitee.com/qishibo/AnotherRedisDesktopManager


    Windows 下安装多个Redis 实例

    https://www.cnblogs.com/wangxingjian/p/12601592.html

  • 相关阅读:
    一个群发站内信的设计
    javascript typeof 小结
    setInterval,setTimeout的用法
    C#中常见异常类
    输入框关闭自动完成功能
    【转】javascript判断一个元素是否数组
    jquery的动态统计输入字符数方法
    giedview绑定数据格式化字符串
    jQuery 1.4单独为某个动画动作设效果
    GridView行编辑中找DropDownList控件
  • 原文地址:https://www.cnblogs.com/systemkk/p/11757842.html
Copyright © 2011-2022 走看看