Browse Source

Caching documents disabled for debug mode.

Because it slows down development
pull/892/head
Alper Ebicoglu 8 years ago
parent
commit
e199619942
  1. 6
      modules/docs/src/Volo.Docs.Admin.Web/Pages/Docs/Admin/Projects/Edit.cshtml.cs
  2. 53
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs

6
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<IActionResult> OnPostAsync()

53
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<DocumentWithDetailsDto> documentCache,
IDocumentStoreFactory documentStoreFactory,
IDistributedCache<DocumentWithDetailsDto> documentCache,
IDistributedCache<DocumentResourceDto> 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<DocumentResourceDto> GetResourceAsync()
{
var store = _documentStoreFactory.Create(project.DocumentStoreType);
var documentResource = await store.GetResource(project, input.Name, input.Version);
return ObjectMapper.Map<DocumentResource, DocumentResourceDto>(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, DocumentResourceDto>(documentResource);
},
GetResourceAsync,
() => new DistributedCacheEntryOptions
{
//TODO: Configurable?
@ -85,22 +93,29 @@ namespace Volo.Docs.Documents
}
protected virtual async Task<DocumentWithDetailsDto> GetDocumentWithDetailsDto(
Project project,
string documentName,
Project project,
string documentName,
string version)
{
var cacheKey = $"Document@{project.ShortName}#{documentName}#{version}";
async Task<DocumentWithDetailsDto> 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?

Loading…
Cancel
Save