/// <summary> /// 返回 key 所关联的字符串值 /// </summary> /// <param name="key"></param> /// <returns></returns> public string Get(string key) { return(RedisConnection.DoWithKey(RequestType.GET, key).Data); }
/// <summary> /// 将一个或多个 member 元素及其 score 值加入到有序集 key 当中 /// </summary> /// <param name="timeSpan"></param> /// <param name="key"></param> /// <param name="scoreVals"></param> public async void ZAddAsync(TimeSpan timeSpan, string key, Dictionary <double, string> scoreVals) { await RedisConnection.DoBatchZaddWithIDDicAsync(RequestType.ZADD, key, scoreVals, timeSpan); }
internal RedisDataBase(RedisConnection cnn) { _cnn = cnn; _redisLock = new RedisLock(_cnn); }
/// <summary> /// 返回有序集 key 中,指定区间内的成员。 /// 其中成员的位置按 score 值递增(从大到小)来排序。 /// </summary> /// <param name="timeSpan"></param> /// <param name="key"></param> /// <param name="start"></param> /// <param name="stop"></param> /// <returns></returns> public async Task <List <ZItem> > ZRevrangeAsync(TimeSpan timeSpan, string key, double start = 0, double stop = -1) { var data = await RedisConnection.DoRangAsync(RequestType.ZREVRANGE, key, start, stop, timeSpan); return(data.ToZList()); }
/// <summary> /// 返回指定成员区间内的成员, /// 此指令适用于分数相同的有序集合中, /// LEX结尾的指令是要求分数必须相同 /// </summary> /// <param name="timeSpan"></param> /// <param name="key"></param> /// <param name="min"></param> /// <param name="max"></param> /// <param name="offset"></param> /// <param name="count"></param> /// <returns></returns> public async Task <List <string> > ZrangeByLexAsync(TimeSpan timeSpan, string key, double min, double max, long offset = -1, int count = 20) { var data = await RedisConnection.DoRangByScoreAsync(timeSpan, RequestType.ZRANGEBYLEX, key, min, max, RangType.None, offset, count); return(data.ToList()); }
/// <summary> /// 返回哈希表中给定域的值 /// </summary> /// <param name="hid"></param> /// <param name="key"></param> /// <returns></returns> public string HGet(string hid, string key) { return(RedisConnection.DoWithKeyValue(RequestType.HGET, hid, key).Data); }
/// <summary> /// 返回哈希表 key 中,所有的域和值 /// </summary> /// <param name="hid"></param> /// <returns></returns> public Dictionary <string, string> HGetAll(string hid) { return(RedisConnection.DoWithKey(RequestType.HGETALL, hid).ToKeyValues()); }
/// <summary> /// 删除给定的一个或多个 key /// </summary> /// <param name="key"></param> public void Del(string key) { RedisConnection.DoWithKey(RequestType.DEL, key); }
/// <summary> /// 删除给定的一个或多个 key /// </summary> /// <param name="keys"></param> public void Del(params string[] keys) { RedisConnection.DoWithMutiParams(RequestType.DEL, keys); }
/// <summary> /// 以毫秒为单位,返回给定 key 的剩余生存时间(TTL, time to live) /// </summary> /// <param name="key"></param> /// <returns>当 key 不存在时,返回 -2 。当 key 存在但没有设置剩余生存时间时,返回 -1 。否则,以秒为单位,返回 key 的剩余生存时间。</returns> public long Pttl(string key) { long.TryParse(RedisConnection.DoWithKey(RequestType.PTTL, key).Data, out long result); return(result); }
/// <summary> /// 从当前数据库中随机返回(不删除)一个 key /// </summary> /// <returns></returns> public string RandomKey() { return(RedisConnection.Do(RequestType.RANDOMKEY).Data); }
/// <summary> /// 以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live) /// </summary> /// <param name="key"></param> /// <returns>当 key 不存在时,返回 -2 。当 key 存在但没有设置剩余生存时间时,返回 -1 。否则,以秒为单位,返回 key 的剩余生存时间。 </returns> public int Ttl(string key) { int.TryParse(RedisConnection.DoWithKey(RequestType.TTL, key).Data, out int result); return(result); }
/// <summary> /// 移除给定 key 的生存时间,将这个 key 从『易失的』(带生存时间 key )转换成『持久的』(一个不带生存时间、永不过期的 key )。 /// </summary> /// <param name="key"></param> public void Persist(string key) { RedisConnection.DoWithKey(RequestType.PERSIST, key); }
/// <summary> /// 返回 key 所关联的字符串值 /// </summary> /// <param name="keys"></param> /// <returns></returns> public List <string> MGet(params string[] keys) { return(RedisConnection.DoWithMutiParams(RequestType.MGET, keys).ToList()); }
/// <summary> /// 将哈希表 hash 中域 field 的值设置为 value /// </summary> /// <param name="hid"></param> /// <param name="key"></param> /// <param name="value"></param> public void HSet(string hid, string key, string value) { RedisConnection.DoWithID(RequestType.HSET, hid, key, value); }
/// <summary> /// 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除。 /// </summary> /// <param name="key"></param> /// <param name="seconds"></param> public void Expire(string key, int seconds) { RedisConnection.DoExpire(key, seconds); }
/// <summary> /// 同时将多个 field-value (域-值)对设置到哈希表 key 中 /// </summary> /// <param name="hid"></param> /// <param name="keyvalues"></param> public void HMSet(string hid, Dictionary <string, string> keyvalues) { RedisConnection.DoBatchWithIDDic(RequestType.HMSET, hid, keyvalues); }
/// <summary> /// 为给定 key 设置生存时间 /// </summary> /// <param name="key"></param> /// <param name="timestamp"></param> public void ExpireAt(string key, int timestamp) { RedisConnection.DoExpireAt(key, timestamp); }
/// <summary> /// 返回哈希表 key 中,一个或多个给定域的值 /// </summary> /// <param name="hid"></param> /// <param name="keys"></param> /// <returns></returns> public List <string> HMGet(string hid, List <string> keys) { return(RedisConnection.DoBatchWithList(RequestType.HMGET, hid, keys).ToList()); }
/// <summary> /// 查找所有符合给定模式 pattern 的 key /// </summary> /// <param name="pattern"></param> /// <returns></returns> public List <string> Keys(string pattern = "*") { return(RedisConnection.DoWithKey(RequestType.KEYS, pattern).ToList()); }
/// <summary> /// 返回哈希表 key 中所有域的值 /// </summary> /// <param name="hid"></param> /// <returns></returns> public List <string> HGetValues(string hid) { return(RedisConnection.DoWithKey(RequestType.HVALS, hid).ToList()); }
public RedisOperator(RedisConnection cnn) { _cnn = cnn; _redisCoder = _cnn.RedisCoder; }
/// <summary> /// 返回有序集 key 中, score 值介于 max 和 min 之间(默认包括等于 max 或 min )的所有的成员。有序集成员按 score 值递减(从大到小)的次序排列。 /// </summary> /// <param name="timeSpan"></param> /// <param name="key"></param> /// <param name="min"></param> /// <param name="max"></param> /// <param name="rangType"></param> /// <param name="offset"></param> /// <param name="count"></param> /// <returns></returns> public async Task <List <ZItem> > ZRevRangeByScoreAsync(TimeSpan timeSpan, string key, double min = double.MinValue, double max = double.MaxValue, RangType rangType = RangType.None, long offset = -1, int count = 20) { var data = await RedisConnection.DoRangByScoreAsync(timeSpan, RequestType.ZREVRANGEBYSCORE, key, min, max, rangType, offset, count, true); return(data.ToZList()); }
/// <summary> /// 检查给定域 field 是否存在于哈希表 hash 当中 /// </summary> /// <param name="hid"></param> /// <param name="key"></param> /// <returns></returns> public bool HExists(string hid, string key) { var result = RedisConnection.DoWithKeyValue(RequestType.HEXISTS, hid, key).Data; return(result == "1" ? true : false); }
/// <summary> /// 将一个或多个 member 元素及其 score 值加入到有序集 key 当中 /// </summary> /// <param name="timeSpan"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="score"></param> public async void ZAddAsync(TimeSpan timeSpan, string key, string value, double score) { await RedisConnection.DoWithIDAsync(RequestType.ZADD, key, score.ToString(), value, timeSpan); }
/// <summary> /// 为哈希表 key 中的域 field 的值加上增量 increment /// </summary> /// <param name="hid"></param> /// <param name="key"></param> /// <param name="num"></param> /// <returns></returns> public long HIncrementBy(string hid, string key, int num) { return(long.Parse(RedisConnection.DoWithID(RequestType.HINCRBY, hid, key, num.ToString()).Data)); }
/// <summary> /// 设置RedisConnection /// </summary> /// <param name="ipPort"></param> /// <param name="cnn"></param> public static void Set(string ipPort, RedisConnection cnn) { _redisConnections.TryAdd(ipPort, cnn); }
/// <summary> /// 为哈希表 key 中的域 field 加上浮点数增量 increment /// </summary> /// <param name="hid"></param> /// <param name="key"></param> /// <param name="num"></param> /// <returns></returns> public float HIncrementByFloat(string hid, string key, float num) { return(float.Parse(RedisConnection.DoWithID(RequestType.HINCRBYFLOAT, hid, key, num.ToString()).Data)); }
public RedisLock(RedisConnection cnn) { _cnn = cnn; }
/// <summary> /// 如果键 key 已经存在并且它的值是一个字符串, APPEND 命令将把 value 追加到键 key 现有值的末尾 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public int Append(string key, string value) { return(int.Parse(RedisConnection.DoWithKeyValue(RequestType.APPEND, key, value).Data)); }