public void BeforeInvoke(InvocationContext invocationContext) { try { 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); } _cacheKey = invocationContext.GetExecutingMethodInfo().GetParameters().Length > 0 ? CacheKeyGenerator.GenerateCacheKey(invocationContext.GetExecutingMethodInfo(), argsDictionary) : invocationContext.GetExecutingMethodInfo().Name; var doesCacheExist = _cacheProvider.Get(_cacheKey); if (doesCacheExist != null) { invocationContext.OverrideMethodReturnValue(doesCacheExist); invocationContext.BypassInvocation(); } } catch (Exception ex) { _logger.ErrorException(ex.Message, ex); throw; } }
protected override void BeforeInvoke(InvocationContext invocationContext) { _memoryCache.TryGetValue(invocationContext.GetExecutingMethodName(), out var result); if (result != null) { invocationContext.OverrideMethodReturnValue(result); invocationContext.BypassInvocation(); } }
public void BeforeInvoke(InvocationContext invocationContext) { _memoryCacheAttribute = invocationContext.GetAttributeFromMethod<MemoryCacheAttribute>(); var cacheKey = _memoryCacheAttribute.CacheKey; var cacheConfig = _cacheConfigFactory.Config[cacheKey]; if (cacheConfig.Enabled) { if (!string.IsNullOrWhiteSpace(_memoryCacheAttribute.CacheKeyParameterSubstitutions)) { List<object> paramValues = new List<object>(); string[] paramNames = _memoryCacheAttribute.CacheKeyParameterSubstitutions.Split(",", StringSplitOptions.RemoveEmptyEntries); foreach (string paramName in paramNames) { paramValues.Add(invocationContext.GetParameterValue(paramName.Trim())); } cacheKey = string.Format(cacheConfig.CacheKey.ToString(), paramValues.ToArray()); } CacheKey = cacheKey; if (_memoryCacheAttribute.RefreshCache) { _memoryCacheService.Remove(CacheKey); } var cacheReturnObj = RetrieveCacheEntry(invocationContext, CacheKey); if (cacheReturnObj != null && cacheReturnObj.CacheEntry != null) { var returnType = invocationContext.GetMethodReturnType(); var isAsyncMethod = returnType != null && returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task<>); if (isAsyncMethod) { invocationContext.OverrideAsyncMethodReturnValue(cacheReturnObj.CacheEntry); } else { invocationContext.OverrideMethodReturnValue(cacheReturnObj.CacheEntry); } invocationContext.TempData[CacheConstants.LOGGING_CACHE_INVOCATIONCONETXT] = string.Format( "{0}{1}{2}", CacheConstants.LOGGING_CACHE_HIT, CacheConstants.LOGGING_CACHE_DELIMITER, cacheKey); invocationContext.BypassInvocation(); } else { invocationContext.TempData[CacheConstants.LOGGING_CACHE_INVOCATIONCONETXT] = string.Format( "{0}{1}{2}", CacheConstants.LOGGING_CACHE_MISS, CacheConstants.LOGGING_CACHE_DELIMITER, cacheKey); } } }
/// <inheritdoc /> public void BeforeInvoke(InvocationContext invocationContext) { // Check the Memory Cache for a cached value for this method this.memoryCache.TryGetValue(invocationContext.GetExecutingMethodName(), out var result); // If a cached value is found; replace the method return value and dont execute // the real underlying method if (result != null) { invocationContext.OverrideMethodReturnValue(result); invocationContext.BypassInvocation(); } }
public void BeforeInvoke(InvocationContext invocationContext) { this._cacheAttribute = invocationContext.GetAttributeFromMethod <CacheAttribute>(); if (typeof(ICacheManager).IsAssignableFrom(_cacheAttribute._cacheType) == false) { throw new Exception("Wrong cache manager"); } cacheManager = (ICacheManager)ActivatorUtilities.CreateInstance(_serviceProvider, _cacheAttribute._cacheType); string key = GetMethodKey(invocationContext); cacheManager.TryGet(key, out var result); if (result != null) { invocationContext.OverrideMethodReturnValue(result); invocationContext.BypassInvocation(); } }