Ejemplo n.º 1
0
        public async Task <IActionResult> Search(string term)
        {
            term = (term ?? "").Trim();

            IEnumerable <SearchResult> results;

            if (term == "")
            {
                results = Array.Empty <SearchResult>();
            }
            else
            {
                var allPosts = await _postRepository.GetAll();

                results = _postIndexer.GenerateIndexContent(allPosts).Search(term).Select(
                    m => new SearchResult(allPosts.First(p => p.Id == m.Key), m.Weight, m.SourceLocationsIfRecorded)
                    );
            }

            return(View(
                       "Search",
                       new SearchResultsModel(
                           term,
                           results.ToNonNullImmutableList(),
                           await _postRepository.GetMostRecentStubs(5),
                           await _postRepository.GetStubs(null, null, true),
                           await _postRepository.GetArchiveLinks(),
                           _siteConfiguration.OptionalCanonicalLinkBase,
                           _siteConfiguration.GetGoogleAnalyticsIdIfAny(Request),
                           _postContentCache
                           )
                       ));
        }
Ejemplo n.º 2
0
        private CachedPostIndexContent GetData(NonNullImmutableList <Post> posts)
        {
            if (posts == null)
            {
                throw new ArgumentNullException(nameof(posts));
            }

            var cachedData = _cache.TryToRetrieve();

            if ((cachedData != null) && cachedData.IsValidForPostsData(posts))
            {
                return(cachedData);
            }

            var postIndexData = _postIndexer.GenerateIndexContent(posts);
            var liveData      = new CachedPostIndexContent(
                postIndexData,
                posts
                );

            _cache.Store(liveData);
            return(liveData);
        }