Exemple #1
0
        /// <summary>
        /// ExecuteBulkReplies
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="request"></param>
        /// <param name="valueFactory"></param>
        /// <param name="asyncState"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">valueFactory is null.</exception>
        private Task <T> ExecuteBulkReplies <T>(RedisRequest request, Func <byte[], T> valueFactory, object asyncState)
        {
            if (valueFactory == null)
            {
                throw new ArgumentNullException("valueFactory");
            }

            return(this.Execute <T>(request.ToPayload(), (source, response) =>
            {
                var mbReeply = response.Reply as BulkReplies;
                if (mbReeply != null)
                {
                    if (mbReeply.Payload == null || mbReeply.Payload.Length == 0)
                    {
                        source.TrySetResult(default(T));
                        return;
                    }

                    try { source.TrySetResult(valueFactory(mbReeply.Payload)); }
                    catch (Exception ex)
                    {
                        source.TrySetException(ex);
                    }
                    return;
                }

                if (response.Reply is ErrorReply)
                {
                    source.TrySetException((response.Reply as ErrorReply).Error());
                    return;
                }

                source.TrySetException(new RedisException("Failed to resolve the bulkReplies"));
            }, asyncState));
        }
Exemple #2
0
        /// <summary>
        /// ExecuteMultiBulkReplies
        /// </summary>
        /// <param name="request"></param>
        /// <param name="asyncState"></param>
        /// <returns></returns>
        private Task <byte[][]> ExecuteMultiBulkReplies(RedisRequest request, object asyncState)
        {
            return(this.Execute <byte[][]>(request.ToPayload(), (source, response) =>
            {
                var mbReeply = response.Reply as MultiBulkReplies;
                if (mbReeply != null)
                {
                    if (mbReeply.Replies == null)
                    {
                        source.TrySetResult(new byte[0][]);
                        return;
                    }

                    byte[][] arrBytes = new byte[mbReeply.Replies.Length][];
                    for (int i = 0, l = mbReeply.Replies.Length; i < l; i++)
                    {
                        var objBulk = mbReeply.Replies[i] as BulkReplies;
                        if (objBulk != null)
                        {
                            arrBytes[i] = objBulk.Payload;
                        }
                    }
                    source.TrySetResult(arrBytes);
                    return;
                }
                if (response.Reply is ErrorReply)
                {
                    source.TrySetException((response.Reply as ErrorReply).Error());
                    return;
                }
                source.TrySetException(new RedisException("Failed to resolve the multiBulkReplies."));
            }, asyncState));
        }
        /// <summary>
        /// ExecuteMultiBulkReplies
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="request"></param>
        /// <param name="valueFactory"></param>
        /// <param name="asyncState"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">valueFactory is null.</exception>
        private Task <T[]> ExecuteMultiBulkReplies <T>(RedisRequest request, Func <byte[], T> valueFactory, object asyncState)
        {
            if (valueFactory == null)
            {
                throw new ArgumentNullException("valueFactory");
            }

            return(this.Execute <T[]>(request.ToPayload(), (source, response) =>
            {
                var mbReeply = response.Reply as MultiBulkReplies;
                if (mbReeply != null)
                {
                    if (mbReeply.Replies == null)
                    {
                        source.TrySetResult(null);
                        return;
                    }

                    var arrResults = new T[mbReeply.Replies.Length];
                    for (int i = 0, l = mbReeply.Replies.Length; i < l; i++)
                    {
                        var objBulk = mbReeply.Replies[i] as BulkReplies;
                        if (objBulk == null)
                        {
                            continue;
                        }

                        if (objBulk.Payload == null)
                        {
                            arrResults[i] = default(T);
                            continue;
                        }

                        try { arrResults[i] = valueFactory(objBulk.Payload); }
                        catch (Exception ex)
                        {
                            source.TrySetException(ex);
                            return;
                        }
                    }
                    source.TrySetResult(arrResults);
                    return;
                }
                if (response.Reply is ErrorReply)
                {
                    source.TrySetException((response.Reply as ErrorReply).Error());
                    return;
                }
                source.TrySetException(new RedisException(
                                           "Failed to resolve the multiBulkReplies, expected is multiBulkReplies, but was " + response.Reply.GetType().Name));
            }, asyncState));
        }
