Пример #1
0
        public override async Task InterceptAsync(ILmsMethodInvocation invocation)
        {
            var serviceEntry = invocation.ArgumentsDictionary["serviceEntry"] as ServiceEntry;
            var serviceKey   = invocation.ArgumentsDictionary["serviceKey"] as string;
            var parameters   = invocation.ArgumentsDictionary["parameters"] as object[];

            async Task <object> GetResultFirstFromCache(string cacheName, string cacheKey, ServiceEntry entry)
            {
                _distributedCache.UpdateCacheName(cacheName);
                return(await _distributedCache.GetOrAddAsync(cacheKey,
                                                             serviceEntry.MethodInfo.GetReturnType(),
                                                             async() => await entry.Executor(serviceKey, parameters)));
            }

            if (serviceEntry.GovernanceOptions.CacheEnabled)
            {
                var removeCachingInterceptProviders = serviceEntry.RemoveCachingInterceptProviders;
                if (removeCachingInterceptProviders.Any())
                {
                    foreach (var removeCachingInterceptProvider in removeCachingInterceptProviders)
                    {
                        var removeCacheKey = serviceEntry.GetCachingInterceptKey(parameters, removeCachingInterceptProvider);
                        _distributedCache.UpdateCacheName(removeCachingInterceptProvider.CacheName);
                        await _distributedCache.RemoveAsync(removeCacheKey, true);
                    }
                }

                if (serviceEntry.GetCachingInterceptProvider != null)
                {
                    if (serviceEntry.IsTransactionServiceEntry())
                    {
                        await invocation.ProceedAsync();
                    }
                    else
                    {
                        var getCacheKey = serviceEntry.GetCachingInterceptKey(parameters,
                                                                              serviceEntry.GetCachingInterceptProvider);
                        invocation.ReturnValue = await GetResultFirstFromCache(
                            serviceEntry.GetCachingInterceptProvider.CacheName,
                            getCacheKey,
                            serviceEntry);
                    }
                }
                else if (serviceEntry.UpdateCachingInterceptProvider != null)
                {
                    if (serviceEntry.IsTransactionServiceEntry())
                    {
                        await invocation.ProceedAsync();
                    }
                    else
                    {
                        var updateCacheKey = serviceEntry.GetCachingInterceptKey(parameters,
                                                                                 serviceEntry.UpdateCachingInterceptProvider);
                        await _distributedCache.RemoveAsync(updateCacheKey);

                        invocation.ReturnValue = await GetResultFirstFromCache(
                            serviceEntry.GetCachingInterceptProvider.CacheName,
                            updateCacheKey,
                            serviceEntry);
                    }
                }
                else
                {
                    await invocation.ProceedAsync();
                }
            }
            else
            {
                await invocation.ProceedAsync();
            }
        }
Пример #2
0
 public static async Task RemoveAsync(this IDistributedInterceptCache cache, string cacheKey, string cacheName, bool?hideErrors = null,
                                      CancellationToken token = default)
 {
     cache.UpdateCacheName(cacheName);
     await cache.RemoveAsync(cacheKey, hideErrors, token);
 }