diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs index e59bf0e357..67819d6ba2 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs @@ -1,7 +1,9 @@ +using System; using Volo.Docs.Projects; namespace Volo.Docs.Documents { + [Serializable] public class DocumentWithDetailsDto { public string Title { get; set; } diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/DocumentResourceDto.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/DocumentResourceDto.cs index bd09ba0e25..439409dd09 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/DocumentResourceDto.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/DocumentResourceDto.cs @@ -1,5 +1,8 @@ +using System; + namespace Volo.Docs.Documents { + [Serializable] public class DocumentResourceDto { public byte[] Content { get; set; } diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs index 2f12ecc2e6..a29ea840b3 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs @@ -3,6 +3,7 @@ using Volo.Abp.Application.Dtos; namespace Volo.Docs.Projects { + [Serializable] public class ProjectDto : EntityDto { public string Name { get; set; } diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs index 4b12777fdb..3e68004b87 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs @@ -1,5 +1,8 @@ +using System; using System.Threading.Tasks; +using Microsoft.Extensions.Caching.Distributed; using Volo.Abp.Application.Services; +using Volo.Abp.Caching; using Volo.Docs.Projects; namespace Volo.Docs.Documents @@ -8,13 +11,19 @@ namespace Volo.Docs.Documents { private readonly IProjectRepository _projectRepository; private readonly IDocumentStoreFactory _documentStoreFactory; + protected IDistributedCache DocumentCache { get; } + protected IDistributedCache ResourceCache { get; } public DocumentAppService( IProjectRepository projectRepository, - IDocumentStoreFactory documentStoreFactory) + IDocumentStoreFactory documentStoreFactory, + IDistributedCache documentCache, + IDistributedCache resourceCache) { _projectRepository = projectRepository; _documentStoreFactory = documentStoreFactory; + DocumentCache = documentCache; + ResourceCache = resourceCache; } public virtual async Task GetAsync(GetDocumentInput input) @@ -53,11 +62,22 @@ namespace Volo.Docs.Documents public async Task GetResourceAsync(GetDocumentResourceInput input) { var project = await _projectRepository.GetAsync(input.ProjectId); - var store = _documentStoreFactory.Create(project.DocumentStoreType); + var cacheKey = $"Resource@{project.ShortName}#{input.Name}#{input.Version}"; - var documentResource = await store.GetResource(project, input.Name, input.Version); + return await ResourceCache.GetOrAddAsync( + cacheKey, + async () => + { + var store = _documentStoreFactory.Create(project.DocumentStoreType); + var documentResource = await store.GetResource(project, input.Name, input.Version); - return ObjectMapper.Map(documentResource); + return ObjectMapper.Map(documentResource); + }, + () => new DistributedCacheEntryOptions + { + SlidingExpiration = TimeSpan.FromMinutes(30) //TODO: Configurable? + } + ); } protected virtual async Task GetDocumentWithDetailsDto( @@ -65,10 +85,22 @@ namespace Volo.Docs.Documents string documentName, string version) { - var store = _documentStoreFactory.Create(project.DocumentStoreType); - var document = await store.GetDocument(project, documentName, version); + var cacheKey = $"Document@{project.ShortName}#{documentName}#{version}"; + + return await DocumentCache.GetOrAddAsync( + cacheKey, + async () => + { + var store = _documentStoreFactory.Create(project.DocumentStoreType); + var document = await store.GetDocument(project, documentName, version); - return CreateDocumentWithDetailsDto(project, document); + return CreateDocumentWithDetailsDto(project, document); + }, + () => new DistributedCacheEntryOptions + { + SlidingExpiration = TimeSpan.FromMinutes(30) //TODO: Configurable? + } + ); } protected virtual DocumentWithDetailsDto CreateDocumentWithDetailsDto(Project project, Document document) diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs index dd8cdae909..34f5956980 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs @@ -51,7 +51,7 @@ namespace Volo.Docs.Projects () => GetVersionsAsync(project), () => new DistributedCacheEntryOptions { - SlidingExpiration = TimeSpan.FromSeconds(2) + SlidingExpiration = TimeSpan.FromMinutes(60) //TODO: Configurable? } ); diff --git a/modules/docs/src/Volo.Docs.Domain/Volo.Docs.Domain.csproj b/modules/docs/src/Volo.Docs.Domain/Volo.Docs.Domain.csproj index ac37f6eebc..f8143caa2f 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo.Docs.Domain.csproj +++ b/modules/docs/src/Volo.Docs.Domain/Volo.Docs.Domain.csproj @@ -24,6 +24,7 @@ + diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs index 6742c5adb5..04b6f0278b 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs @@ -15,7 +15,7 @@ using Project = Volo.Docs.Projects.Project; namespace Volo.Docs.GitHub.Documents { - //TODO: Needs refactoring + //TODO: Needs more refactoring public class GithubDocumentStore : DomainService, IDocumentStore { @@ -46,7 +46,7 @@ namespace Volo.Docs.GitHub.Documents LocalDirectory = localDirectory, FileName = fileName, Version = version, - Content = await DownloadWebContentAsync(rawDocumentUrl, project.GetGitHubAccessTokenOrNull()) + Content = await DownloadWebContentAsStringAsync(rawDocumentUrl, project.GetGitHubAccessTokenOrNull()) }; } @@ -136,7 +136,7 @@ namespace Volo.Docs.GitHub.Documents } } - private async Task DownloadWebContentAsync(string rawUrl, string token) + private async Task DownloadWebContentAsStringAsync(string rawUrl, string token) { try {