Esempio n. 1
0
        /// <summary>
        /// 删除元素
        /// </summary>
        /// <param name="key"></param>
        /// <param name="members"></param>
        /// <returns>返回的是从有序集合中删除的成员个数,不包括不存在的成员。</returns>
        public int ZREM(string key, params string[] members)
        {
            //ZREM key member [member ...]
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (members == null || members.Length == 0)
            {
                throw new ArgumentNullException("member");
            }

            using (var w = new RedisWriter(this.client, members.Length + 2, "ZREM"))
            {
                w.WriteArgument(key);
                foreach (var member in members)
                {
                    w.WriteArgument(member);
                }

                this.connection.SendCommand(w);
            }

            return(this.connection.ExpectInt());
        }
Esempio n. 2
0
        /// <summary>
        /// 设置一个对象
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public bool Set(string key, object value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            using (var redisWriter = new RedisWriter(this, 3, "SET"))
            {
                redisWriter.WriteArgument(key);

                if (value is string)
                {
                    redisWriter.WriteArgument((string)value);
                }
                else if (value is byte[])
                {
                    redisWriter.WriteArgument((byte[])value);
                }
                else
                {
                    redisWriter.WriteArgument(this.BinarySerializable.Serialize(value));
                }

                this.Connection.SendCommand(redisWriter);
            }

            return(this.Connection.ExpectSuccess());
        }
Esempio n. 3
0
        /// <summary>
        /// 获取一个项
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        public string HGET(string key, string field)
        {
            //HGET key field

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (field == null)
            {
                throw new ArgumentNullException("field");
            }

            using (var w = new RedisWriter(this.client, 3, "HGET"))
            {
                w.WriteArgument(key);
                w.WriteArgument(field);

                this.connection.SendCommand(w);
            }

            //            Integer reply, specifically:
            //1 if the hash contains field.
            //0 if the hash does not contain field, or key does not exist.
            return(this.connection.ExpectToString());
        }
Esempio n. 4
0
        /// <summary>
        /// 获取并设置一个新值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public string GetSet(string key, string value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (value.Length > 1073741824)
            {
                throw new ArgumentException("value exceeds 1G", "value");
            }

            using (var redisWriter = new RedisWriter(this, 3, "GETSET"))
            {
                redisWriter.WriteArgument(key);
                redisWriter.WriteArgument(value);
                this.Connection.SendCommand(redisWriter);
            }

            return(this.Connection.ExpectToString());
        }
Esempio n. 5
0
        int StoreSetCommands(string cmd, string destKey, params string[] keys)
        {
            if (String.IsNullOrEmpty(cmd))
            {
                throw new ArgumentNullException("cmd");
            }

            if (String.IsNullOrEmpty(destKey))
            {
                throw new ArgumentNullException("destKey");
            }

            if (keys == null)
            {
                throw new ArgumentNullException("keys");
            }

            using (var w = new RedisWriter(this.client, keys.Length + 2, cmd))
            {
                w.WriteArgument(destKey);
                foreach (var key in keys)
                {
                    w.WriteArgument(key);
                }

                this.connection.SendCommand(w);
            }
            return(this.connection.ExpectInt());
        }
Esempio n. 6
0
        /// <summary>
        /// 返回有序集 key 中,成员 member 的 score 值。如果 member 元素不是有序集 key 的成员,或 key 不存在,返回 nil 。
        /// </summary>
        /// <param name="key"></param>
        /// <param name="member"></param>
        /// <returns></returns>
        public double ZSCORE(string key, string member)
        {
            //ZSCORE key member
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (member == null || member.Length == 0)
            {
                throw new ArgumentNullException("member");
            }

            //If member exists in the sorted set, Integer reply: the rank of member.
            //If member does not exist in the sorted set or key does not exist, Bulk string reply: nil.
            using (var w = new RedisWriter(this.client, 3, "ZSCORE"))
            {
                w.WriteArgument(key);
                w.WriteArgument(member);

                this.connection.SendCommand(w);
            }

            return(this.connection._ExpectToDouble());
        }
