Exemplo n.º 1
0
        /// <summary>
        /// Execute cache operation
        /// </summary>
        /// <returns>Return cache result</returns>
        internal async Task <CacheResult <TResponse> > ExecuteAsync()
        {
            var servers = GetCacheServers();

            if (servers.IsNullOrEmpty())
            {
                return(CacheResult <TResponse> .EmptyResult());
            }
            CacheResult <TResponse> result = new CacheResult <TResponse>();

            //Single cache server
            if (servers.Count == 1)
            {
                var firstServer = servers.First();
                var provider    = CacheManager.Configuration.GetCacheProvider(firstServer.ServerType);
                if (provider != null)
                {
                    result.AddResponse(await ExecuteCacheOperationAsync(provider, firstServer).ConfigureAwait(false));
                }
                return(result);
            }

            //Multiple cache server
            Task <TResponse>[] cacheTasks = new Task <TResponse> [servers.Count];
            var serverIndex = 0;

            foreach (var server in servers)
            {
                if (server == null)
                {
                    continue;
                }
                var provider = CacheManager.Configuration.GetCacheProvider(server.ServerType);
                if (provider == null)
                {
                    continue;
                }
                cacheTasks[serverIndex] = ExecuteCacheOperationAsync(provider, server);
                serverIndex++;
            }
            result.AddResponse(await Task.WhenAll(cacheTasks).ConfigureAwait(false));
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Execute cache operation
        /// </summary>
        /// <param name="server">Cache server</param>
        /// <returns>Return cache result</returns>
        internal async Task <CacheResult <TResponse> > ExecuteAsync(CacheServer server)
        {
            if (server == null)
            {
                return(CacheResult <TResponse> .EmptyResult());
            }
            var provider = CacheManager.Configuration.GetCacheProvider(server.ServerType);

            if (provider == null)
            {
                return(CacheResult <TResponse> .EmptyResult());
            }
            CacheResult <TResponse> result = new CacheResult <TResponse>();
            var response = await ExecuteCacheOperationAsync(provider, server).ConfigureAwait(false);

            if (response != null)
            {
                result.AddResponse(response);
            }
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Execute cache operation
        /// </summary>
        /// <returns>Return cache result</returns>
        internal async Task <CacheResult <TResponse> > ExecuteAsync()
        {
            var servers = GetCacheServers();

            if (servers.IsNullOrEmpty())
            {
                return(CacheResult <TResponse> .EmptyResult());
            }
            CacheResult <TResponse> result = new CacheResult <TResponse>();

            //In memory first
            var inmemoryServer = servers.FirstOrDefault(c => c.ServerType == CacheServerType.InMemory);

            if (InMemoryFirst && inmemoryServer != null)
            {
                var memoryProvider = CacheManager.Configuration.GetCacheProvider(CacheServerType.InMemory);
                if (memoryProvider != null)
                {
                    var cacheResponses = await ExecuteCacheOperationAsync(memoryProvider, inmemoryServer).ConfigureAwait(false);

                    foreach (var cacheRes in cacheResponses)
                    {
                        if (VerifyResponse?.Invoke(cacheRes) ?? true)
                        {
                            result.AddResponse(cacheRes);
                        }
                    }
                }
                servers = servers.Where(c => c.ServerType != CacheServerType.InMemory).ToList();
            }

            //Single cache server
            if (servers.Count == 1)
            {
                var firstServer = servers.First();
                var provider    = CacheManager.Configuration.GetCacheProvider(firstServer.ServerType);
                if (provider == null)
                {
                    LogManager.LogError <CacheOptions <TResponse> >($"Cache server :{firstServer.ServerType} no provider");
                    return(result);
                }
                result.AddResponse(await ExecuteCacheOperationAsync(provider, firstServer).ConfigureAwait(false));
                return(result);
            }

            //Multiple cache server
            Task <IEnumerable <TResponse> >[] cacheTasks = new Task <IEnumerable <TResponse> > [servers.Count];
            var serverIndex = 0;

            foreach (var server in servers)
            {
                if (server == null)
                {
                    continue;
                }
                var provider = CacheManager.Configuration.GetCacheProvider(server.ServerType);
                if (provider == null)
                {
                    continue;
                }
                cacheTasks[serverIndex] = ExecuteCacheOperationAsync(provider, server);
                serverIndex++;
            }
            var taskResponses = await Task.WhenAll(cacheTasks).ConfigureAwait(false);

            foreach (var taskRes in taskResponses)
            {
                result.AddResponse(taskRes);
            }
            return(result);
        }