Skip to content

Commit

Permalink
Merge pull request #4 from snickler/feature/Caching21
Browse files Browse the repository at this point in the history
Implemented Memory Caching
  • Loading branch information
snickler authored May 12, 2018
2 parents 52d8047 + 94a7e69 commit 4e90e46
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 16 deletions.
11 changes: 11 additions & 0 deletions src/Snickler.RSSCore/Caching/MemoryCacheProvider.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
8 changes: 6 additions & 2 deletions src/Snickler.RSSCore/Extensions/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RSSMiddleware>(path, options);
return builder.UseWhen(context => context.Request.Path.Value.ToLower() == path, config =>
{
config.UseMiddleware<RSSMiddleware>(path, options);
});

}
}
}
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
4 changes: 3 additions & 1 deletion src/Snickler.RSSCore/Models/RSSFeedOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Snickler.RSSCore.Providers;
using System;
using System.Globalization;

namespace Snickler.RSSCore.Models
Expand All @@ -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; }
}
}
14 changes: 14 additions & 0 deletions src/Snickler.RSSCore/Providers/IRSSCacheProvider.cs
Original file line number Diff line number Diff line change
@@ -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; }

}
}
14 changes: 11 additions & 3 deletions src/Snickler.RSSCore/RSSMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
18 changes: 17 additions & 1 deletion src/Snickler.RSSCore/RSSService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -33,6 +37,18 @@ public async Task<string> 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<string> BuildRSSFeedInternal(RSSFeedOptions rssOptions)
{


var sb = new StringBuilder();
using (var _ = new StringWriter(sb))
using (XmlWriter xmlWriter = XmlWriter.Create(_, new XmlWriterSettings { Async = true, Indent = true }))
Expand Down
15 changes: 10 additions & 5 deletions src/Snickler.RSSCore/Snickler.RSSCore.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0</TargetFrameworks>
Expand All @@ -11,18 +11,23 @@
<PackageTags>RSS,aspnetcore</PackageTags>
<RepositoryUrl>https://github.com/snickler/RSSCore</RepositoryUrl>
<Description>ASPNETCore Middleware for generating RSS Feeds</Description>
<AssemblyVersion>1.0.0.1</AssemblyVersion>
<FileVersion>1.0.0.1</FileVersion>
<Version>1.0.1</Version>
<PackageReleaseNotes>-Fixed issue with Middleware failing to advance to the next middlewares</PackageReleaseNotes>
<AssemblyVersion>1.0.0.2</AssemblyVersion>
<FileVersion>1.0.0.2</FileVersion>
<Version>1.0.2</Version>
<PackageReleaseNotes>-Added MemoryCaching by default
-Added UseWhen
-Fixed issue with Middleware calling next improperly.
</PackageReleaseNotes>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="1.1.2" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="2.0.2" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SyndicationFeed.ReaderWriter" Version="1.0.2" />
Expand Down

0 comments on commit 4e90e46

Please sign in to comment.