Esempio n. 7
0
        /// <summary>
        /// 获取元素的键项列表
        /// </summary>
        /// <param name="key"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        public string[] HMGET(string key, params string[] fields)
        {
            //HMGET key field [field ...]

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (fields.Length == 0)
            {
                throw new ArgumentException("fields no data");
            }

            using (var w = new RedisWriter(this.client, fields.Length + 2, "HMGET"))
            {
                w.WriteArgument(key);

                foreach (var field in fields)
                {
                    w.WriteArgument(field);
                }

                this.connection.SendCommand(w);
            }

            //Array reply
            return(this.connection.ExpectToStringArray(this.connection.ExpectMultiBulkReply()));
        }
Esempio n. 8
0
        /// <summary>
        /// 获取元素排名
        /// </summary>
        /// <param name="key"></param>
        /// <param name="member"></param>
        /// <returns></returns>
        public int?ZRANK(string key, string member)
        {
            //ZRANK key member
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (member == null)
            {
                throw new ArgumentNullException("member");
            }

            //If member exists in the sorted set, Integer reply: the rank of member.
            //If member does not exist in the sorted set or key does not exist, Bulk string reply: nil.
            using (var w = new RedisWriter(this.client, 3, "ZRANK"))
            {
                w.WriteArgument(key);
                w.WriteArgument(member);

                this.connection.SendCommand(w);
            }

            return(this.connection.ExpectIntOrNil());
        }
Esempio n. 9
0
        /// <summary>
        /// 对某项自增一浮点数值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <param name="increment"></param>
        /// <returns>自增后的数值,服务端版本要求:Available since 2.6.0.</returns>
        public double HINCRBYFLOAT(string key, string field, double increment)
        {
            //HINCRBYFLOAT mykey field 0.1

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (field == null)
            {
                throw new ArgumentNullException("field");
            }

            using (var w = new RedisWriter(this.client, 4, "HINCRBYFLOAT"))
            {
                w.WriteArgument(key);
                w.WriteArgument(field);
                w.WriteArgument(increment.ToString());

                this.connection.SendCommand(w);
            }

            //Bulk string reply: the value of field after the increment.
            return(this.connection._ExpectToDouble());
        }
Esempio n. 10
0
        /// <summary>
        /// 删除一个项
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        public bool HDEL(string key, string field)
        {
            //HDEL myhash field1

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (field == null)
            {
                throw new ArgumentNullException("field");
            }

            using (var w = new RedisWriter(this.client, 3, "HDEL"))
            {
                w.WriteArgument(key);
                w.WriteArgument(field);

                this.connection.SendCommand(w);
            }

            //Integer reply: the number of fields that were removed from the hash, not including specified but non existing fields.
            return(this.connection.ExpectInt() == 1);
        }
Esempio n. 11
0
        /// <summary>
        /// 设置一个元素项,如果元素项不存在则成功,否则设置失败
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <returns>
        /// 1 if field is a new field in the hash and value was set.
        /// 0 if field already exists in the hash and the value was updated.
        /// </returns>
        public int HSETNX(string key, string field, string value)
        {
            //HSETNX key field value

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (field == null)
            {
                throw new ArgumentNullException("field");
            }

            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            using (var w = new RedisWriter(this.client, 4, "HSETNX"))
            {
                w.WriteArgument(key);
                w.WriteArgument(field);
                w.WriteArgument(value);

                this.connection.SendCommand(w);
            }

            //         Integer reply, specifically:
            //1 if field is a new field in the hash and value was set.
            //0 if field already exists in the hash and the value was updated.
            return(this.connection.ExpectInt());
        }
