public T Get <T>(string key) { try { using (var redis = RedisManager.GetClient()) { return(redis.Get <T>(key)); } } catch (Exception ex) { throw new Exception("Get<T>() =>> " + ex.Message); } }
/// <summary> /// 批量添加list /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="datetime"></param> public void AddListRange(string key, List <string> value, DateTime?datetime = null) { try { using (var redis = RedisManager.GetClient()) { redis.AddRangeToList(key, value); if (datetime != null) { redis.ExpireEntryAt(key, datetime.Value); } } } catch (Exception ex) { throw new Exception("AddListRange() =>> " + ex.Message); } }
public bool Set <T>(string key, T value, DateTime?datetime = null) { try { using (var redis = RedisManager.GetClient()) { if (datetime != null) { return(redis.Set <T>(key, value, datetime.Value)); } return(redis.Set <T>(key, value)); } } catch (Exception ex) { throw new Exception("Set<T>() =>> " + ex.Message); } }
/// <summary> /// 批量添加hash /// </summary> /// <param name="hashid"></param> /// <param name="key"></param> /// <param name="KeyValuePair"></param> public void AddHashRange(string hashid, IEnumerable <KeyValuePair <string, string> > KeyValuePair, DateTime?datetime = null) { try { using (var redis = RedisManager.GetClient()) { redis.SetRangeInHash(hashid, KeyValuePair); if (datetime != null) { redis.ExpireEntryAt(hashid, datetime.Value); } } } catch (Exception ex) { throw new Exception("AddHashRange() =>> " + ex.Message); } }
/// <summary> /// 向list初始位置插入值 /// </summary> /// <param name="key">listid,数组key</param> /// <param name="value">本次插入的值</param> public void PrependItemToList(string key, string value, DateTime?datetime = null) { try { using (var redis = RedisManager.GetClient()) { redis.PrependItemToList(key, value); if (datetime != null) { redis.ExpireEntryAt(key, datetime.Value); } } } catch (Exception ex) { throw new Exception("PrependItemToList() =>> " + ex.Message); } }
/// <summary> /// 根据hashid获取【所有value】 /// </summary> /// <param name="hashid"></param> /// <returns></returns> public Dictionary <string, string> GetHashAll(string hashid) { try { using (var redis = RedisManager.GetClient()) { byte[][] bbyte = redis.HGetAll(hashid); Dictionary <string, string> dic = new Dictionary <string, string>(); for (int i = 0; i < bbyte.Length; i += 2) { string key = bbyte[i].FromUtf8Bytes(); string value = bbyte[i + 1].FromUtf8Bytes(); dic.Add(key, value); } return(dic); } } catch (Exception ex) { throw new Exception("GetHashAll() =>> " + ex.Message); } }
/// <summary> /// Hash设置值 key存在则覆盖 /// </summary> /// <param name="hashid"></param> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool HashSet(string hashid, string key, string value, DateTime?datetime = null) { try { using (var redis = RedisManager.GetClient()) { if (redis.SetEntryInHash(hashid, key, value)) { if (datetime != null) { redis.ExpireEntryAt(hashid, datetime.Value); } return(true); } return(false); //return redis.HSet(hashid, key.ToUtf8Bytes(), value.ToUtf8Bytes()); } } catch (Exception ex) { throw new Exception("HashSet() =>> " + ex.Message); } }