Exemple #4
0
        /// <summary>
        /// Sets the specified fields to their respective values in the hash stored at key.
        /// This command overwrites any existing fields in the hash. If key does not exist, a new key holding a hash is created.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="values"></param>
        /// <param name="asyncState"></param>
        /// <returns></returns>
        Task IHashCommands.Set(string key, Dictionary <string, byte[]> values, object asyncState)
        {
            if (values == null || values.Count == 0)
            {
                throw new ArgumentNullException("values is null or empty.");
            }

            var request = new RedisRequest(2 + values.Count * 2).AddArgument("HMSET").AddArgument(key);

            foreach (var child in values)
            {
                request.AddArgument(child.Key).AddArgument(child.Value);
            }
            return(this.ExecuteStatusReply(request, asyncState));
        }
Exemple #5
0
        /// <summary>
        /// Returns the values associated with the specified fields in the hash stored at key.
        /// For every field that does not exist in the hash, a nil value is returned.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="fields"></param>
        /// <param name="asyncState"></param>
        /// <returns>list of values associated with the given fields, in the same order as they are requested.</returns>
        Task <byte[][]> IHashCommands.Get(string key, string[] fields, object asyncState)
        {
            if (fields == null || fields.Length == 0)
            {
                throw new ArgumentNullException("fields is null or empty.");
            }

            var request = new RedisRequest(fields.Length + 2).AddArgument("HMGET").AddArgument(key);

            for (int i = 0, l = fields.Length; i < l; i++)
            {
                request.AddArgument(fields[i]);
            }
            return(this.ExecuteMultiBulkReplies(request, asyncState));
        }
Exemple #6
0
        /// <summary>
        /// Removes the specified fields from the hash stored at key.
        /// Specified fields that do not exist within this hash are ignored.
        /// If key does not exist, it is treated as an empty hash and this command returns 0.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="fields"></param>
        /// <param name="asyncState"></param>
        /// <returns>
        /// the number of fields that were removed from the hash, not including specified but non existing fields.
        /// </returns>
        /// <exception cref="ArgumentNullException">fields is null or empty.</exception>
        Task <int> IHashCommands.Remove(string key, string[] fields, object asyncState)
        {
            if (fields == null || fields.Length == 0)
            {
                throw new ArgumentNullException("fields is null or empty.");
            }

            var request = new RedisRequest(fields.Length + 2).AddArgument("HDEL").AddArgument(key);

            for (int i = 0, l = fields.Length; i < l; i++)
            {
                request.AddArgument(fields[i]);
            }

            return(this.ExecuteIntegerReply(request, asyncState));
        }
Exemple #7
0
        /// <summary>
        /// Sets the given keys to their respective values.
        /// MSET replaces existing values with new values, just as regular SET. See MSETNX if you don't want to overwrite existing values.
        /// MSET is atomic, so all given keys are set at once.
        /// It is not possible for clients to see that some of the keys were updated while others are unchanged.
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="asyncState"></param>
        /// <returns>always OK since MSET can't fail.</returns>
        /// <exception cref="ArgumentNullException">dic is null or empty.</exception>
        Task IStringCommands.Set(Dictionary <string, byte[]> dic, object asyncState)
        {
            if (dic == null || dic.Count == 0)
            {
                throw new ArgumentNullException("dic", "dic is null or empty.");
            }

            var request = new RedisRequest(dic.Count + dic.Count + 1).AddArgument("MSET");

            foreach (var kv in dic)
            {
                request.AddArgument(kv.Key).AddArgument(kv.Value);
            }

            return(this.ExecuteStatusReply(request, asyncState));
        }
Exemple #8
0
 /// <summary>
 /// ExecuteBulkReplies
 /// </summary>
 /// <param name="request"></param>
 /// <param name="asyncState"></param>
 /// <returns></returns>
 private Task <byte[]> ExecuteBulkReplies(RedisRequest request, object asyncState)
 {
     return(this.Execute <byte[]>(request.ToPayload(), (source, response) =>
     {
         var mbReeply = response.Reply as BulkReplies;
         if (mbReeply != null)
         {
             source.TrySetResult(mbReeply.Payload);
             return;
         }
         if (response.Reply is ErrorReply)
         {
             source.TrySetException((response.Reply as ErrorReply).Error());
             return;
         }
         source.TrySetException(new RedisException("Failed to resolve the bulkReplies"));
     }, asyncState));
 }
