/// <summary> /// Processes the evict. /// </summary> /// <param name="invocation">Invocation.</param> /// <param name="preRemoveKey">preRemoveKey</param> private void ProcessEvictAfter(IInvocation invocation, CachingEvictAttribute attribute, IEnumerable <string> cacheKeys) { var pollyTimeoutSeconds = _cacheProvider.CacheOptions.PollyTimeoutSeconds; var keyExpireSeconds = pollyTimeoutSeconds + 1; _cacheProvider.KeyExpireAsync(cacheKeys, keyExpireSeconds).GetAwaiter().GetResult(); var expireDt = DateTime.Now.AddSeconds(keyExpireSeconds); var cancelTokenSource = new CancellationTokenSource(); var timeoutPolicy = Policy.Timeout(pollyTimeoutSeconds, Polly.Timeout.TimeoutStrategy.Optimistic); timeoutPolicy.Execute((cancellToken) => { invocation.Proceed(); cancellToken.ThrowIfCancellationRequested(); }, cancelTokenSource.Token); try { _cacheProvider.RemoveAll(cacheKeys); } catch (Exception ex) { LocalVariables.Instance.Queue.Enqueue(new LocalVariables.Model(cacheKeys, expireDt)); if (!attribute.IsHighAvailability) { throw; } else { _logger?.LogError(new EventId(), ex, $"Cache provider remove error."); } } }
/// <summary> /// Processes the put. /// </summary> /// <param name="invocation">Invocation.</param> //private void ProcessPut(IInvocation invocation, CachingPutAttribute attribute) //{ // var serviceMethod = invocation.Method ?? invocation.MethodInvocationTarget; // var cacheKey = string.IsNullOrEmpty(attribute.CacheKey) // ? _keyGenerator.GetCacheKey(serviceMethod, invocation.Arguments, attribute.CacheKeyPrefix) // : attribute.CacheKey // ; // try // { // var returnValue = serviceMethod.IsReturnTask() // ? invocation.UnwrapAsyncReturnValue().Result // : invocation.ReturnValue; // _cacheProvider.Set(cacheKey, returnValue, TimeSpan.FromSeconds(attribute.Expiration)); // } // catch (Exception ex) // { // if (!attribute.IsHighAvailability) throw; // else _logger?.LogError(new EventId(), ex, $"Cache provider set error."); // } //} /// <summary> /// Processes the evict. /// </summary> /// <param name="invocation">IInvocation.</param> /// <returns></returns> private List <string> ProcessEvictBefore(IInvocation invocation, CachingEvictAttribute attribute) { var serviceMethod = invocation.Method ?? invocation.MethodInvocationTarget; var needRemovedKeys = new HashSet <string>(); if (!string.IsNullOrEmpty(attribute.CacheKey)) { needRemovedKeys.Add(attribute.CacheKey); } if (attribute.CacheKeys?.Length > 0) { needRemovedKeys.UnionWith(attribute.CacheKeys); } if (!string.IsNullOrWhiteSpace(attribute.CacheKeyPrefix)) { var cacheKeys = _keyGenerator.GetCacheKeys(serviceMethod, invocation.Arguments, attribute.CacheKeyPrefix); needRemovedKeys.UnionWith(cacheKeys); } return(needRemovedKeys.ToList()); }
private (List <string> cacheKeys, DateTime expireDt) ProcessEvictBefore(IInvocation invocation, CachingEvictAttribute attribute) { var serviceMethod = invocation.Method ?? invocation.MethodInvocationTarget; var needRemovedKeys = new HashSet <string>(); if (!string.IsNullOrEmpty(attribute.CacheKey)) { needRemovedKeys.Add(attribute.CacheKey); } if (attribute.CacheKeys?.Length > 0) { needRemovedKeys.UnionWith(attribute.CacheKeys); } if (!string.IsNullOrWhiteSpace(attribute.CacheKeyPrefix)) { var cacheKeys = _keyGenerator.GetCacheKeys(serviceMethod, invocation.Arguments, attribute.CacheKeyPrefix); needRemovedKeys.UnionWith(cacheKeys); } var keyExpireSeconds = _cacheProvider.CacheOptions.PollyTimeoutSeconds + 1; _cacheProvider.KeyExpireAsync(needRemovedKeys, keyExpireSeconds).GetAwaiter().GetResult(); return(needRemovedKeys.ToList(), DateTime.Now.AddSeconds(keyExpireSeconds)); }