예제 #1
0
        public void AfterInvoke(InvocationContext invocationContext, object methodResult)
        {
            try
            {
                var attribute = invocationContext.GetAttributeFromMethod <CacheAttribute>();

                var argsDictionary = new Dictionary <string, object>();
                var args           = invocationContext.GetExecutingMethodInfo().GetParameters();
                for (var i = 0; i < args.Length; i++)
                {
                    var argumentValue = invocationContext.GetParameterValue(i);
                    var argumentName  = args[i].Name;
                    argsDictionary.Add(argumentName, argumentValue);
                }

                var cacheKey = invocationContext.GetExecutingMethodInfo().GetParameters().Length > 0
                    ? CacheKeyGenerator.GenerateCacheKey(invocationContext.GetExecutingMethodInfo(), argsDictionary)
                    : invocationContext.GetExecutingMethodInfo().Name;

                var cachingDuration = new TimeSpan(attribute.Day, attribute.Hour, attribute.Minute, attribute.Second);

                if (cachingDuration > TimeSpan.Zero)
                {
                    _cacheProvider.AddOrUpdate(cacheKey, methodResult, cachingDuration);
                }
                else
                {
                    _cacheProvider.AddOrUpdate(_cacheKey, methodResult, new TimeSpan(0, 10, 0));
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorException(ex.Message, ex);
                throw;
            }
        }