Skip to content

Commit 7ea8632

Browse files
authored
Merge pull request filipw#187 from bobend/shared-max-age
Shared Max Age support added.
2 parents 1129abc + 724d873 commit 7ea8632

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

src/WebApi.OutputCache.Core/Time/CacheTime.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public class CacheTime
77
// client cache length in seconds
88
public TimeSpan ClientTimeSpan { get; set; }
99

10+
public TimeSpan? SharedTimeSpan { get; set; }
11+
1012
public DateTimeOffset AbsoluteExpiration { get; set; }
1113
}
1214
}

src/WebApi.OutputCache.Core/Time/ShortTime.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ public class ShortTime : IModelQuery<DateTime, CacheTime>
66
{
77
private readonly int serverTimeInSeconds;
88
private readonly int clientTimeInSeconds;
9+
private readonly int? sharedTimeInSecounds;
910

10-
public ShortTime(int serverTimeInSeconds, int clientTimeInSeconds)
11+
public ShortTime(int serverTimeInSeconds, int clientTimeInSeconds, int? sharedTimeInSecounds)
1112
{
1213
if (serverTimeInSeconds < 0)
1314
serverTimeInSeconds = 0;
@@ -18,14 +19,20 @@ public ShortTime(int serverTimeInSeconds, int clientTimeInSeconds)
1819
clientTimeInSeconds = 0;
1920

2021
this.clientTimeInSeconds = clientTimeInSeconds;
22+
23+
if (sharedTimeInSecounds.HasValue && sharedTimeInSecounds.Value < 0)
24+
sharedTimeInSecounds = 0;
25+
26+
this.sharedTimeInSecounds = sharedTimeInSecounds;
2127
}
2228

2329
public CacheTime Execute(DateTime model)
2430
{
2531
var cacheTime = new CacheTime
2632
{
2733
AbsoluteExpiration = model.AddSeconds(serverTimeInSeconds),
28-
ClientTimeSpan = TimeSpan.FromSeconds(clientTimeInSeconds)
34+
ClientTimeSpan = TimeSpan.FromSeconds(clientTimeInSeconds),
35+
SharedTimeSpan = sharedTimeInSecounds.HasValue ? (TimeSpan?) TimeSpan.FromSeconds(sharedTimeInSecounds.Value) : null
2936
};
3037

3138
return cacheTime;

src/WebApi.OutputCache.V2/CacheOutputAttribute.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Net.Http.Formatting;
66
using System.Net.Http.Headers;
77
using System.Runtime.ExceptionServices;
8+
using System.Runtime.InteropServices;
89
using System.Text;
910
using System.Threading;
1011
using System.Threading.Tasks;
@@ -48,6 +49,23 @@ public class CacheOutputAttribute : ActionFilterAttribute
4849
/// </summary>
4950
public int ClientTimeSpan { get; set; }
5051

52+
53+
private int? _sharedTimeSpan = null;
54+
55+
/// <summary>
56+
/// Corresponds to CacheControl Shared MaxAge HTTP header (in seconds)
57+
/// </summary>
58+
public int SharedTimeSpan
59+
{
60+
get // required for property visibility
61+
{
62+
if (!_sharedTimeSpan.HasValue)
63+
throw new Exception("should not be called without value set");
64+
return _sharedTimeSpan.Value;
65+
}
66+
set { _sharedTimeSpan = value; }
67+
}
68+
5169
/// <summary>
5270
/// Corresponds to CacheControl NoCache HTTP header
5371
/// </summary>
@@ -98,7 +116,7 @@ protected virtual void EnsureCacheTimeQuery()
98116

99117
protected void ResetCacheTimeQuery()
100118
{
101-
CacheTimeQuery = new ShortTime( ServerTimeSpan, ClientTimeSpan );
119+
CacheTimeQuery = new ShortTime( ServerTimeSpan, ClientTimeSpan, _sharedTimeSpan);
102120
}
103121

104122
protected virtual MediaTypeHeaderValue GetExpectedMediaType(HttpConfiguration config, HttpActionContext actionContext)
@@ -247,6 +265,7 @@ protected virtual void ApplyCacheHeaders(HttpResponseMessage response, CacheTime
247265
var cachecontrol = new CacheControlHeaderValue
248266
{
249267
MaxAge = cacheTime.ClientTimeSpan,
268+
SharedMaxAge = cacheTime.SharedTimeSpan,
250269
MustRevalidate = MustRevalidate,
251270
Private = Private
252271
};

test/WebApi.OutputCache.V2.Tests/ClientSideTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,22 @@ public void private_true_headers_correct()
178178
Assert.IsTrue(result.Headers.CacheControl.Private);
179179
}
180180

181+
[Test]
182+
public void shared_max_age_header_correct()
183+
{
184+
var client = new HttpClient(_server);
185+
var result = client.GetAsync(_url + "Get_c100_s100_sm200").Result;
186+
Assert.AreEqual(result.Headers.CacheControl.SharedMaxAge,TimeSpan.FromSeconds(200));
187+
}
188+
189+
[Test]
190+
public void shared_max_age_header_not_present()
191+
{
192+
var client = new HttpClient(_server);
193+
var result = client.GetAsync(_url + "Get_c100_s100").Result;
194+
Assert.AreEqual(result.Headers.CacheControl.SharedMaxAge, null);
195+
}
196+
181197
[TestFixtureTearDown]
182198
public void fixture_dispose()
183199
{

test/WebApi.OutputCache.V2.Tests/TestControllers/SampleController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,12 @@ public IHttpActionResult Get_ihttpactionresult()
152152
{
153153
return Ok("value");
154154
}
155+
156+
[CacheOutput(ClientTimeSpan = 100, ServerTimeSpan = 100, SharedTimeSpan = 200)]
157+
public string Get_c100_s100_sm200()
158+
{
159+
return "test";
160+
}
161+
155162
}
156163
}

0 commit comments

Comments
 (0)