コード例 #1
0
        public async ValueTask ProcessResultAsync(CancellationToken token)
        {
            try
            {
                switch (_asyncReadCommand)
                {
                case null:
                    ProcessResultThrowIfSync();
                    break;

                case Func <CancellationToken, ValueTask> VoidReadCommandAsync:
                    await VoidReadCommandAsync(token).ConfigureAwait(false);

                    OnSuccessVoidCallback?.Invoke();
                    break;

                case Func <CancellationToken, ValueTask <int> > IntReadCommandAsync:
                    var i32 = await IntReadCommandAsync(token).ConfigureAwait(false);

                    OnSuccessIntCallback?.Invoke(i32);
                    OnSuccessLongCallback?.Invoke(i32);
                    OnSuccessBoolCallback?.Invoke(i32 == RedisNativeClient.Success);
                    OnSuccessVoidCallback?.Invoke();
                    break;

                case Func <CancellationToken, ValueTask <long> > LongReadCommandAsync:
                    var i64 = await LongReadCommandAsync(token).ConfigureAwait(false);

                    OnSuccessIntCallback?.Invoke((int)i64);
                    OnSuccessLongCallback?.Invoke(i64);
                    OnSuccessBoolCallback?.Invoke(i64 == RedisNativeClient.Success);
                    OnSuccessVoidCallback?.Invoke();
                    break;

                case Func <CancellationToken, ValueTask <double> > DoubleReadCommandAsync:
                    var f64 = await DoubleReadCommandAsync(token).ConfigureAwait(false);

                    OnSuccessDoubleCallback?.Invoke(f64);
                    break;

                case Func <CancellationToken, ValueTask <byte[]> > BytesReadCommandAsync:
                    var bytes = await BytesReadCommandAsync(token).ConfigureAwait(false);

                    if (bytes != null && bytes.Length == 0)
                    {
                        bytes = null;
                    }
                    OnSuccessBytesCallback?.Invoke(bytes);
                    OnSuccessStringCallback?.Invoke(bytes != null ? Encoding.UTF8.GetString(bytes) : null);
                    OnSuccessTypeCallback?.Invoke(bytes != null ? Encoding.UTF8.GetString(bytes) : null);
                    OnSuccessIntCallback?.Invoke(bytes != null ? int.Parse(Encoding.UTF8.GetString(bytes)) : 0);
                    OnSuccessBoolCallback?.Invoke(bytes != null && Encoding.UTF8.GetString(bytes) == "OK");
                    break;

                case Func <CancellationToken, ValueTask <string> > StringReadCommandAsync:
                    var s = await StringReadCommandAsync(token).ConfigureAwait(false);

                    OnSuccessStringCallback?.Invoke(s);
                    OnSuccessTypeCallback?.Invoke(s);
                    break;

                case Func <CancellationToken, ValueTask <byte[][]> > MultiBytesReadCommandAsync:
                    var multiBytes = await MultiBytesReadCommandAsync(token).ConfigureAwait(false);

                    OnSuccessMultiBytesCallback?.Invoke(multiBytes);
                    OnSuccessMultiStringCallback?.Invoke(multiBytes?.ToStringList());
                    OnSuccessMultiTypeCallback?.Invoke(multiBytes.ToStringList());
                    OnSuccessDictionaryStringCallback?.Invoke(multiBytes.ToStringDictionary());
                    break;

                case Func <CancellationToken, ValueTask <List <string> > > MultiStringReadCommandAsync:
                    var multiString = await MultiStringReadCommandAsync(token).ConfigureAwait(false);

                    OnSuccessMultiStringCallback?.Invoke(multiString);
                    break;

                case Func <CancellationToken, ValueTask <RedisData> > RedisDataReadCommandAsync:
                    var data = await RedisDataReadCommandAsync(token).ConfigureAwait(false);

                    OnSuccessRedisTextCallback?.Invoke(data.ToRedisText());
                    OnSuccessRedisDataCallback?.Invoke(data);
                    break;

                case Func <CancellationToken, ValueTask <bool> > BoolReadCommandAsync:
                    var b = await BoolReadCommandAsync(token).ConfigureAwait(false);

                    OnSuccessBoolCallback?.Invoke(b);
                    break;

                case Func <CancellationToken, ValueTask <Dictionary <string, string> > > DictionaryStringReadCommandAsync:
                    var dict = await DictionaryStringReadCommandAsync(token).ConfigureAwait(false);

                    OnSuccessDictionaryStringCallback?.Invoke(dict);
                    break;

                default:
                    ProcessResultThrowIfSync();
                    break;
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);

                if (OnErrorCallback != null)
                {
                    OnErrorCallback(ex);
                }
                else
                {
                    throw;
                }
            }
        }
