Example #1
0
        /// <summary>
        /// Save the return data from invocation to cache
        /// </summary>
        /// <param name="methodExecutedContext"></param>
        public override void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
        {
            if (Duration > 0 && !methodExecutedContext.TryGet <bool>(Global.__flatwhite_outputcache_restored))
            {
                var key = methodExecutedContext.TryGet <string>(Global.__flatwhite_outputcache_key);
                if (string.IsNullOrWhiteSpace(key))
                {
                    return;
                }

                var cacheStore = methodExecutedContext.TryGet <ICacheStore>(Global.__flatwhite_outputcache_store);
                var strategy   = methodExecutedContext.TryGet <ICacheStrategy>(Global.__flatwhite_outputcache_strategy);
                var cacheItem  = new CacheItem
                {
                    Key     = key,
                    Data    = methodExecutedContext.Result,
                    StoreId = cacheStore.StoreId,
                    StaleWhileRevalidate = StaleWhileRevalidate,
                    MaxAge      = Duration,
                    CreatedTime = DateTime.UtcNow
                };
                CreatePhoenix(methodExecutedContext.Invocation, cacheItem);

                var changeMonitors = strategy.GetChangeMonitors(methodExecutedContext.Invocation, methodExecutedContext.InvocationContext);
                foreach (var mon in changeMonitors)
                {
                    mon.CacheMonitorChanged += state =>
                    {
                        RefreshCache(key);
                    };
                }

                cacheStore.Set(key, cacheItem, DateTime.UtcNow.AddSeconds(Duration + StaleWhileRevalidate));
            }
        }
        /// <summary>
        /// Initializes the exception context with current <see cref="MethodExecutedContext" />
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="executedContext"></param>
        public MethodExceptionContext(Exception exception, MethodExecutedContext executedContext)
        {
            Invocation        = executedContext.Invocation;
            MethodInfo        = executedContext.MethodInfo;
            InvocationContext = executedContext.InvocationContext;
            Exception         = exception;

            _result       = executedContext.Result;
            _updateResult = r => executedContext.Result = r;
        }
Example #3
0
        public virtual Task OnMethodExecutedAsync(MethodExecutedContext methodExecutedContext)
        {
            try
            {
                OnMethodExecuted(methodExecutedContext);
            }
            catch (Exception ex)
            {
                return(TaskHelpers.FromError(ex));
            }

            return(TaskHelpers.DefaultCompleted);
        }
Example #4
0
        private void HandleSync(List <MethodFilterAttribute> filterAttributes, MethodExecutingContext methodExecutingContext)
        {
            Exception exception = null;

            foreach (var f in filterAttributes)
            {
                try
                {
                    if (methodExecutingContext.Result == null)
                    {
                        f.OnMethodExecuting(methodExecutingContext);
                    }
                }
                catch (Exception ex)
                {
                    exception = ex;
                    break;
                }
            }

            if (exception != null)
            {
                HandleException(new MethodExceptionContext(exception, methodExecutingContext));
                exception = null;
            }

            filterAttributes.Reverse();
            var methodExecutedContext = new MethodExecutedContext(methodExecutingContext);

            foreach (var f in filterAttributes)
            {
                try
                {
                    f.OnMethodExecuted(methodExecutedContext);
                }
                catch (Exception ex)
                {
                    exception = ex;
                    break;
                }
            }

            if (exception != null)
            {
                HandleException(new MethodExceptionContext(exception, methodExecutedContext));
            }
            if (methodExecutedContext.Result != null && typeof(void) != methodExecutedContext.Invocation.Method.ReturnType)
            {
                methodExecutedContext.Invocation.ReturnValue = methodExecutedContext.Result;
            }
        }