Esempio n. 12
0
        /// <summary>
        /// 对某项自增一数值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <param name="increment"></param>
        /// <returns>自增后的数值</returns>
        public int HINCRBY(string key, string field, int increment)
        {
            //HINCRBY key field increment

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (field == null)
            {
                throw new ArgumentNullException("field");
            }

            using (var w = new RedisWriter(this.client, 4, "HINCRBY"))
            {
                w.WriteArgument(key);
                w.WriteArgument(field);
                w.WriteArgument(increment.ToString());

                this.connection.SendCommand(w);
            }

            //Integer reply: the value at field after the increment operation.
            return(this.connection.ExpectInt());
        }
Esempio n. 13
0
        /// <summary>
        /// 设置一个值并指定过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expireSeconds"></param>
        /// <returns></returns>
        public bool SetEX(string key, byte[] value, int expireSeconds)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (value.Length > 1073741824)
            {
                throw new ArgumentException("value exceeds 1G", "value");
            }

            using (var redisWriter = new RedisWriter(this, 4, "SETEX"))
            {
                redisWriter.WriteArgument(key);
                redisWriter.WriteArgument(expireSeconds.ToString());
                redisWriter.WriteArgument(value);
                this.Connection.SendCommand(redisWriter);
            }

            return(this.Connection.ExpectSuccess());
        }
Esempio n. 14
0
        /// <summary>
        /// 随机返回指定数量元素,Redis 2.6
        /// </summary>
        /// <param name="key"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public string[] SRANDMEMBER(string key, int count)
        {
            using (var w = new RedisWriter(this.client, 3, "SRANDMEMBER"))
            {
                w.WriteArgument(key);
                w.WriteArgument(count.ToString());

                this.connection.SendCommand(w);
            }
            return(this.connection.ExpectToStringArray());
        }
Esempio n. 15
0
 /// <summary>
 /// 在列表左侧添加元素
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <returns>添加后元素数</returns>
 public int LPush(string key, string value)
 {
     //Integer reply: the length of the list after the push operations.
     using (var w = new RedisWriter(this.client, 3, "LPUSH"))
     {
         w.WriteArgument(key);
         w.WriteArgument(value);
         this.connection.SendCommand(w);
     }
     return(this.connection.ExpectInt());
 }
Esempio n. 16
0
        /// <summary>
        /// 移除元素后的一个列表范围
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="count">
        ///
        /// count &lt; 0: Remove elements equal to value moving from head to tail.
        /// count &gt; 0: Remove elements equal to value moving from tail to head.
        /// count = 0: Remove all elements equal to value.
        ///
        /// </param>
        /// <returns>Integer reply: the number of removed elements.</returns>
        public int LRem(string key, string value, int count)
        {
            using (var w = new RedisWriter(this.client, 4, "LREM"))
            {
                w.WriteArgument(key);
                w.WriteArgument(count.ToString());
                w.WriteArgument(value);

                this.connection.SendCommand(w);
            }
            return(this.connection.ExpectInt());
        }
Esempio n. 17
0
        /// <summary>
        /// 将元素从一个列表转到另一个列表
        /// 原子性地返回并移除存储在 source 的列表的最后一个元素(列表尾部元素), 并把该元素放入存储在 destination 的列表的第一个元素位置(列表头部)
        /// </summary>
        /// <param name="destinationKey"></param>
        /// <param name="sourceKey"></param>
        public string RPOPLPUSH(string sourceKey, string destinationKey)
        {
            using (var w = new RedisWriter(this.client, 3, "RPOPLPUSH"))
            {
                w.WriteArgument(sourceKey);
                w.WriteArgument(destinationKey);
                this.connection.SendCommand(w);
            }
            var data = this.connection.ExpectBulkReply();

            return(this.client.Encoding.GetString(data));
        }