Exemple #9
0
 /// <summary>
 /// ExecuteStatusReply
 /// </summary>
 /// <param name="request"></param>
 /// <param name="asyncState"></param>
 /// <returns></returns>
 private Task <string> ExecuteStatusReply(RedisRequest request, object asyncState)
 {
     return(this.Execute <string>(request.ToPayload(), (source, response) =>
     {
         var statusReply = response.Reply as StatusReply;
         if (statusReply != null)
         {
             source.TrySetResult(statusReply.Status);
             return;
         }
         if (response.Reply is ErrorReply)
         {
             source.TrySetException((response.Reply as ErrorReply).Error());
             return;
         }
         source.TrySetException(new RedisException("Failed to resolve the statusReply"));
     }, asyncState));
 }
Exemple #10
0
 /// <summary>
 /// ExecuteIntegerReply2
 /// </summary>
 /// <param name="request"></param>
 /// <param name="asyncState"></param>
 /// <returns></returns>
 private Task <bool> ExecuteIntegerReply2(RedisRequest request, object asyncState)
 {
     return(this.Execute <bool>(request.ToPayload(), (source, response) =>
     {
         var intReply = response.Reply as IntegerReply;
         if (intReply != null)
         {
             source.TrySetResult(intReply.Value == 1);
             return;
         }
         if (response.Reply is ErrorReply)
         {
             source.TrySetException((response.Reply as ErrorReply).Error());
             return;
         }
         source.TrySetException(new RedisException("Failed to resolve the integerReply"));
     }, asyncState));
 }
Exemple #11
0
 /// <summary>
 /// ExecuteIntegerReply
 /// </summary>
 /// <param name="request"></param>
 /// <param name="asyncState"></param>
 /// <returns></returns>
 private Task <int> ExecuteIntegerReply(RedisRequest request, object asyncState)
 {
     return(this.Execute <int>(request.ToPayload(), (source, response) =>
     {
         var intReply = response.Reply as IntegerReply;
         if (intReply != null)
         {
             source.TrySetResult(intReply.Value);
             return;
         }
         if (response.Reply is ErrorReply)
         {
             source.TrySetException((response.Reply as ErrorReply).Error());
             return;
         }
         source.TrySetException(new RedisException(
                                    "Failed to resolve the integerReply, expected is integerReply, but was " + response.Reply.GetType().Name));
     }, asyncState));
 }
        /// <summary>
        /// unsubscribe pattern
        /// </summary>
        /// <param name="patterns"></param>
        private void UnPatternSubscribeInternal(string[] patterns)
        {
            if (patterns == null || patterns.Length == 0)
            {
                return;
            }

            var connection = this._currentConnection;

            if (connection == null)
            {
                return;
            }

            var r = new RedisRequest(patterns.Length + 1).AddArgument("PUNSUBSCRIBE");

            foreach (var pattern in patterns)
            {
                r.AddArgument(pattern);
            }

            connection.BeginSend(new Packet(r.ToPayload()));
        }
        /// <summary>
        /// unsubscribe channel
        /// </summary>
        /// <param name="channels"></param>
        private void UnSubscribeInternal(string[] channels)
        {
            if (channels == null || channels.Length == 0)
            {
                return;
            }

            var connection = this._currentConnection;

            if (connection == null)
            {
                return;
            }

            var r = new RedisRequest(channels.Length + 1).AddArgument("UNSUBSCRIBE");

            foreach (var channel in channels)
            {
                r.AddArgument(channel);
            }

            connection.BeginSend(new Packet(r.ToPayload()));
        }
        /// <summary>
        /// unsubscribe pattern
        /// </summary>
        /// <param name="patterns"></param>
        private void UnPatternSubscribeInternal(string[] patterns)
        {
            if (patterns == null || patterns.Length == 0) return;

            var connection = this._currentConnection;
            if (connection == null) return;

            var r = new RedisRequest(patterns.Length + 1).AddArgument("PUNSUBSCRIBE");
            foreach (var pattern in patterns) r.AddArgument(pattern);

            connection.BeginSend(new Packet(r.ToPayload()));
        }
        /// <summary>
        /// unsubscribe channel
        /// </summary>
        /// <param name="channels"></param>
        private void UnSubscribeInternal(string[] channels)
        {
            if (channels == null || channels.Length == 0) return;

            var connection = this._currentConnection;
            if (connection == null) return;

            var r = new RedisRequest(channels.Length + 1).AddArgument("UNSUBSCRIBE");
            foreach (var channel in channels) r.AddArgument(channel);

            connection.BeginSend(new Packet(r.ToPayload()));
        }