private async Task <DocumentWithDetailsDto> GetDocumentAsync(string documentName, Project project, string languageCode, string version, Document oldDocument = null) { Logger.LogInformation($"Not found in the cache. Requesting {documentName} from the source..."); var source = _documentStoreFactory.Create(project.DocumentStoreType); var sourceDocument = await source.GetDocumentAsync(project, documentName, languageCode, version, oldDocument?.LastSignificantUpdateTime); await _documentRepository.DeleteAsync(project.Id, sourceDocument.Name, sourceDocument.LanguageCode, sourceDocument.Version); await _documentRepository.InsertAsync(sourceDocument, true); Logger.LogInformation($"Document retrieved: {documentName}"); var cacheKey = CacheKeyGenerator.GenerateDocumentUpdateInfoCacheKey( project, sourceDocument.Name, sourceDocument.LanguageCode, sourceDocument.Version ); await DocumentUpdateCache.SetAsync(cacheKey, new DocumentUpdateInfo { Name = sourceDocument.Name, CreationTime = sourceDocument.CreationTime, LastUpdatedTime = sourceDocument.LastUpdatedTime, LastSignificantUpdateTime = sourceDocument.LastSignificantUpdateTime }); return(CreateDocumentWithDetailsDto(project, sourceDocument)); }
protected virtual async Task <DocumentWithDetailsDto> GetDocumentWithDetailsDtoAsync( Project project, string documentName, string languageCode, string version) { version = string.IsNullOrWhiteSpace(version) ? project.LatestVersionBranchName : version; if (HostEnvironment.IsDevelopment()) { return(await GetDocumentAsync(documentName, project, languageCode, version)); } var document = await _documentRepository.FindAsync(project.Id, documentName, languageCode, version); if (document == null) { return(await GetDocumentAsync(documentName, project, languageCode, version)); } //Only the latest version (dev) of the document needs to update the cache. if (!project.LatestVersionBranchName.IsNullOrWhiteSpace() && document.Version == project.LatestVersionBranchName && document.LastCachedTime + _cacheTimeout < DateTime.Now) { return(await GetDocumentAsync(documentName, project, languageCode, version, document)); } var cacheKey = CacheKeyGenerator.GenerateDocumentUpdateInfoCacheKey( project: project, documentName: document.Name, languageCode: document.LanguageCode, version: document.Version ); await DocumentUpdateCache.SetAsync(cacheKey, new DocumentUpdateInfo { Name = document.Name, CreationTime = document.CreationTime, LastUpdatedTime = document.LastUpdatedTime, LastSignificantUpdateTime = document.LastSignificantUpdateTime }); return(CreateDocumentWithDetailsDto(project, document)); }
protected virtual async Task <DocumentWithDetailsDto> GetDocumentWithDetailsDtoAsync( Project project, string documentName, string languageCode, string version) { version = string.IsNullOrWhiteSpace(version) ? project.LatestVersionBranchName : version; async Task <DocumentWithDetailsDto> GetDocumentAsync() { Logger.LogInformation($"Not found in the cache. Requesting {documentName} from the source..."); var source = _documentStoreFactory.Create(project.DocumentStoreType); var sourceDocument = await source.GetDocumentAsync(project, documentName, languageCode, version); await _documentRepository.DeleteAsync(project.Id, sourceDocument.Name, sourceDocument.LanguageCode, sourceDocument.Version); await _documentRepository.InsertAsync(sourceDocument, true); Logger.LogInformation($"Document retrieved: {documentName}"); var cacheKey = $"DocumentUpdateInfo{sourceDocument.ProjectId}#{sourceDocument.Name}#{sourceDocument.LanguageCode}#{sourceDocument.Version}"; await DocumentUpdateCache.SetAsync(cacheKey, new DocumentUpdateInfo { Name = sourceDocument.Name, CreationTime = sourceDocument.CreationTime, LastUpdatedTime = sourceDocument.LastUpdatedTime }); return(CreateDocumentWithDetailsDto(project, sourceDocument)); } if (HostEnvironment.IsDevelopment()) { return(await GetDocumentAsync()); } var document = await _documentRepository.FindAsync(project.Id, documentName, languageCode, version); if (document == null) { return(await GetDocumentAsync()); } //Only the latest version (dev) of the document needs to update the cache. if (!project.LatestVersionBranchName.IsNullOrWhiteSpace() && document.Version == project.LatestVersionBranchName && //TODO: Configurable cache time? document.LastCachedTime + TimeSpan.FromHours(2) < DateTime.Now) { return(await GetDocumentAsync()); } var cacheKey = $"DocumentUpdateInfo{document.ProjectId}#{document.Name}#{document.LanguageCode}#{document.Version}"; await DocumentUpdateCache.SetAsync(cacheKey, new DocumentUpdateInfo { Name = document.Name, CreationTime = document.CreationTime, LastUpdatedTime = document.LastUpdatedTime, }); return(CreateDocumentWithDetailsDto(project, document)); }