예제 #1
0
 public void After(InterceptorAfterEventArgs args)
 {
     switch (args.Command._command)
     {
     case "PING": return;
     }
     if (_iscached == false && args.Exception == null && args.Command._command == "GET")
     {
         _dicStrings.TryAdd(args.Command._flagKey[0], args.Value);
     }
 }
예제 #2
0
 public void After(InterceptorAfterEventArgs args)
 {
     switch (args.Command._command)
     {
     case "GET":
         if (_iscached == false && args.Exception == null)
         {
             _dicStrings.TryAdd(args.Command.GetKey(0), args.Value);
         }
         break;
     }
 }
예제 #3
0
        internal T LogCall <T>(CommandPacket cmd, Func <T> func)
        {
            cmd.Prefix(Prefix);
            var isnotice = this.Notice != null;

            if (isnotice == false && this.Interceptors.Any() == false)
            {
                return(func());
            }
            Exception exception = null;

            T   ret      = default(T);
            var isaopval = false;

            IInterceptor[] aops   = new IInterceptor[this.Interceptors.Count + (isnotice ? 1 : 0)];
            Stopwatch[]    aopsws = new Stopwatch[aops.Length];
            for (var idx = 0; idx < aops.Length; idx++)
            {
                aopsws[idx] = new Stopwatch();
                aopsws[idx].Start();
                aops[idx] = isnotice && idx == aops.Length - 1 ? new NoticeCallInterceptor(this) : this.Interceptors[idx]?.Invoke();
                var args = new InterceptorBeforeEventArgs(this, cmd);
                aops[idx].Before(args);
                if (args.ValueIsChanged && args.Value is T argsValue)
                {
                    isaopval = true;
                    ret      = argsValue;
                }
            }
            try
            {
                if (isaopval == false)
                {
                    ret = func();
                }
                return(ret);
            }
            catch (Exception ex)
            {
                exception = ex;
                throw ex;
            }
            finally
            {
                for (var idx = 0; idx < aops.Length; idx++)
                {
                    aopsws[idx].Stop();
                    var args = new InterceptorAfterEventArgs(this, cmd, ret, exception, aopsws[idx].ElapsedMilliseconds);
                    aops[idx].After(args);
                }
            }
        }
예제 #4
0
                public void After(InterceptorAfterEventArgs args)
                {
                    switch (args.Command._command)
                    {
                    case "GET":
                        if (_iscached == false && args.Exception == null)
                        {
                            var getkey = args.Command.GetKey(0);
                            if (_cscc._options.KeyFilter?.Invoke(getkey) != false)
                            {
                                _cscc.SetCacheValue(args.Command._command, getkey, args.ValueType, args.Value);
                            }
                        }
                        break;

                    case "MGET":
                        if (_iscached == false && args.Exception == null)
                        {
                            if (args.Value is Array valueArr)
                            {
                                var valueArrElementType = args.ValueType.GetElementType();
                                var sourceArrLen        = valueArr.Length;
                                for (var a = 0; a < sourceArrLen; a++)
                                {
                                    var getkey = args.Command.GetKey(a);
                                    if (_cscc._options.KeyFilter?.Invoke(getkey) != false)
                                    {
                                        _cscc.SetCacheValue("GET", getkey, valueArrElementType, valueArr.GetValue(a));
                                    }
                                }
                            }
                        }
                        break;

                    default:
                        if (args.Command._keyIndexes.Any())
                        {
                            var cmdset = CommandSets.Get(args.Command._command);
                            if (cmdset != null &&
                                (cmdset.Flag & CommandSets.ServerFlag.write) == CommandSets.ServerFlag.write &&
                                (cmdset.Tag & CommandSets.ServerTag.write) == CommandSets.ServerTag.write &&
                                (cmdset.Tag & CommandSets.ServerTag.@string) == CommandSets.ServerTag.@string)
                            {
                                _cscc.RemoveCache(args.Command._keyIndexes.Select((item, index) => args.Command.GetKey(index)).ToArray());
                            }
                        }
                        break;
                    }
                }
예제 #5
0
            public void After(InterceptorAfterEventArgs args)
            {
                switch (args.Command._command)
                {
                case "GET":
                    if (_iscached == false && args.Exception == null)
                    {
                        _dicStrings.TryAdd(args.Command.GetKey(0), args.Value);
                    }
                    break;

                case "SUBSCRIBLE":
                    if (args.Command._input.Where((a, b) => b > 0 && string.Compare(a as string, "__redis__:invalidate", true) == 0).Any())
                    {
                        _context._clientid = _context._cli.ClientId();
                    }
                    break;
                }
            }
예제 #6
0
        async internal Task <T> LogCallAsync <T>(CommandPacket cmd, Func <Task <T> > func)
        {
            cmd.Prefix(Prefix);
            var isnotice = this.Notice != null;
            var isaop    = this.Interceptors.Any();

            if (isnotice == false && isaop == false)
            {
                return(await func());
            }
            Exception exception = null;
            Stopwatch sw        = default;

            if (isnotice)
            {
                sw = new Stopwatch();
                sw.Start();
            }

            T   ret      = default(T);
            var isaopval = false;

            IInterceptor[] aops   = null;
            Stopwatch[]    aopsws = null;
            if (isaop)
            {
                aops   = new IInterceptor[this.Interceptors.Count];
                aopsws = new Stopwatch[aops.Length];
                for (var idx = 0; idx < aops.Length; idx++)
                {
                    aopsws[idx] = new Stopwatch();
                    aopsws[idx].Start();
                    aops[idx] = this.Interceptors[idx]?.Invoke();
                    var args = new InterceptorBeforeEventArgs(this, cmd);
                    aops[idx].Before(args);
                    if (args.ValueIsChanged && args.Value is T argsValue)
                    {
                        isaopval = true;
                        ret      = argsValue;
                    }
                }
            }
            try
            {
                if (isaopval == false)
                {
                    ret = await func();
                }
                return(ret);
            }
            catch (Exception ex)
            {
                exception = ex;
                throw ex;
            }
            finally
            {
                if (isaop)
                {
                    for (var idx = 0; idx < aops.Length; idx++)
                    {
                        aopsws[idx].Stop();
                        var args = new InterceptorAfterEventArgs(this, cmd, ret, exception, aopsws[idx].ElapsedMilliseconds);
                        aops[idx].After(args);
                    }
                }

                if (isnotice)
                {
                    sw.Stop();
                    LogCallFinally(cmd, ret, sw, exception);
                }
            }
        }