Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/WebApi.OutputCache.Core/Time/CacheTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class CacheTime
// client cache length in seconds
public TimeSpan ClientTimeSpan { get; set; }

public TimeSpan? SharedTimeSpan { get; set; }

public DateTimeOffset AbsoluteExpiration { get; set; }
}
}
11 changes: 9 additions & 2 deletions src/WebApi.OutputCache.Core/Time/ShortTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ public class ShortTime : IModelQuery<DateTime, CacheTime>
{
private readonly int serverTimeInSeconds;
private readonly int clientTimeInSeconds;
private readonly int? sharedTimeInSecounds;

public ShortTime(int serverTimeInSeconds, int clientTimeInSeconds)
public ShortTime(int serverTimeInSeconds, int clientTimeInSeconds, int? sharedTimeInSecounds)
{
if (serverTimeInSeconds < 0)
serverTimeInSeconds = 0;
Expand All @@ -18,14 +19,20 @@ public ShortTime(int serverTimeInSeconds, int clientTimeInSeconds)
clientTimeInSeconds = 0;

this.clientTimeInSeconds = clientTimeInSeconds;

if (sharedTimeInSecounds.HasValue && sharedTimeInSecounds.Value < 0)
sharedTimeInSecounds = 0;

this.sharedTimeInSecounds = sharedTimeInSecounds;
}

public CacheTime Execute(DateTime model)
{
var cacheTime = new CacheTime
{
AbsoluteExpiration = model.AddSeconds(serverTimeInSeconds),
ClientTimeSpan = TimeSpan.FromSeconds(clientTimeInSeconds)
ClientTimeSpan = TimeSpan.FromSeconds(clientTimeInSeconds),
SharedTimeSpan = sharedTimeInSecounds.HasValue ? (TimeSpan?) TimeSpan.FromSeconds(sharedTimeInSecounds.Value) : null
};

return cacheTime;
Expand Down
21 changes: 20 additions & 1 deletion src/WebApi.OutputCache.V2/CacheOutputAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -48,6 +49,23 @@ public class CacheOutputAttribute : ActionFilterAttribute
/// </summary>
public int ClientTimeSpan { get; set; }


private int? _sharedTimeSpan = null;

/// <summary>
/// Corresponds to CacheControl Shared MaxAge HTTP header (in seconds)
/// </summary>
public int SharedTimeSpan
{
get // required for property visibility
{
if (!_sharedTimeSpan.HasValue)
throw new Exception("should not be called without value set");
return _sharedTimeSpan.Value;
}
set { _sharedTimeSpan = value; }
}

/// <summary>
/// Corresponds to CacheControl NoCache HTTP header
/// </summary>
Expand Down Expand Up @@ -98,7 +116,7 @@ protected virtual void EnsureCacheTimeQuery()

protected void ResetCacheTimeQuery()
{
CacheTimeQuery = new ShortTime( ServerTimeSpan, ClientTimeSpan );
CacheTimeQuery = new ShortTime( ServerTimeSpan, ClientTimeSpan, _sharedTimeSpan);
}

protected virtual MediaTypeHeaderValue GetExpectedMediaType(HttpConfiguration config, HttpActionContext actionContext)
Expand Down Expand Up @@ -247,6 +265,7 @@ protected virtual void ApplyCacheHeaders(HttpResponseMessage response, CacheTime
var cachecontrol = new CacheControlHeaderValue
{
MaxAge = cacheTime.ClientTimeSpan,
SharedMaxAge = cacheTime.SharedTimeSpan,
MustRevalidate = MustRevalidate,
Private = Private
};
Expand Down
16 changes: 16 additions & 0 deletions test/WebApi.OutputCache.V2.Tests/ClientSideTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ public void private_true_headers_correct()
Assert.IsTrue(result.Headers.CacheControl.Private);
}

[Test]
public void shared_max_age_header_correct()
{
var client = new HttpClient(_server);
var result = client.GetAsync(_url + "Get_c100_s100_sm200").Result;
Assert.AreEqual(result.Headers.CacheControl.SharedMaxAge,TimeSpan.FromSeconds(200));
}

[Test]
public void shared_max_age_header_not_present()
{
var client = new HttpClient(_server);
var result = client.GetAsync(_url + "Get_c100_s100").Result;
Assert.AreEqual(result.Headers.CacheControl.SharedMaxAge, null);
}

[TestFixtureTearDown]
public void fixture_dispose()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,12 @@ public IHttpActionResult Get_ihttpactionresult()
{
return Ok("value");
}

[CacheOutput(ClientTimeSpan = 100, ServerTimeSpan = 100, SharedTimeSpan = 200)]
public string Get_c100_s100_sm200()
{
return "test";
}

}
}