From 94a7e6975c659f3e06e2ac7ad18176cf4f3e3afa Mon Sep 17 00:00:00 2001 From: Jeremy Sinclair Date: Sat, 12 May 2018 15:53:03 -0400 Subject: [PATCH] Implemented Memory Caching --- .../Caching/MemoryCacheProvider.cs | 11 +++++++++++ .../Extensions/AppBuilderExtensions.cs | 8 ++++++-- .../Extensions/ServiceCollectionExtensions.cs | 4 ---- src/Snickler.RSSCore/Models/RSSFeedOptions.cs | 4 +++- .../Providers/IRSSCacheProvider.cs | 14 ++++++++++++++ src/Snickler.RSSCore/RSSMiddleware.cs | 14 +++++++++++--- src/Snickler.RSSCore/RSSService.cs | 18 +++++++++++++++++- src/Snickler.RSSCore/Snickler.RSSCore.csproj | 15 ++++++++++----- 8 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 src/Snickler.RSSCore/Caching/MemoryCacheProvider.cs create mode 100644 src/Snickler.RSSCore/Providers/IRSSCacheProvider.cs diff --git a/src/Snickler.RSSCore/Caching/MemoryCacheProvider.cs b/src/Snickler.RSSCore/Caching/MemoryCacheProvider.cs new file mode 100644 index 0000000..9084101 --- /dev/null +++ b/src/Snickler.RSSCore/Caching/MemoryCacheProvider.cs @@ -0,0 +1,11 @@ +using Snickler.RSSCore.Providers; +using System; + +namespace Snickler.RSSCore.Caching +{ + public class MemoryCacheProvider : IRSSCacheProvider + { + public TimeSpan CacheDuration { get; set; } + public string Key { get; set; } + } +} diff --git a/src/Snickler.RSSCore/Extensions/AppBuilderExtensions.cs b/src/Snickler.RSSCore/Extensions/AppBuilderExtensions.cs index eaaf43d..d362111 100644 --- a/src/Snickler.RSSCore/Extensions/AppBuilderExtensions.cs +++ b/src/Snickler.RSSCore/Extensions/AppBuilderExtensions.cs @@ -3,11 +3,15 @@ namespace Snickler.RSSCore.Extensions { - public static class AppBuilderExtensions + public static class AppBuilderExtensions { public static IApplicationBuilder UseRSSFeed(this IApplicationBuilder builder, string path, RSSFeedOptions options) { - return builder.UseMiddleware(path, options); + return builder.UseWhen(context => context.Request.Path.Value.ToLower() == path, config => + { + config.UseMiddleware(path, options); + }); + } } } diff --git a/src/Snickler.RSSCore/Extensions/ServiceCollectionExtensions.cs b/src/Snickler.RSSCore/Extensions/ServiceCollectionExtensions.cs index 1533d67..9475c97 100644 --- a/src/Snickler.RSSCore/Extensions/ServiceCollectionExtensions.cs +++ b/src/Snickler.RSSCore/Extensions/ServiceCollectionExtensions.cs @@ -1,9 +1,5 @@ using Microsoft.Extensions.DependencyInjection; -using Snickler.RSSCore.Models; using Snickler.RSSCore.Providers; -using System; -using System.Collections.Generic; -using System.Text; namespace Snickler.RSSCore.Extensions { diff --git a/src/Snickler.RSSCore/Models/RSSFeedOptions.cs b/src/Snickler.RSSCore/Models/RSSFeedOptions.cs index e29541b..5393cf7 100644 --- a/src/Snickler.RSSCore/Models/RSSFeedOptions.cs +++ b/src/Snickler.RSSCore/Models/RSSFeedOptions.cs @@ -1,4 +1,5 @@ -using System; +using Snickler.RSSCore.Providers; +using System; using System.Globalization; namespace Snickler.RSSCore.Models @@ -13,5 +14,6 @@ public class RSSFeedOptions public Uri Url {get;set;} public Uri ImageUrl {get;set;} public CultureInfo Language {get;set;} + public IRSSCacheProvider Caching { get; set; } } } diff --git a/src/Snickler.RSSCore/Providers/IRSSCacheProvider.cs b/src/Snickler.RSSCore/Providers/IRSSCacheProvider.cs new file mode 100644 index 0000000..f0ceba7 --- /dev/null +++ b/src/Snickler.RSSCore/Providers/IRSSCacheProvider.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.Caching.Memory; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Snickler.RSSCore.Providers +{ + public interface IRSSCacheProvider + { + TimeSpan CacheDuration { get; set; } + string Key { get; set; } + + } +} diff --git a/src/Snickler.RSSCore/RSSMiddleware.cs b/src/Snickler.RSSCore/RSSMiddleware.cs index 6139b16..142189e 100644 --- a/src/Snickler.RSSCore/RSSMiddleware.cs +++ b/src/Snickler.RSSCore/RSSMiddleware.cs @@ -16,19 +16,27 @@ public RSSMiddleware(RequestDelegate next, string urlPath, RSSFeedOptions rssOpt _urlPath = urlPath ?? throw new ArgumentNullException(nameof(urlPath)); _rssOptions = rssOptions; } - + public async Task Invoke(HttpContext context, RSSService rssService) { if(context.Request.Path.Equals(_urlPath)) { - context.Response.ContentType = "application/rss+xml"; + var rssFeed = await rssService.BuildRSSFeed(_rssOptions).ConfigureAwait(false); if(!string.IsNullOrEmpty(rssFeed)) { + context.Response.ContentType = "application/rss+xml"; await context.Response.WriteAsync(rssFeed).ConfigureAwait(false); } + else + { + await _next(context); + } + } + else + { + await _next(context); } - await _next(context); } } } diff --git a/src/Snickler.RSSCore/RSSService.cs b/src/Snickler.RSSCore/RSSService.cs index 0d15924..664d0b8 100644 --- a/src/Snickler.RSSCore/RSSService.cs +++ b/src/Snickler.RSSCore/RSSService.cs @@ -10,13 +10,17 @@ using System.Threading.Tasks; using Snickler.RSSCore.Providers; using Snickler.RSSCore.Extensions; +using Microsoft.Extensions.Caching.Memory; + namespace Snickler.RSSCore { public class RSSService { + private readonly IMemoryCache _memoryCache; private readonly IRSSProvider _rssProvider; - public RSSService(IRSSProvider rssProvider) + public RSSService(IRSSProvider rssProvider, IMemoryCache memoryCache) { + _memoryCache = memoryCache; _rssProvider = rssProvider; } @@ -33,6 +37,18 @@ public async Task BuildRSSFeed(RSSFeedOptions rssOptions) throw new ArgumentNullException(nameof(rssOptions.Title)); } + return await _memoryCache.GetOrCreateAsync(rssOptions.Caching?.Key ?? "RSSCache", async caching => + { + caching.SetAbsoluteExpiration(rssOptions.Caching?.CacheDuration ?? TimeSpan.FromDays(1)); + return await BuildRSSFeedInternal(rssOptions); + }); + + } + + private async Task BuildRSSFeedInternal(RSSFeedOptions rssOptions) + { + + var sb = new StringBuilder(); using (var _ = new StringWriter(sb)) using (XmlWriter xmlWriter = XmlWriter.Create(_, new XmlWriterSettings { Async = true, Indent = true })) diff --git a/src/Snickler.RSSCore/Snickler.RSSCore.csproj b/src/Snickler.RSSCore/Snickler.RSSCore.csproj index 12c1d10..6a73bf5 100644 --- a/src/Snickler.RSSCore/Snickler.RSSCore.csproj +++ b/src/Snickler.RSSCore/Snickler.RSSCore.csproj @@ -1,4 +1,4 @@ - + netstandard1.3;netstandard2.0 @@ -11,18 +11,23 @@ RSS,aspnetcore https://github.com/snickler/RSSCore ASPNETCore Middleware for generating RSS Feeds - 1.0.0.1 - 1.0.0.1 - 1.0.1 - -Fixed issue with Middleware failing to advance to the next middlewares + 1.0.0.2 + 1.0.0.2 + 1.0.2 + -Added MemoryCaching by default +-Added UseWhen +-Fixed issue with Middleware calling next improperly. + + +