Esempio n. 18
0
        /// <summary>
        /// 移除元素
        /// </summary>
        /// <param name="key"></param>
        /// <param name="member"></param>
        /// <returns></returns>
        public bool REM(string key, string member)
        {
            //Integer reply: the number of members that were removed from the set, not including non existing members.

            using (var w = new RedisWriter(this.client, 3, "SREM"))
            {
                w.WriteArgument(key);
                w.WriteArgument(member);

                this.connection.SendCommand(w);
            }
            return(this.connection.ExpectInt() == 1);
        }
Esempio n. 19
0
        /// <summary>
        /// 获取指定索引元素
        /// 如果index是负数则表示从右边开始计算的索引,最右边元素的索引是-1
        /// </summary>
        /// <param name="key"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public string LIndex(string key, int index)
        {
            //Bulk string reply: the requested element, or nil when index is out of range.
            using (var w = new RedisWriter(this.client, 3, "LINDEX"))
            {
                w.WriteArgument(key);
                w.WriteArgument(index.ToString());
                this.connection.SendCommand(w);
            }
            var data = this.connection.ExpectBulkReply();

            return(this.client.Encoding.GetString(data));
        }
Esempio n. 20
0
        /// <summary>
        /// 判断是否存在元素
        /// </summary>
        /// <param name="key"></param>
        /// <param name="member"></param>
        /// <returns></returns>
        public bool SISMEMBER(string key, string member)
        {
            //            Integer reply, specifically:
            //1 if the element is a member of the set.
            //0 if the element is not a member of the set, or if key does not exist.
            using (var w = new RedisWriter(this.client, 3, "SISMEMBER"))
            {
                w.WriteArgument(key);
                w.WriteArgument(member);

                this.connection.SendCommand(w);
            }
            return(this.connection.ExpectInt() == 1);
        }
Esempio n. 21
0
        /// <summary>
        /// 设置指定索引元素值
        /// 将索引为index的元素赋值为value
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="index"></param>
        /// <returns>Integer reply: the number of removed elements.</returns>
        public bool LSet(string key, int index, string value)
        {
            var v = value == null ? new byte[0] : this.client.Encoding.GetBytes(value);

            using (var w = new RedisWriter(this.client, 4, "LSET"))
            {
                w.WriteArgument(key);
                w.WriteArgument(index.ToString());
                w.WriteArgument(value);

                this.connection.SendCommand(w);
            }
            return(this.connection.ExpectSuccess());
        }
Esempio n. 22
0
        /// <summary>
        /// 获取列表中指定位置元素
        /// 当"start=0,end=-1"获取整个集合中所有元素
        /// </summary>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public string[] LRange(string key, int start, int end)
        {
            using (var w = new RedisWriter(this.client, 4, "LRANGE"))
            {
                w.WriteArgument(key);
                w.WriteArgument(start.ToString());
                w.WriteArgument(end.ToString());

                this.connection.SendCommand(w);
            }
            var data = this.connection.ExpectMultiBulkReply();

            return(this.connection.ExpectToStringArray(data));
        }
Esempio n. 23
0
        /// <summary>
        /// 设置键过期间隔
        /// </summary>
        /// <param name="key"></param>
        /// <param name="seconds"></param>
        /// <returns></returns>
        public bool Expire(string key, int seconds)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            using (var redisWriter = new RedisWriter(this, 3, "EXPIRE"))
            {
                redisWriter.WriteArgument(key);
                redisWriter.WriteArgument(seconds.ToString());
                this.Connection.SendCommand(redisWriter);
            }
            return(this.Connection.ExpectInt() == 1);
        }
Esempio n. 24
0
        /// <summary>
        /// 指定分数范围的元素个数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="max"></param>
        /// <param name="min"></param>
        /// <returns></returns>
        public int ZCOUNT(string key, double min, double max)
        {
            //ZCOUNT key min max

            //Integer reply: the number of elements in the specified score range.
            using (var w = new RedisWriter(this.client, 4, "ZCOUNT"))
            {
                w.WriteArgument(key);
                w.WriteArgument(min.ToString());
                w.WriteArgument(max.ToString());

                this.connection.SendCommand(w);
            }
            return(this.connection.ExpectInt());
        }