コード例 #2
0
        public void ProcessResult()
        {
            try
            {
                if (VoidReadCommand != null)
                {
                    VoidReadCommand();
                    OnSuccessVoidCallback?.Invoke();
                }
                else if (IntReadCommand != null)
                {
                    var result = IntReadCommand();
                    OnSuccessIntCallback?.Invoke(result);
                    OnSuccessLongCallback?.Invoke(result);
                    OnSuccessBoolCallback?.Invoke(result == RedisNativeClient.Success);
                    OnSuccessVoidCallback?.Invoke();
                }
                else if (LongReadCommand != null)
                {
                    var result = LongReadCommand();
                    OnSuccessIntCallback?.Invoke((int)result);
                    OnSuccessLongCallback?.Invoke(result);
                    OnSuccessBoolCallback?.Invoke(result == RedisNativeClient.Success);
                    OnSuccessVoidCallback?.Invoke();
                }
                else if (DoubleReadCommand != null)
                {
                    var result = DoubleReadCommand();
                    OnSuccessDoubleCallback?.Invoke(result);
                }
                else if (BytesReadCommand != null)
                {
                    var result = BytesReadCommand();
                    if (result != null && result.Length == 0)
                    {
                        result = null;
                    }

                    OnSuccessBytesCallback?.Invoke(result);
                    OnSuccessStringCallback?.Invoke(result != null ? Encoding.UTF8.GetString(result) : null);
                    OnSuccessTypeCallback?.Invoke(result != null ? Encoding.UTF8.GetString(result) : null);
                    OnSuccessIntCallback?.Invoke(result != null ? int.Parse(Encoding.UTF8.GetString(result)) : 0);
                    OnSuccessBoolCallback?.Invoke(result != null && Encoding.UTF8.GetString(result) == "OK");
                }
                else if (StringReadCommand != null)
                {
                    var result = StringReadCommand();
                    OnSuccessStringCallback?.Invoke(result);
                    OnSuccessTypeCallback?.Invoke(result);
                }
                else if (MultiBytesReadCommand != null)
                {
                    var result = MultiBytesReadCommand();
                    OnSuccessMultiBytesCallback?.Invoke(result);
                    OnSuccessMultiStringCallback?.Invoke(result != null ? result.ToStringList() : null);
                    OnSuccessMultiTypeCallback?.Invoke(result.ToStringList());
                    OnSuccessDictionaryStringCallback?.Invoke(result.ToStringDictionary());
                }
                else if (MultiStringReadCommand != null)
                {
                    var result = MultiStringReadCommand();
                    OnSuccessMultiStringCallback?.Invoke(result);
                }
                else if (RedisDataReadCommand != null)
                {
                    var data = RedisDataReadCommand();
                    OnSuccessRedisTextCallback?.Invoke(data.ToRedisText());
                    OnSuccessRedisDataCallback?.Invoke(data);
                }
                else if (BoolReadCommand != null)
                {
                    var result = BoolReadCommand();
                    OnSuccessBoolCallback?.Invoke(result);
                }
                else if (DictionaryStringReadCommand != null)
                {
                    var result = DictionaryStringReadCommand();
                    OnSuccessDictionaryStringCallback?.Invoke(result);
                }
                else
                {
                    ProcessResultThrowIfAsync();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);

                if (OnErrorCallback != null)
                {
                    OnErrorCallback(ex);
                }
                else
                {
                    throw;
                }
            }
        }