mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	unit tests for cache clearing passing
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
@@ -13,16 +14,19 @@ namespace Ocelot.Cache.Middleware
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IOcelotLogger _logger;
 | 
			
		||||
        private readonly IOcelotCache<HttpResponseMessage> _outputCache;
 | 
			
		||||
        private readonly IRegionCreator _regionCreator;
 | 
			
		||||
 | 
			
		||||
        public OutputCacheMiddleware(RequestDelegate next,
 | 
			
		||||
            IOcelotLoggerFactory loggerFactory,
 | 
			
		||||
            IRequestScopedDataRepository scopedDataRepository,
 | 
			
		||||
            IOcelotCache<HttpResponseMessage> outputCache)
 | 
			
		||||
            IOcelotCache<HttpResponseMessage> outputCache,
 | 
			
		||||
            IRegionCreator regionCreator)
 | 
			
		||||
            :base(scopedDataRepository)
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _outputCache = outputCache;
 | 
			
		||||
            _logger = loggerFactory.CreateLogger<OutputCacheMiddleware>();
 | 
			
		||||
            _regionCreator = regionCreator;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Invoke(HttpContext context)
 | 
			
		||||
@@ -63,7 +67,7 @@ namespace Ocelot.Cache.Middleware
 | 
			
		||||
 | 
			
		||||
            var response = HttpResponseMessage;
 | 
			
		||||
 | 
			
		||||
            var region = $"{DownstreamRoute.ReRoute.UpstreamHttpMethod}-{DownstreamRoute.ReRoute.UpstreamPathTemplate.Value}";
 | 
			
		||||
            var region = _regionCreator.Region(DownstreamRoute.ReRoute);
 | 
			
		||||
 | 
			
		||||
            _outputCache.Add(downstreamUrlKey, response, TimeSpan.FromSeconds(DownstreamRoute.ReRoute.FileCacheOptions.TtlSeconds), region);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										22
									
								
								src/Ocelot/Cache/RegionBuilder.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/Ocelot/Cache/RegionBuilder.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Cache
 | 
			
		||||
{
 | 
			
		||||
    public interface IRegionCreator
 | 
			
		||||
    {
 | 
			
		||||
        string Region(ReRoute reRoute);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public class RegionCreator : IRegionCreator
 | 
			
		||||
    {
 | 
			
		||||
        public string Region(ReRoute reRoute)
 | 
			
		||||
        {
 | 
			
		||||
            var methods = string.Join(",", reRoute.UpstreamHttpMethod.Select(m => m.Method));
 | 
			
		||||
 | 
			
		||||
            var region = $"{methods} {reRoute.UpstreamPathTemplate.Value}";
 | 
			
		||||
            
 | 
			
		||||
            return region;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										47
									
								
								src/Ocelot/Cache/RegionsGetter.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/Ocelot/Cache/RegionsGetter.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Ocelot.Configuration.Provider;
 | 
			
		||||
using Ocelot.Logging;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Cache
 | 
			
		||||
{
 | 
			
		||||
    public interface IRegionsGetter
 | 
			
		||||
    {
 | 
			
		||||
        Task<List<string>> Regions();
 | 
			
		||||
    }
 | 
			
		||||
    public class RegionsGetter : IRegionsGetter
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IOcelotConfigurationProvider _provider;
 | 
			
		||||
        private readonly IRegionCreator _creator;
 | 
			
		||||
        private readonly IOcelotLogger _logger;
 | 
			
		||||
 | 
			
		||||
        public RegionsGetter(IOcelotConfigurationProvider provider, IRegionCreator creator, IOcelotLoggerFactory loggerFactory)
 | 
			
		||||
        {
 | 
			
		||||
            _logger = loggerFactory.CreateLogger<RegionsGetter>();
 | 
			
		||||
            _provider = provider;
 | 
			
		||||
            _creator = creator;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<List<string>> Regions()
 | 
			
		||||
        {
 | 
			
		||||
            var config = await _provider.Get();
 | 
			
		||||
 | 
			
		||||
            if(config.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                _logger.LogError("unable to find regions", new Exception(string.Join(",", config.Errors)));
 | 
			
		||||
                return new List<string>();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var regions = new List<string>();
 | 
			
		||||
 | 
			
		||||
            foreach(var reRoute in config.Data.ReRoutes)
 | 
			
		||||
            {
 | 
			
		||||
                var region = _creator.Region(reRoute);
 | 
			
		||||
                regions.Add(region);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return regions;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -2,6 +2,7 @@ using System.Net.Http;
 | 
			
		||||
using Microsoft.AspNetCore.Authorization;
 | 
			
		||||
using Microsoft.AspNetCore.Mvc;
 | 
			
		||||
using Ocelot.Cache;
 | 
			
		||||
using Ocelot.Configuration.Provider;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Controllers
 | 
			
		||||
{
 | 
			
		||||
@@ -10,16 +11,19 @@ namespace Ocelot.Controllers
 | 
			
		||||
    public class OutputCacheController : Controller
 | 
			
		||||
    {
 | 
			
		||||
        private IOcelotCache<HttpResponseMessage> _cache;
 | 
			
		||||
        private IRegionsGetter _regionsGetter;
 | 
			
		||||
 | 
			
		||||
        public OutputCacheController(IOcelotCache<HttpResponseMessage> cache)
 | 
			
		||||
        public OutputCacheController(IOcelotCache<HttpResponseMessage> cache, IRegionsGetter regionsGetter)
 | 
			
		||||
        {
 | 
			
		||||
            _cache = cache;
 | 
			
		||||
            _regionsGetter = regionsGetter;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [HttpGet]
 | 
			
		||||
        public IActionResult Get()
 | 
			
		||||
        {
 | 
			
		||||
            return new NotFoundResult();
 | 
			
		||||
            var regions = _regionsGetter.Regions();
 | 
			
		||||
            return new OkObjectResult(regions);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [HttpDelete]
 | 
			
		||||
 
 | 
			
		||||
@@ -145,6 +145,8 @@ namespace Ocelot.DependencyInjection
 | 
			
		||||
                .AddJsonFormatters();
 | 
			
		||||
 | 
			
		||||
            services.AddLogging();
 | 
			
		||||
            services.TryAddSingleton<IRegionsGetter, RegionsGetter>();
 | 
			
		||||
            services.TryAddSingleton<IRegionCreator, RegionCreator>();
 | 
			
		||||
            services.TryAddSingleton<IFileConfigurationRepository, FileConfigurationRepository>();
 | 
			
		||||
            services.TryAddSingleton<IFileConfigurationSetter, FileConfigurationSetter>();
 | 
			
		||||
            services.TryAddSingleton<IFileConfigurationProvider, FileConfigurationProvider>();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user