public async Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
            {
                var argument      = methodInvocation.GetArgument("value");
                var argumentValue = (int)argument.Value;

                int result;

                return(PrecalculatedValues.TryGetValue(argumentValue, out result)
                    ? methodInvocation.CreateResult(result)
                    : await methodInvocation.InvokeNextAsync().ConfigureAwait(false));
            }
Exemple #2
0
        private async Task <IMethodInvocationResult> GetFromCacheAsync(IAsyncMethodInvocation methodInvocation)
        {
            var parentKey = GetClassAndMethodName(methodInvocation);

            var useCache = cache.Get <IDictionary <string, object> >(parentKey)
                           .NoneWhenNullOrDefault(() => ResolveCacheDictionary(parentKey));

            var keyName = GetParamWithValueKey(methodInvocation);

            return(useCache.TryGetValue(keyName, out object cacheEntry)
                ? methodInvocation.CreateResult(cacheEntry)
                : await SetCacheAndReturnAsync(methodInvocation, useCache, keyName));
        }
Exemple #3
0
        public async Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
        {
            var cache = methodInvocation.MethodInfo.GetCustomAttributes(typeof(Cached), false).FirstOrDefault();

            if (cache != null)
            {
                var cacheObject = new MethodCacheObject(methodInvocation);

                var cacheManager = AppCore.Instance.Get <ICacheStrategy <MethodCacheObject> >();

                var cachedObject = cacheManager.MemoryCache.Get(cacheObject.Key).Result;

                if (null != cachedObject)
                {
                    if (DateTime.Now > cachedObject.Expiration)
                    {
                        cacheManager.MemoryCache.Remove(cachedObject.Key).Wait();
                    }
                    else
                    {
                        this.LogInformation($"{nameof(AsyncCachingInterceptor)} - {methodInvocation.MethodInfo.Name}");

                        return(methodInvocation.CreateResult(cachedObject.Value));
                    }
                }

                var result = await methodInvocation.InvokeNextAsync();

                cacheObject.Value = result.GetReturnValueOrThrow();

                if (null == cacheObject.Value)
                {
                    return(result);
                }

                cacheObject.Expiration = DateTime.Now.AddMilliseconds(((Cached)cache).Duration);

                await cacheManager.MemoryCache.Put(cacheObject.Key, cacheObject);

                return(result);
            }

            return(await methodInvocation.InvokeNextAsync());
        }
            public async Task<IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
            {
                var argument = methodInvocation.GetArgument("value");
                var argumentValue = (int)argument.Value;

                int result;
                return PrecalculatedValues.TryGetValue(argumentValue, out result)
                    ? methodInvocation.CreateResult(result)
                    : await methodInvocation.InvokeNextAsync().ConfigureAwait(false);
            }