Cache is disabled for json responses.

pull/4/head
Keivan Beigi 11 years ago
parent b029195260
commit 642207c68d

@ -0,0 +1,16 @@
using Nancy;
namespace NzbDrone.Api.Extensions
{
public static class CacheHeaderPipeline
{
public static void Handle(NancyContext context)
{
if (context.Response.ContentType.Contains("json"))
{
context.Response.Headers.DisableCache();
}
}
}
}

@ -32,24 +32,27 @@ namespace NzbDrone.Api.Extensions
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK)
{
return new JsonResponse<TModel>(model, NancySerializer) { StatusCode = statusCode };
var response = new JsonResponse<TModel>(model, NancySerializer) { StatusCode = statusCode };
response.Headers.DisableCache();
return response;
}
public static IDictionary<string, string> DisableCache(this IDictionary<string, string> headers)
{
headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.Add("Pragma", "no-cache");
headers.Add("Expires", "0");
headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
headers["Pragma"] = "no-cache";
headers["Expires"] = "0";
return headers;
}
public static IDictionary<string, string> EnableCache(this IDictionary<string, string> headers)
{
headers.Add("Cache-Control", "max-age=31536000 , public");
headers.Add("Expires", "Sat, 29 Jun 2020 00:00:00 GMT");
headers.Add("Last-Modified", "Sat, 29 Jun 2000 00:00:00 GMT");
headers.Add("Age", "193266");
headers["Cache-Control"] = "max-age=31536000 , public";
headers["Expires"] = "Sat, 29 Jun 2020 00:00:00 GMT";
headers["Last-Modified"] = "Sat, 29 Jun 2000 00:00:00 GMT";
headers["Age"] = "193266";
return headers;
}

@ -33,6 +33,7 @@ namespace NzbDrone.Api
container.Resolve<IMessageAggregator>().PublishEvent(new ApplicationStartedEvent());
pipelines.AfterRequest.AddItemToStartOfPipeline(GzipCompressionPipeline.Handle);
pipelines.AfterRequest.AddItemToEndOfPipeline(CacheHeaderPipeline.Handle);
ApplicationPipelines.OnError.AddItemToEndOfPipeline(container.Resolve<NzbDroneErrorPipeline>().HandleException);
}

@ -106,6 +106,7 @@
<Compile Include="Directories\DirectoryModule.cs" />
<Compile Include="Episodes\EpisodeModule.cs" />
<Compile Include="Episodes\EpisodeResource.cs" />
<Compile Include="Extensions\CacheHeaderPipeline.cs" />
<Compile Include="Extensions\GZipPipeline.cs" />
<Compile Include="Extensions\NancyJsonSerializer.cs" />
<Compile Include="Frontend\IAddCacheHeaders.cs" />

@ -5,6 +5,7 @@ using NLog;
using NzbDrone.Api.REST;
using NzbDrone.Common.Serializer;
using RestSharp;
using System.Linq;
namespace NzbDrone.Integration.Test.Client
{
@ -99,6 +100,8 @@ namespace NzbDrone.Integration.Test.Client
throw response.ErrorException;
}
AssertDisableCache(response.Headers);
response.ErrorMessage.Should().BeBlank();
response.StatusCode.Should().Be(statusCode);
@ -106,5 +109,13 @@ namespace NzbDrone.Integration.Test.Client
return Json.Deserialize<T>(response.Content);
}
private static void AssertDisableCache(IList<Parameter> headers)
{
headers.Single(c => c.Name == "Cache-Control").Value.Should().Be("no-cache, no-store, must-revalidate");
headers.Single(c => c.Name == "Pragma").Value.Should().Be("no-cache");
headers.Single(c => c.Name == "Expires").Value.Should().Be("0");
}
}
}
Loading…
Cancel
Save