1
0
mirror of https://github.com/mxpv/podsync.git synced 2024-05-11 05:55:04 +00:00

Move feeds caching logic inside FeedService

This commit is contained in:
Maksym Pavlenko
2017-01-18 18:05:27 -08:00
parent ce43d495ca
commit d2a110024f
3 changed files with 52 additions and 38 deletions

View File

@@ -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<IActionResult> 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}");
});
}
}
}

View File

@@ -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<Contracts.Feed> Get(string id)
public Task<Feed> Get(string id)
{
return _rssBuilder.Query(id);
}
public async Task<string> Get(string id, Action<string, Feed> 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;
}
}
}

View File

@@ -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<string> Create(FeedMetadata metadata);
Task<Contracts.Feed> Get(string id);
Task<Feed> Get(string id);
Task<string> Get(string id, Action<string, Feed> fixup);
}
}