Browse Source

feat: swagger.json加载添加缓存处理

pull/126/head
wangjun 3 years ago
parent
commit
ce0d2ecdd4
  1. 2
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/GlobalUsings.cs
  2. 26
      aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Swagger/CachingSwaggerProvider.cs

2
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/GlobalUsings.cs

@ -1,6 +1,7 @@
// Global using directives
global using System;
global using System.Collections.Concurrent;
global using System.Collections.Generic;
global using System.Linq;
global using System.Net;
@ -29,6 +30,7 @@ global using Microsoft.OpenApi.Models;
global using Serilog;
global using Serilog.Exceptions;
global using Serilog.Sinks.Elasticsearch;
global using Swashbuckle.AspNetCore.Swagger;
global using Swashbuckle.AspNetCore.SwaggerGen;
global using Volo.Abp;
global using Volo.Abp.AspNetCore.ExceptionHandling;

26
aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Swagger/CachingSwaggerProvider.cs

@ -0,0 +1,26 @@
namespace Swagger;
/// <summary>
/// swagger注释加载慢,把文档添加到缓存
/// </summary>
[Dependency(ReplaceServices = true)]
[ExposeServices(typeof(ISwaggerProvider))]
public class CachingSwaggerProvider : ISwaggerProvider, ITransientDependency
{
private static readonly ConcurrentDictionary<string, OpenApiDocument> _cache = new ConcurrentDictionary<string, OpenApiDocument>();
private readonly SwaggerGenerator _swaggerGenerator;
public CachingSwaggerProvider(
IOptions<SwaggerGeneratorOptions> optionsAccessor,
IApiDescriptionGroupCollectionProvider apiDescriptionsProvider,
ISchemaGenerator schemaGenerator)
{
_swaggerGenerator = new SwaggerGenerator(optionsAccessor.Value, apiDescriptionsProvider, schemaGenerator);
}
public OpenApiDocument GetSwagger(string documentName, string host = null, string basePath = null)
{
return _cache.GetOrAdd(documentName, (_) => _swaggerGenerator.GetSwagger(documentName, host, basePath));
}
}
Loading…
Cancel
Save