Esempio n. 25
0
        /// <summary>
        /// 设置一个二进制值
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="value">值对空格与双引号数据敏感,若值包含空格或引号,请使用base64进行编码后传输,订阅时进行解码</param>
        /// <returns>received client count</returns>
        public int Publish(string channel, string value)
        {
            if (string.IsNullOrEmpty(channel))
            {
                throw new ArgumentException("channel");
            }

            using (var redisWriter = new RedisWriter(this.client, 3, "PUBLISH"))
            {
                redisWriter.WriteArgument(channel);
                redisWriter.WriteArgument(value);
                this.connection.SendCommand(redisWriter);
            }
            return(this.connection.ExpectInt());
        }
Esempio n. 26
0
        /// <summary>
        /// 自减指定数值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public int Decrement(string key, int count)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            using (var redisWriter = new RedisWriter(this, 3, "DECRBY"))
            {
                redisWriter.WriteArgument(key);
                redisWriter.WriteArgument(count.ToString());
                this.Connection.SendCommand(redisWriter);
            }
            return(this.Connection.ExpectInt());
        }
Esempio n. 27
0
        /// <summary>
        /// 给指定元素添加自增值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="increment"></param>
        /// <param name="member"></param>
        /// <returns></returns>
        public double ZINCRBY(string key, double increment, string member)
        {
            //ZINCRBY key increment member

            //Bulk string reply: the new score of member (a double precision floating point number), represented as string.
            using (var w = new RedisWriter(this.client, 4, "ZINCRBY"))
            {
                w.WriteArgument(key);
                w.WriteArgument(increment.ToString());
                w.WriteArgument(member);

                this.connection.SendCommand(w);
            }
            return(this.connection._ExpectToDouble());
        }
Esempio n. 28
0
        /// <summary>
        /// 获取一个项
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Dictionary <string, string> HGETALL(string key)
        {
            //HGETALL myhash

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            using (var w = new RedisWriter(this.client, 2, "HGETALL"))
            {
                w.WriteArgument(key);

                this.connection.SendCommand(w);
            }

            //Array reply: list of fields and their values stored in the hash, or an empty list when key does not exist.
            var b = this.connection.ExpectMultiBulkReply();

            var dict = new Dictionary <string, string>(b.Length);

            for (int i = 0, l = b.Length; i < l; i += 2)
            {
                dict[this.client.Encoding.GetString(b[i])] = this.client.Encoding.GetString(b[i + 1]);
            }
            return(dict);
        }
Esempio n. 29
0
        /// <summary>
        /// 获取键类型
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public RedisKeyType TypeOf(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            using (var redisWriter = new RedisWriter(this, 2, "TYPE"))
            {
                redisWriter.WriteArgument(key);
                this.Connection.SendCommand(redisWriter);
            }

            switch (this.Connection.ExpectString())
            {
            case "none":
                return(RedisKeyType.None);

            case "string":
                return(RedisKeyType.String);

            case "set":
                return(RedisKeyType.Set);

            case "list":
                return(RedisKeyType.List);
            }

            throw new RedisResponseException("Invalid value");
        }
Esempio n. 30
0
        /// <summary>
        /// 移动元素至指定集合
        /// </summary>
        /// <param name="srcKey"></param>
        /// <param name="destKey"></param>
        /// <param name="member"></param>
        /// <returns></returns>
        public bool SMOVE(string srcKey, string destKey, string member)
        {
            //            Integer reply, specifically:
            //1 if the element is moved.
            //0 if the element is not a member of source and no operation was performed.

            using (var w = new RedisWriter(this.client, 4, "SMOVE"))
            {
                w.WriteArgument(srcKey);
                w.WriteArgument(destKey);
                w.WriteArgument(member);

                this.connection.SendCommand(w);
            }
            return(this.connection.ExpectInt() == 1);
        }