Browse Source

#418 Caching documents.

pull/671/head
Halil ibrahim Kalkan 8 years ago
parent
commit
b0cf0352ac
  1. 2
      modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs
  2. 3
      modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/DocumentResourceDto.cs
  3. 1
      modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs
  4. 46
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs
  5. 2
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs
  6. 1
      modules/docs/src/Volo.Docs.Domain/Volo.Docs.Domain.csproj
  7. 6
      modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs

2
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; }

3
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; }

1
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<Guid>
{
public string Name { get; set; }

46
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<DocumentWithDetailsDto> DocumentCache { get; }
protected IDistributedCache<DocumentResourceDto> ResourceCache { get; }
public DocumentAppService(
IProjectRepository projectRepository,
IDocumentStoreFactory documentStoreFactory)
IDocumentStoreFactory documentStoreFactory,
IDistributedCache<DocumentWithDetailsDto> documentCache,
IDistributedCache<DocumentResourceDto> resourceCache)
{
_projectRepository = projectRepository;
_documentStoreFactory = documentStoreFactory;
DocumentCache = documentCache;
ResourceCache = resourceCache;
}
public virtual async Task<DocumentWithDetailsDto> GetAsync(GetDocumentInput input)
@ -53,11 +62,22 @@ namespace Volo.Docs.Documents
public async Task<DocumentResourceDto> 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, DocumentResourceDto>(documentResource);
return ObjectMapper.Map<DocumentResource, DocumentResourceDto>(documentResource);
},
() => new DistributedCacheEntryOptions
{
SlidingExpiration = TimeSpan.FromMinutes(30) //TODO: Configurable?
}
);
}
protected virtual async Task<DocumentWithDetailsDto> 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)

2
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?
}
);

1
modules/docs/src/Volo.Docs.Domain/Volo.Docs.Domain.csproj

@ -24,6 +24,7 @@
<ItemGroup>
<ProjectReference Include="..\Volo.Docs.Domain.Shared\Volo.Docs.Domain.Shared.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Ddd.Domain\Volo.Abp.Ddd.Domain.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Caching\Volo.Abp.Caching.csproj" />
</ItemGroup>
</Project>

6
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<string> DownloadWebContentAsync(string rawUrl, string token)
private async Task<string> DownloadWebContentAsStringAsync(string rawUrl, string token)
{
try
{

Loading…
Cancel
Save