Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Processes the evict.
        /// </summary>
        /// <param name="invocation">IInvocation.</param>
        /// <returns></returns>
        private string ProcessEvictBefore(IInvocation invocation)
        {
            string preRemoveKey  = string.Empty;
            var    serviceMethod = invocation.Method ?? invocation.MethodInvocationTarget;

            if (GetMethodAttributes(serviceMethod).FirstOrDefault(x => typeof(CachingEvictAttribute).IsAssignableFrom(x.GetType())) is CachingEvictAttribute attribute)
            {
                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);
                }
                //if (attribute.IsAll)
                //{
                //    preRemoveKey = $"{CachingConstValue.PreRemoveAllKeyPrefix}{LinkChar}{ attribute.CacheKeyPrefix.GetHashCode()}";
                //    needRemovedKeys = new string[] { attribute.CacheKeyPrefix, preRemoveKey };
                //}
                //else
                //{
                //    var cacheKey = _keyGenerator.GetCacheKey(serviceMethod, invocation.Arguments, attribute.CacheKeyPrefix);
                //    preRemoveKey = $"{CachingConstValue.PreRemoveKey}{LinkChar}{cacheKey.GetHashCode()}";
                //    needRemovedKeys = new string[] { cacheKey, preRemoveKey };
                //}

                preRemoveKey = $"{CachingConstValue.PreRemoveKey}{LinkChar}{string.Join(",", needRemovedKeys).GetHashCode()}";
                needRemovedKeys.Add(preRemoveKey);
                _cacheProvider.Set(preRemoveKey, needRemovedKeys.ToArray(), TimeSpan.FromSeconds(60 * 60 * 24));
            }
            return(preRemoveKey);
        }
Exemplo n.º 3
0
        /// <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());
        }