public void Before(InterceptorBeforeEventArgs args) { switch (args.Command._command) { case "GET": if (_cscc.TryGetCacheValue(args.Command.GetKey(0), args.ValueType, out var getval)) { args.Value = getval; _iscached = true; } break; case "MGET": var mgetValType = args.ValueType.GetElementType(); var mgetKeys = args.Command._keyIndexes.Select((item, index) => args.Command.GetKey(index)).ToArray(); var mgetVals = mgetKeys.Select(a => _cscc.TryGetCacheValue(a, mgetValType, out var mgetval) ? new DictGetResult { Value = mgetval, Exists = true } : new DictGetResult { Value = null, Exists = false }) .Where(a => a.Exists).Select(a => a.Value).ToArray(); if (mgetVals.Length == mgetKeys.Length) { args.Value = args.ValueType.FromObject(mgetVals); _iscached = true; } break; } }
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); } } }
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); } } }