Esempio n. 1
0
 internal bool SelectDB(RedisConnectionSettings settings, int dbIndex)
 {
     if (dbIndex >= RedisConstants.MinDbIndex && dbIndex <= RedisConstants.MaxDbIndex)
     {
         var result = false;
         try
         {
             if (this.IsConnected())
             {
                 using (var cmd = new RedisCommand(dbIndex, RedisCommandList.Select, RedisCommandType.SendAndReceive, dbIndex.ToBytes()))
                 {
                     result = cmd.ExpectOK(new RedisSocketContext(this, settings), false);
                     if (result)
                     {
                         SetDb(dbIndex);
                     }
                 }
             }
         }
         catch (Exception)
         { }
         return(result);
     }
     return(true);
 }
Esempio n. 2
0
 protected bool Auth(RedisSocket socket, string password)
 {
     if (!password.IsEmpty())
     {
         ValidateNotDisposed();
         using (var cmd = new RedisCommand(-1, RedisCommandList.Auth, RedisCommandType.SendAndReceive, password.ToBytes()))
         {
             return(cmd.ExpectOK(new RedisSocketContext(socket, Settings), true));
         }
     }
     return(true);
 }
Esempio n. 3
0
        protected bool SetClientName(RedisSocket socket, string clientName)
        {
            if (clientName == null)
            {
                throw new ArgumentNullException("clientName");
            }

            ValidateNotDisposed();
            using (var cmd = new RedisCommand(-1, RedisCommandList.Client, RedisCommandType.SendAndReceive, RedisCommandList.SetName, clientName.ToBytes()))
            {
                return(cmd.ExpectOK(new RedisSocketContext(socket, Settings), true));
            }
        }
Esempio n. 4
0
        protected override void OnFlush(IList <RedisRequest> requests, RedisSocketContext context, out bool success)
        {
            var queue = Interlocked.Exchange(ref m_WatchQ, null);

            if (queue != null && queue.Count > 0)
            {
                var watchCommand = new RedisCommand(DbIndex, RedisCommandList.Watch,
                                                    RedisCommandType.SendAndReceive, queue.ToArray().ToBytesArray());
                var watchResult = watchCommand.ExpectOK(context);

                if (!watchResult)
                {
                    success = false;
                    return;
                }
            }

            var multiCommand = new RedisCommand(DbIndex, RedisCommandList.Multi);
            var multiResult  = multiCommand.ExpectOK(context);

            success = multiResult;
            if (!success)
            {
                Cancel(requests);
                return;
            }

            success = Process(requests, context);
            if (!success)
            {
                Discard(requests, context);
                return;
            }

            success = Exec(requests, context);
        }
Esempio n. 5
0
        protected internal override T Expect <T>(RedisCommand command, RedisCommandExpect expectation, string okIf = null)
        {
            using (var connection = Connect())
            {
                switch (expectation)
                {
                case RedisCommandExpect.Response:
                    return((T)(object)command.Execute(connection, ThrowOnError));

                case RedisCommandExpect.Array:
                    return((T)(object)command.ExpectArray(connection, ThrowOnError));

                case RedisCommandExpect.BulkString:
                    return((T)(object)command.ExpectBulkString(connection, ThrowOnError));

                case RedisCommandExpect.BulkStringBytes:
                    return((T)(object)command.ExpectBulkStringBytes(connection, ThrowOnError));

                case RedisCommandExpect.Double:
                    return((T)(object)command.ExpectDouble(connection, ThrowOnError));

                case RedisCommandExpect.GreaterThanZero:
                    return((T)(object)command.ExpectInteger(connection, ThrowOnError));

                case RedisCommandExpect.Integer:
                    return((T)(object)command.ExpectInteger(connection, ThrowOnError));

                case RedisCommandExpect.MultiDataBytes:
                    return((T)(object)command.ExpectMultiDataBytes(connection, ThrowOnError));

                case RedisCommandExpect.MultiDataStrings:
                    return((T)(object)command.ExpectMultiDataStrings(connection, ThrowOnError));

                case RedisCommandExpect.Nothing:
                    return((T)(object)command.ExpectNothing(connection, ThrowOnError));

                case RedisCommandExpect.NullableDouble:
                    return((T)(object)command.ExpectNullableDouble(connection, ThrowOnError));

                case RedisCommandExpect.NullableInteger:
                    return((T)(object)command.ExpectNullableInteger(connection, ThrowOnError));

                case RedisCommandExpect.OK:
                    return((T)(object)command.ExpectOK(connection, ThrowOnError));

                case RedisCommandExpect.One:
                    return((T)(object)command.ExpectOne(connection, ThrowOnError));

                case RedisCommandExpect.SimpleString:
                    if (!okIf.IsEmpty())
                    {
                        return((T)(object)command.ExpectSimpleString(connection, okIf, ThrowOnError));
                    }
                    return((T)(object)command.ExpectSimpleString(connection, ThrowOnError));

                case RedisCommandExpect.SimpleStringBytes:
                    if (!okIf.IsEmpty())
                    {
                        return((T)(object)command.ExpectSimpleStringBytes(connection, okIf.ToBytes(), ThrowOnError));
                    }
                    return((T)(object)command.ExpectSimpleStringBytes(connection, ThrowOnError));

                default:
                    throw new RedisException(String.Format("Undefined expectation type, {0}", expectation.ToString("F")), RedisErrorCode.NotSupported);
                }
            }
        }