From e1996199421c8235fcc126a38a3829a2c2d7a7e3 Mon Sep 17 00:00:00 2001 From: Alper Ebicoglu Date: Mon, 18 Mar 2019 16:03:22 +0300 Subject: [PATCH] Caching documents disabled for debug mode. Because it slows down development --- .../Pages/Docs/Admin/Projects/Edit.cshtml.cs | 6 +-- .../Volo/Docs/Documents/DocumentAppService.cs | 53 ++++++++++++------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/modules/docs/src/Volo.Docs.Admin.Web/Pages/Docs/Admin/Projects/Edit.cshtml.cs b/modules/docs/src/Volo.Docs.Admin.Web/Pages/Docs/Admin/Projects/Edit.cshtml.cs index 77c5321ad0..8f9ce3fe4a 100644 --- a/modules/docs/src/Volo.Docs.Admin.Web/Pages/Docs/Admin/Projects/Edit.cshtml.cs +++ b/modules/docs/src/Volo.Docs.Admin.Web/Pages/Docs/Admin/Projects/Edit.cshtml.cs @@ -38,10 +38,8 @@ namespace Volo.Docs.Admin.Pages.Docs.Admin.Projects SetGithubProjectFromDto(project); return Page(); } - else - { - throw new BusinessException("UnknowDocumentSourceExceptionMessage"); - } + + throw new BusinessException("UnknowDocumentSourceExceptionMessage"); } public async Task OnPostAsync() 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 4aaedd579b..3a4474e877 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,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Logging; @@ -18,8 +19,8 @@ namespace Volo.Docs.Documents public DocumentAppService( IProjectRepository projectRepository, - IDocumentStoreFactory documentStoreFactory, - IDistributedCache documentCache, + IDocumentStoreFactory documentStoreFactory, + IDistributedCache documentCache, IDistributedCache resourceCache) { _projectRepository = projectRepository; @@ -66,15 +67,22 @@ namespace Volo.Docs.Documents var project = await _projectRepository.GetAsync(input.ProjectId); var cacheKey = $"Resource@{project.ShortName}#{input.Name}#{input.Version}"; + async Task GetResourceAsync() + { + var store = _documentStoreFactory.Create(project.DocumentStoreType); + var documentResource = await store.GetResource(project, input.Name, input.Version); + + return ObjectMapper.Map(documentResource); + } + + if (Debugger.IsAttached) + { + return await GetResourceAsync(); + } + 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); - }, + GetResourceAsync, () => new DistributedCacheEntryOptions { //TODO: Configurable? @@ -85,22 +93,29 @@ namespace Volo.Docs.Documents } protected virtual async Task GetDocumentWithDetailsDto( - Project project, - string documentName, + Project project, + string documentName, string version) { var cacheKey = $"Document@{project.ShortName}#{documentName}#{version}"; + async Task GetDocumentAsync() + { + Logger.LogInformation($"Not found in the cache. Requesting {documentName} from the store..."); + var store = _documentStoreFactory.Create(project.DocumentStoreType); + var document = await store.GetDocumentAsync(project, documentName, version); + Logger.LogInformation($"Document retrieved: {documentName}"); + return CreateDocumentWithDetailsDto(project, document); + } + + if (Debugger.IsAttached) + { + return await GetDocumentAsync(); + } + return await DocumentCache.GetOrAddAsync( cacheKey, - async () => - { - Logger.LogInformation($"Not found in the cache. Requesting {documentName} from the store..."); - var store = _documentStoreFactory.Create(project.DocumentStoreType); - var document = await store.GetDocumentAsync(project, documentName, version); - Logger.LogInformation($"Document retrieved: {documentName}"); - return CreateDocumentWithDetailsDto(project, document); - }, + GetDocumentAsync, () => new DistributedCacheEntryOptions { //TODO: Configurable?