Skip to content

Commit

Permalink
Revert "feat: RPDE cache headers (#163)"
Browse files Browse the repository at this point in the history
This reverts commit e73eee3.
  • Loading branch information
nickevansuk committed Sep 30, 2021
1 parent e73eee3 commit 8382e22
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 72 deletions.
37 changes: 3 additions & 34 deletions Examples/BookingSystem.AspNetCore/Helpers/ResponseContentHelper.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,15 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BookingSystem.AspNetCore.Helpers
namespace BookingSystem.AspNetCore.Helpers
{
public static class ResponseContentHelper
{
public static Microsoft.AspNetCore.Mvc.ContentResult GetContentResult(this OpenActive.Server.NET.OpenBookingHelper.ResponseContent response)
{
return new CacheableContentResult
return new Microsoft.AspNetCore.Mvc.ContentResult
{
StatusCode = (int)response.StatusCode,
Content = response.Content,
ContentType = response.ContentType,
CacheControlMaxAge = response.CacheControlMaxAge,
ContentType = response.ContentType
};
}
}

/// <summary>
/// ContentResult that also sets Cache-Control: public, max-age=X, s-maxage=X
/// See https://developer.openactive.io/publishing-data/data-feeds/scaling-feeds for more information
/// </summary>
public class CacheableContentResult : Microsoft.AspNetCore.Mvc.ContentResult
{
public TimeSpan CacheControlMaxAge { get; set; }

public override async Task ExecuteResultAsync(ActionContext context)
{
if (CacheControlMaxAge != null)
{
context.HttpContext.Response.GetTypedHeaders().CacheControl =
new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
{
Public = true,
MaxAge = CacheControlMaxAge,
SharedMaxAge = CacheControlMaxAge,
};
}

await base.ExecuteResultAsync(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ public static HttpResponseMessage GetContentResult(this OpenActive.Server.NET.Op
StatusCode = response.StatusCode
};
resp.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(response.ContentType);
/// Sets additional header Cache-Control: public, max-age=X, s-maxage=X
/// See https://developer.openactive.io/publishing-data/data-feeds/scaling-feeds for more information
if (response.CacheControlMaxAge != null)
{
resp.Headers.CacheControl = new CacheControlHeaderValue()
{
Public = true,
MaxAge = response.CacheControlMaxAge,
SharedMaxAge = response.CacheControlMaxAge
};
}
// Ensure custom error messages do not override responses
HttpContext.Current.Response.TrySkipIisCustomErrors = true;
return resp;
Expand Down
14 changes: 6 additions & 8 deletions OpenActive.Server.NET/CustomBookingEngine/CustomBookingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,16 @@ public async Task<ResponseContent> RenderDatasetSite()
/// <returns></returns>
public async Task<ResponseContent> GetOpenDataRPDEPageForFeed(string feedname, string afterTimestamp, string afterId, string afterChangeNumber)
{
return GetResponseContentFromRPDEPage(
return ResponseContent.RpdeResponse(
(await RouteOpenDataRPDEPageForFeed(
feedname,
RpdeOrderingStrategyRouter.ConvertStringToLongOrThrow(afterTimestamp, nameof(afterTimestamp)),
afterId,
RpdeOrderingStrategyRouter.ConvertStringToLongOrThrow(afterChangeNumber, nameof(afterChangeNumber))
)));
)).ToString());
}


/// <summary>
/// Handler for an RPDE endpoint
/// Designed to be used on a single controller method with a "feedname" parameter,
Expand All @@ -190,14 +191,11 @@ public async Task<ResponseContent> GetOpenDataRPDEPageForFeed(string feedname, s
/// <returns></returns>
public async Task<ResponseContent> GetOpenDataRPDEPageForFeed(string feedname, long? afterTimestamp, string afterId, long? afterChangeNumber)
{
return GetResponseContentFromRPDEPage(await RouteOpenDataRPDEPageForFeed(feedname, afterTimestamp, afterId, afterChangeNumber));
return ResponseContent.RpdeResponse((await RouteOpenDataRPDEPageForFeed(feedname, afterTimestamp, afterId, afterChangeNumber)).ToString());
}

private ResponseContent GetResponseContentFromRPDEPage(RpdePage rpdePage)
{
var cacheMaxAge = rpdePage.Items.Count == 0 ? this.settings.RPDELastPageCacheDuration : this.settings.RPDEPageCacheDuration;
return ResponseContent.RpdeResponse(rpdePage.ToString(), cacheMaxAge);
}



/// <summary>
/// Handler for an RPDE endpoint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using OpenActive.NET;
using System;
using System.Net;

namespace OpenActive.Server.NET.OpenBookingHelper
Expand Down Expand Up @@ -61,14 +60,13 @@ public static ResponseContent OrdersFeedRpdeResponse(string content)
};
}

public static ResponseContent RpdeResponse(string content, TimeSpan cacheControlMaxAge)
public static ResponseContent RpdeResponse(string content)
{
return new ResponseContent
{
Content = content,
ContentType = OpenActiveMediaTypes.RealtimePagedDataExchange.Version1,
StatusCode = HttpStatusCode.OK,
CacheControlMaxAge = cacheControlMaxAge
StatusCode = HttpStatusCode.OK
};
}

Expand All @@ -84,10 +82,6 @@ public static ResponseContent RpdeResponse(string content, TimeSpan cacheControl
// Summary:
// The intended HTTP status code of the response
public HttpStatusCode StatusCode { get; internal set; } = HttpStatusCode.OK;
//
// Summary:
// The Cache-Control header intended for the response (public, max-age=X)
public TimeSpan CacheControlMaxAge { get; internal set; }

public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ public class BookingEngineSettings
public OrdersRPDEFeedGenerator OrdersFeedGenerator { get; set; }
public OrdersRPDEFeedGenerator OrderProposalsFeedGenerator { get; set; }
public SellerStore SellerStore { get; set; }
public bool HasSingleSeller { get; set; } = false;
/// <summary>
/// TTL in the Cache-Control header for all RPDE pages that contain greater than zero items
/// See https://developer.openactive.io/publishing-data/data-feeds/scaling-feeds for CDN configuration instructions
/// </summary>
public TimeSpan RPDEPageCacheDuration { get; set; } = TimeSpan.FromHours(1);
/// <summary>
/// TTL in the Cache-Control header for all RPDE pages that contain zero items
/// See https://developer.openactive.io/publishing-data/data-feeds/scaling-feeds for CDN configuration instructions
/// </summary>
public TimeSpan RPDELastPageCacheDuration { get; set; } = TimeSpan.FromSeconds(8);
public bool HasSingleSeller { get; set; }
}
}

0 comments on commit 8382e22

Please sign in to comment.