private static void HMSet() { try { IRHash hash = new RHash(); var keys = new byte[][] { RedisHelp.GetByte("name"), RedisHelp.GetByte("price"), RedisHelp.GetByte("width"), RedisHelp.GetByte("productor data"), }; var values = new byte[][] { RedisHelp.GetByte("SKU-3"), RedisHelp.GetByte("200.10"), RedisHelp.GetByte("25"), RedisHelp.GetByte(DateTime.Now.ToString()), }; hash.MSet(entityHash, keys, values); } catch (Exception ex) { LoggerFactory.Error( String.Format("数据查询异常,HMSet", "HMSet"), ex); } }
/// <summary> /// 将单个 field-value (域-值)对设置到哈希表 key 中 /// </summary> /// <param name="hashID"></param> /// <param name="hashKey">读取的键</param> /// <returns></returns> public long Set(string hashID, string hashKey, string hashVal) { var result = 0L; try { result = MasterRedisClient.HSet(hashID, RedisHelp.GetByte(hashKey), RedisHelp.GetByte(hashVal)); } catch (RedisException ex) { throw ex; } return(result); }
/// <summary> /// 读取单个hash的值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="hashID"></param> /// <param name="hkey"></param> /// <returns></returns> public T Get <T>(string hashID, string hkey) { var result = default(T); try { var getValue = MasterRedisClient.HGet(hashID, RedisHelp.GetByte(hkey)); result = (T)Convert.ChangeType(RedisHelp.GetString(getValue), typeof(T)); } catch (RedisException ex) { throw ex; } return(result); }
/// <summary> /// 提供泛型操作方法,直接保存实体数据到Redis的Hash中 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="hashID"></param> /// <param name="entiy"></param> public bool Save <T>(string hashID, T entiy) where T : class { var result = false; try { var entityType = entiy.GetType(); var entityPropers = entityType.GetProperties(); // 定义一纬包含一个一纬数组值的二维数组 var hashKeys = new byte[entityPropers.Length][]; var hashValues = new byte[entityPropers.Length][]; #region 赋值 var index = 0; foreach (var item in entityPropers) { hashKeys[index] = RedisHelp.GetByte(item.Name); var itemPro = entityType.GetProperty(item.Name); if (itemPro != null) { var keyVal = string.Empty; if (itemPro.GetValue(entiy, null) != null) { keyVal = (itemPro.GetValue(entiy, null)).ToString(); } hashValues[index] = RedisHelp.GetByte(keyVal); } index++; } MSet(hashID, hashKeys, hashValues); #endregion result = true; } catch (Exception ex) { throw ex; } return(result); }