From d2a110024fa15944946561cc42ffb6c18b2c290a Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Wed, 18 Jan 2017 18:05:27 -0800 Subject: [PATCH] Move feeds caching logic inside FeedService --- src/Podsync/Controllers/FeedController.cs | 60 ++++++++++------------- src/Podsync/Services/Rss/FeedService.cs | 22 ++++++++- src/Podsync/Services/Rss/IFeedService.cs | 8 ++- 3 files changed, 52 insertions(+), 38 deletions(-) diff --git a/src/Podsync/Controllers/FeedController.cs b/src/Podsync/Controllers/FeedController.cs index fcaf435..8986fe8 100644 --- a/src/Podsync/Controllers/FeedController.cs +++ b/src/Podsync/Controllers/FeedController.cs @@ -27,13 +27,11 @@ namespace Podsync.Controllers private readonly ILinkService _linkService; private readonly IFeedService _feedService; - private readonly IStorageService _storageService; - public FeedController(IRssBuilder rssBuilder, ILinkService linkService, IStorageService storageService, IFeedService feedService, IStorageService storageService1) + public FeedController(IRssBuilder rssBuilder, ILinkService linkService, IFeedService feedService) { _linkService = linkService; _feedService = feedService; - _storageService = storageService1; } [HttpPost] @@ -75,41 +73,33 @@ namespace Podsync.Controllers [ValidateModelState] public async Task Feed([Required] string feedId) { - var serializedFeed = await _storageService.GetCached(Constants.Cache.FeedsPrefix, feedId); - - if (string.IsNullOrEmpty(serializedFeed)) + try { - Feed feed; - - try - { - feed = await _feedService.Get(feedId); - } - catch (KeyNotFoundException) - { - return NotFound($"ERROR: No feed with id {feedId}"); - } - - var selfHost = Request.GetBaseUrl(); - - // Set atom link to this feed - // See https://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html - var selfLink = new Uri(selfHost, Request.Path); - feed.Channels.ForEach(x => x.AtomLink = selfLink); - - // No magic here, just make download links to DownloadController.Download - feed.Channels.SelectMany(x => x.Items).ForEach(item => - { - var ext = Extensions[item.ContentType]; - item.DownloadLink = new Uri(selfHost, $"download/{feedId}/{item.Id}.{ext}"); - }); - - serializedFeed = feed.ToString(); - - await _storageService.Cache(Constants.Cache.FeedsPrefix, feedId, serializedFeed, TimeSpan.FromMinutes(3)); + var body = await _feedService.Get(feedId, FixLinks); + return Content(body, "application/rss+xml; charset=UTF-8"); } + catch (KeyNotFoundException) + { + return NotFound($"ERROR: No feed with id {feedId}"); + } + } - return Content(serializedFeed, "application/rss+xml; charset=UTF-8"); + [NonAction] + private void FixLinks(string feedId, Feed feed) + { + var selfHost = Request.GetBaseUrl(); + + // Set atom link to this feed + // See https://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html + var selfLink = new Uri(selfHost, Request.Path); + feed.Channels.ForEach(x => x.AtomLink = selfLink); + + // No magic here, just make download links to DownloadController.Download + feed.Channels.SelectMany(x => x.Items).ForEach(item => + { + var ext = Extensions[item.ContentType]; + item.DownloadLink = new Uri(selfHost, $"download/{feedId}/{item.Id}.{ext}"); + }); } } } \ No newline at end of file diff --git a/src/Podsync/Services/Rss/FeedService.cs b/src/Podsync/Services/Rss/FeedService.cs index 7a58676..2e20b02 100644 --- a/src/Podsync/Services/Rss/FeedService.cs +++ b/src/Podsync/Services/Rss/FeedService.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using Podsync.Helpers; using Podsync.Services.Links; +using Podsync.Services.Rss.Contracts; using Podsync.Services.Storage; namespace Podsync.Services.Rss @@ -27,9 +28,28 @@ namespace Podsync.Services.Rss return _storageService.Save(metadata); } - public Task Get(string id) + public Task Get(string id) { return _rssBuilder.Query(id); } + + public async Task Get(string id, Action fixup) + { + var serializedFeed = await _storageService.GetCached(Constants.Cache.FeedsPrefix, id); + + if (string.IsNullOrEmpty(serializedFeed)) + { + var feed = await Get(id); + + // Fix download links + fixup(id, feed); + + // Add to cache + serializedFeed = feed.ToString(); + await _storageService.Cache(Constants.Cache.FeedsPrefix, id, serializedFeed, TimeSpan.FromMinutes(3)); + } + + return serializedFeed; + } } } \ No newline at end of file diff --git a/src/Podsync/Services/Rss/IFeedService.cs b/src/Podsync/Services/Rss/IFeedService.cs index c7a013d..6132163 100644 --- a/src/Podsync/Services/Rss/IFeedService.cs +++ b/src/Podsync/Services/Rss/IFeedService.cs @@ -1,4 +1,6 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; +using Podsync.Services.Rss.Contracts; using Podsync.Services.Storage; namespace Podsync.Services.Rss @@ -7,6 +9,8 @@ namespace Podsync.Services.Rss { Task Create(FeedMetadata metadata); - Task Get(string id); + Task Get(string id); + + Task Get(string id, Action fixup); } } \ No newline at end of file