Example #1
0
        public virtual async Task <NavigationNode> GetNavigationAsync(GetNavigationDocumentInput input)
        {
            var project = await _projectRepository.GetAsync(input.ProjectId);

            input.Version = GetProjectVersionPrefixIfExist(project) + input.Version;

            var navigationDocument = await GetDocumentWithDetailsDtoAsync(
                project,
                project.NavigationDocumentName,
                input.LanguageCode,
                input.Version
                );

            if (!JsonConvertExtensions.TryDeserializeObject <NavigationNode>(navigationDocument.Content,
                                                                             out var navigationNode))
            {
                throw new UserFriendlyException(
                          $"Cannot validate navigation file '{project.NavigationDocumentName}' for the project {project.Name}.");
            }

            var leafs = navigationNode.Items.GetAllNodes(x => x.Items)
                        .Where(x => !x.Path.IsNullOrWhiteSpace())
                        .ToList();

            foreach (var leaf in leafs)
            {
                var cacheKey =
                    CacheKeyGenerator.GenerateDocumentUpdateInfoCacheKey(project, leaf.Path, input.LanguageCode,
                                                                         input.Version);
                var documentUpdateInfo = await DocumentUpdateCache.GetAsync(cacheKey);

                if (documentUpdateInfo != null)
                {
                    leaf.CreationTime              = documentUpdateInfo.CreationTime;
                    leaf.LastUpdatedTime           = documentUpdateInfo.LastUpdatedTime;
                    leaf.LastSignificantUpdateTime = documentUpdateInfo.LastSignificantUpdateTime;
                }
            }

            await NavigationTreePostProcessor.ProcessAsync(
                new NavigationTreePostProcessorContext(
                    navigationDocument,
                    navigationNode
                    )
                );

            return(navigationNode);
        }
Example #2
0
        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));
        }
        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));
        }
Example #4
0
        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));
        }