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> /// 设值 /// </summary> /// <param name="value"></param> public void Set(object value) { if (string.IsNullOrEmpty(Key)) { return; } switch (this.cacheType) { case CacheType.Cache: RunTimeCache.Set(Key, value, TimeOut, this.timeOutType); break; case CacheType.Memcached: MemcachedCache.Set(this.DatabaseOrPath, Key, value, TimeOut, this.timeOutType); break; case CacheType.File: var filename = Barfoo.Library.IO.PathUtility.GetPhysicalPath(this.DatabaseOrPath); FileCache.Set(filename, value); break; case CacheType.Session: SessionCache.Set(Key, value); break; case CacheType.Redis: if (redis == null) { redis = RedisHelp.GetConnection(); } redis.Set(Key, value); break; } }
/// <summary> /// 清除缓存 /// </summary> public void RemoveCache() { if (string.IsNullOrEmpty(Key)) { return; } switch (this.cacheType) { case CacheType.Cache: RunTimeCache.RemoveCache(Key); break; case CacheType.Memcached: MemcachedCache.RemoveCache(this.DatabaseOrPath, Key); // 未经测试 break; case CacheType.File: var filename = Barfoo.Library.IO.PathUtility.GetPhysicalPath(this.DatabaseOrPath); FileCache.Remove(filename); break; case CacheType.Session: SessionCache.Remove(Key); break; case CacheType.Redis: if (redis == null) { redis = RedisHelp.GetConnection(); } redis.Remove(Key); // 未经测试 break; } }
/// <summary> /// 获取缓存的值 /// </summary> /// <returns></returns> public object Get() { if (string.IsNullOrEmpty(Key)) { return(null); } switch (this.cacheType) { case CacheType.Cache: return(RunTimeCache.Get(Key)); case CacheType.Memcached: return(MemcachedCache.Get(this.DatabaseOrPath, Key, timeOutType, TimeOut)); case CacheType.File: var filename = Barfoo.Library.IO.PathUtility.GetPhysicalPath(this.DatabaseOrPath); return(FileCache.Get(filename, TimeOut)); case CacheType.Session: return(SessionCache.Get(Key)); case CacheType.Redis: if (redis == null) { redis = RedisHelp.GetConnection(); } return(redis.Get <object>(Key)); // 类型会被转成 json 格式的字符串 default: return(null); } }
public string Get(int id) { if (RedisHelp.StringSet("test", "values")) { return(RedisHelp.StringGet("test")); } return("value"); }
/// <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> /// 获取hashID的实体数据 /// </summary> /// <exception cref="NullReferenceException">Return T class instatnce is Null</exception> /// <typeparam name="T">Defind model class it property must maping redis return HMGet Byte[][]</typeparam> /// <param name="hashid"></param> /// <returns>T instatnce</returns> public T Get <T>(string hashid) where T : class { T entity; try { var hKeys = RedisHelp.GetByteProperty <T>().Item1; var hValus = MasterRedisClient.HMGet(hashid, hKeys); entity = hValus.HValuesToEnity <T>(hKeys, MasterRedisClient); } catch (Exception ex) { throw ex; } return(entity); }
public UserService(IUserRepository _userRepository, IUnitOfWork _unitOfWork, IUserInfoRepository _userInfoRepository, HttpContextUtil _httpContextUtil, IUserDepartmentRepository _userDepartmentRepository, IDepartmentRepository _departmentRepository, IUserRoleRepository _userRoleRepository, IRoleRepository _roleRepository, IButtonActionRepository _buttonActionRepository, IHostingEnvironment _hostingEnv, RedisHelp _redisHelp) { userRepository = _userRepository; unitOfWork = _unitOfWork; userInfoRepository = _userInfoRepository; httpContextUtil = _httpContextUtil; userDepartmentRepository = _userDepartmentRepository; departmentRepository = _departmentRepository; userRoleRepository = _userRoleRepository; roleRepository = _roleRepository; buttonActionRepository = _buttonActionRepository; hostingEnv = _hostingEnv; redisHelp = _redisHelp; }
/// <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); }
public PlaceOrderController(ESHOPContext context, RedisHelp redisHelp) //依赖注入得到实例 { _context = context; _redisdb = redisHelp.GetDatabase(); }
public MenuInfoViewComponent(IUserService _userService, RedisHelp _redisHelp) { userService = _userService; redisHelp = _redisHelp; }
/// <summary> /// Set property value of class type use byte[][] array from redis database return /// </summary> /// <typeparam name="T"></typeparam> /// <param name="Hvalues">byte[][] arry of hash values from hash database</param> /// <returns></returns> public static T HValuesToEnity <T>(this byte[][] hValues, byte[][] hKeys, RedisClient db) where T : class { T entity; try { if (db == null) { LoggerFactory.Error("RedisClientExtend HValuesToEnity Method db is Null references."); throw new ArgumentException("RedisClientExtend HValuesToEnity Method db is Null references."); } entity = (T)Activator.CreateInstance(typeof(T)); var index = 0; /// TODO: 需要判断hKeys和hValues不相等的情况,记录到日志中 for (int i = 0; i < hValues.Length; i++) { var property = typeof(T).GetProperty(RedisHelp.GetString(hKeys[index])); var propertyType = property.DeclaringType; if (property != null) { var value = RedisHelp.GetString(hValues[index]); #region Value值设定 /// TODO:兼容各种Nullable的定义,在这转换成功 ///在定义Nullable类型时全部使用Nullable<int>等,不能使用 int?方式定义 if (!property.PropertyType.IsGenericType) { // 非泛型 property.SetValue(entity, string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, property.PropertyType), null); } else { //泛型Nullable<> Type genericTypeDefinition = property.PropertyType.GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(Nullable <>)) { property.SetValue(entity, string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, Nullable.GetUnderlyingType(property.PropertyType)), null); } } #endregion } else { LoggerFactory.Error("Get Property from redis database is not match with type of class property,May be number is not equals,or property name not equals,or class"); } index++; } return(entity); } catch (System.InvalidCastException castEx) { throw new ArgumentException(castEx.Message + "\n The exception help information as follo\n If Model class property delcare type is Nullable then please use genericType define,not use character ?" + "\n"); } catch (System.Exception ex) { throw ex; } }
public TestController(ApplicationConfigServices _applicationConfigServices, RedisHelp _redisHelp) : base(_applicationConfigServices) { redisHelp = _redisHelp; }
static void Main(string[] args) { //var a = RedisCache.Instance.GetString("name"); //Console.WriteLine(a); //RedisCache.Instance.InsertString("dd","1234"); //var a = RedisCache.Instance.GetString("dd"); //Console.WriteLine(a); //var list = new List<User>(); //list=RedisCache.Instance.Get(key, () => // { // list = new List<User>() // { // new User() {Name="詹宝华1",Sex="男",age=23 }, // new User() {Name="测试2",Sex="未知",age=11 }, // new User() {Name="王宝强3",Sex="女" } // }; // return list; // }); //foreach (var item in list) //{ // Console.WriteLine($"姓名:{item.Name}\t性别:{item.Sex}\t年龄:{item.age}"); //} RedisHelp redis = new RedisHelp(); var list = new List <User>() { new User() { Name = "詹宝华1", Sex = "男", age = 23 }, new User() { Name = "测试2", Sex = "未知", age = 11 }, new User() { Name = "王宝强3", Sex = "女" } }; redis.setValue <List <User> >("whys", list); var whys = redis.getValue <List <User> >("whys"); if (whys != null && whys.Count > 0) { whys.ForEach(f => Console.WriteLine($"{f.Name}\t{f.Sex}\t{f.age}")); } else { list.ForEach(f => Console.WriteLine($"{f.Name}\t{f.Sex}\t{f.age}")); } //whys.ForEach(a => Console.WriteLine($"{a.Name}\t{a.Sex}\t{a.age}")); // redis.FlushALL(); //redis.FlushDB(); //redis.setValueString("test","zhanbaohua",new TimeSpan(0,0,5)); Console.WriteLine(redis.getValueString("test")); Console.ReadKey(); }