Example #5
0
        /// <summary>
        /// This will be called via Reflection
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="filterAttributes"></param>
        /// <param name="methodExecutingContext"></param>
        /// <param name="taskResultType"></param>
        /// <returns></returns>
        private async Task <TResult> HandleAsyncWithType <TResult>(List <MethodFilterAttribute> filterAttributes, MethodExecutingContext methodExecutingContext, Type taskResultType)
        {
            Exception exception = null;

            foreach (var f in filterAttributes)
            {
                try
                {
                    if (methodExecutingContext.Result == null)
                    {
                        await f.OnMethodExecutingAsync(methodExecutingContext).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    exception = ex;
                    break;
                }
            }

            if (exception != null)
            {
                await HandleExceptionAsync(new MethodExceptionContext(exception, methodExecutingContext));

                exception = null;
            }

            filterAttributes.Reverse();
            var methodExecutedContext = new MethodExecutedContext(methodExecutingContext);

            foreach (var f in filterAttributes)
            {
                try
                {
                    await f.OnMethodExecutedAsync(methodExecutedContext).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    exception = ex;
                    break;
                }
            }

            if (exception != null)
            {
                await HandleExceptionAsync(new MethodExceptionContext(exception, methodExecutedContext));
            }

            return(methodExecutedContext.Result != null ? (TResult)methodExecutedContext.Result : default(TResult));
        }
Example #6
0
        private async Task HandleAsync(List <MethodFilterAttribute> filterAttributes, MethodExecutingContext methodExecutingContext)
        {
            Exception exception = null;

            foreach (var f in filterAttributes)
            {
                try
                {
                    await f.OnMethodExecutingAsync(methodExecutingContext).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    exception = ex;
                    break;
                }
            }

            if (exception != null)
            {
                await HandleExceptionAsync(new MethodExceptionContext(exception, methodExecutingContext));

                exception = null;
            }

            filterAttributes.Reverse();
            var methodExecutedContext = new MethodExecutedContext(methodExecutingContext);

            foreach (var f in filterAttributes)
            {
                try
                {
                    await f.OnMethodExecutedAsync(methodExecutedContext).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    exception = ex;
                    break;
                }
            }

            if (exception != null)
            {
                await HandleExceptionAsync(new MethodExceptionContext(exception, methodExecutedContext));
            }
        }
Example #7
0
        /// <summary>
        /// Pop the current trace and print info
        /// </summary>
        /// <param name="methodExecutedContext"></param>
        public override void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
        {
            var traces = GetTraceContexts();
            var trace  = traces.Pop();

            trace.Timer.Stop();
            try
            {
                var invocation = methodExecutedContext.Invocation;
                trace.Info.Insert(0, $"{" ".PadLeft(trace.CallLevel * 4)} {(invocation.Method.DeclaringType ?? invocation.TargetType).FullName}.{invocation.Method.Name} : {trace.Timer.ElapsedMilliseconds}ms\r\n");
            }
            finally
            {
                if (!trace.IsNested)
                {
                    Global.Logger.Info("\r\n" + trace.Info);
                }
            }
        }
Example #8
0
        /// <summary>
        /// Revalidate cache async
        /// </summary>
        /// <param name="methodExecutedContext"></param>
        /// <returns></returns>
        public override Task OnMethodExecutedAsync(MethodExecutedContext methodExecutedContext)
        {
            var revalidateKeys = KeyFormats.Select(k => CacheKeyProvider.GetRevalidateKey(methodExecutedContext.Invocation, k)).ToList();

            return(Global.RevalidateCachesAsync(revalidateKeys));
        }
Example #9
0
        /// <summary>
        /// Clear the cache with the provided revalidation keys
        /// </summary>
        /// <param name="methodExecutedContext"></param>
        /// <returns></returns>
        public override void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
        {
            var revalidatedKeys = KeyFormats.Select(k => CacheKeyProvider.GetRevalidateKey(methodExecutedContext.Invocation, k)).ToList();

            Global.RevalidateCaches(revalidatedKeys);
        }
Example #10
0
 public virtual void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
 {
 }