Exemplo n.º 1
0
        /// <summary>
        /// Gets the sitemap XML for the current site. If an index of null is passed and there are more than 25,000
        /// sitemap nodes, a sitemap index file is returned (A sitemap index file contains links to other sitemap files
        /// and is a way of splitting up your sitemap into separate files). If an index is specified, a standard
        /// sitemap is returned for the specified index parameter. See http://www.sitemaps.org/protocol.html
        /// </summary>
        /// <param name="index">The index of the sitemap to retrieve. <c>null</c> if you want to retrieve the root
        /// sitemap or sitemap index document, depending on the number of sitemap nodes.</param>
        /// <returns>The sitemap XML for the current site or <c>null</c> if the sitemap index is out of range.</returns>
        public async Task <string> GetSitemapXml(int?index = null)
        {
            // Here we are caching the entire set of sitemap documents. We cannot use the caching attribute because
            // cache expiry could get out of sync if the number of sitemaps changes.
            var sitemapDocuments =
                await _distributedCache.GetAsJsonAsync <List <string> >(CacheProfileName.SitemapNodes);

            if (sitemapDocuments == null)
            {
                IReadOnlyCollection <SitemapNode> sitemapNodes = GetSitemapNodes();
                sitemapDocuments = GetSitemapDocuments(sitemapNodes);
                await _distributedCache.SetAsJsonAsync(
                    CacheProfileName.SitemapNodes,
                    sitemapDocuments,
                    options : new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = _expirationDuration
                });
            }

            if (index.HasValue && ((index < 1) || (index.Value >= sitemapDocuments.Count)))
            {
                return(null);
            }

            return(sitemapDocuments[index ?? 0]);
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public async Task <IEnumerable <Video> > Search(string id)
        {
            var uri = $"{options.Address}movie/{id}/videos?api_key={options.ApiKey}";

            // Try getting the result from the cache
            var videos = await cache.GetAsJsonAsync <IEnumerable <Video> >(uri).ToList();

            if (videos == null)
            {
                // Fetch the results
                var client   = factory.Create();
                var response = await Policies.Retry.ExecuteAsync(() => client.GetAsJson(uri));

                // Parse the results
                videos = new List <Video>();
                foreach (var result in response.results)
                {
                    if (result.type != "Trailer")
                    {
                        continue;
                    }

                    Video video = ParseVideo(result);
                    videos.Add(video);
                }

                // Store the values in the cache
                await cache.SetAsJsonAsync(uri, videos, cacheOptions);
            }

            return(videos);
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public async Task <Movie> Get(string id)
        {
            var uri = $"{options.Address}movie/{id}?api_key={options.ApiKey}";

            // Try getting the result from the cache
            var movie = await cache.GetAsJsonAsync <Movie>(uri);

            if (movie == null)
            {
                // Fetch the movie from the API
                var client   = factory.Create();
                var response = await Policies.Retry.ExecuteAsync(() => client.GetAsJson(uri));

                // Parse the movie
                movie = ParseMovie(response);

                // Store the value in the cache
                await cache.SetAsJsonAsync(uri, movie, cacheOptions);
            }

            return(movie);
        }
Exemplo n.º 4
0
        /// <inheritdoc />
        public async Task <IEnumerable <Video> > Search(string id)
        {
            // Fetch the movie with the specified id
            var movie = await movieService.Get(id);

            // Yeah, not very intelligent, but it does the job :)
            var query = $"{movie.Title} {movie.Year} trailer";

            // Try getting the result from the cache
            var videos = await cache.GetAsJsonAsync <IEnumerable <Video> >(query).ToList();

            if (videos == null)
            {
                var searchListRequest = youtubeService.Search.List("snippet");
                searchListRequest.Q          = query;
                searchListRequest.MaxResults = 50;
                searchListRequest.Type       = "video";

                // Call the search.list method to retrieve results matching the specified query term.
                var searchListResponse = await Policies.Retry.ExecuteAsync(() => searchListRequest.ExecuteAsync());

                // Parse the results
                videos = searchListResponse.Items.Select(searchResult => new Video
                {
                    Name = searchResult.Snippet.Title,
                    Key  = searchResult.Id.VideoId,
                    Type = VideoType.YouTube
                }).ToList();

                // Store the values in the cache
                await cache.SetAsJsonAsync(query, videos, cacheOptions);
            }


            return(videos);
        }