Skip to content

Commit 704bb01

Browse files
pstarkovmoonpyk
authored andcommitted
Initial support for VaryByHeader (ref: #6)
1 parent 4ad209c commit 704bb01

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

DevTrends.MvcDonutCaching/CacheSettings.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public class CacheSettings
2828
/// </value>
2929
public string VaryByParam { get; set; }
3030

31+
/// <summary>
32+
/// Gets or sets the VaryByHeader cache parameter.
33+
/// </summary>
34+
/// <value>
35+
/// The VaryByHeader cache parameter.
36+
/// </value>
37+
public string VaryByHeader { get; set; }
38+
3139
/// <summary>
3240
/// Gets or sets the VaryByCustom cache parameter.
3341
/// </summary>

DevTrends.MvcDonutCaching/DonutOutputCacheAttribute.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ public string VaryByParam
6868
set;
6969
}
7070

71+
/// <summary>
72+
/// Gets or sets the vary-by-header value.
73+
/// </summary>
74+
public string VaryByHeader
75+
{
76+
get;
77+
set;
78+
}
79+
7180
/// <summary>
7281
/// Gets or sets the vary-by-custom value.
7382
/// </summary>
@@ -262,12 +271,13 @@ protected CacheSettings BuildCacheSettings()
262271
cacheSettings = new CacheSettings
263272
{
264273
IsCachingEnabled = CacheSettingsManager.IsCachingEnabledGlobally,
265-
Duration = Duration,
266-
VaryByCustom = VaryByCustom,
267-
VaryByParam = VaryByParam,
268-
Location = (int)Location == -1 ? OutputCacheLocation.Server : Location,
269-
NoStore = NoStore,
270-
Options = Options,
274+
Duration = Duration,
275+
VaryByCustom = VaryByCustom,
276+
VaryByParam = VaryByParam,
277+
VaryByHeader = VaryByHeader,
278+
Location = (int)Location == -1 ? OutputCacheLocation.Server : Location,
279+
NoStore = NoStore,
280+
Options = Options,
271281
};
272282
}
273283
else
@@ -277,12 +287,13 @@ protected CacheSettings BuildCacheSettings()
277287
cacheSettings = new CacheSettings
278288
{
279289
IsCachingEnabled = CacheSettingsManager.IsCachingEnabledGlobally && cacheProfile.Enabled,
280-
Duration = Duration == -1 ? cacheProfile.Duration : Duration,
281-
VaryByCustom = VaryByCustom ?? cacheProfile.VaryByCustom,
282-
VaryByParam = VaryByParam ?? cacheProfile.VaryByParam,
283-
Location = (int)Location == -1 ? ((int)cacheProfile.Location == -1 ? OutputCacheLocation.Server : cacheProfile.Location) : Location,
284-
NoStore = _noStore.HasValue ? _noStore.Value : cacheProfile.NoStore,
285-
Options = Options,
290+
Duration = Duration == -1 ? cacheProfile.Duration : Duration,
291+
VaryByCustom = VaryByCustom ?? cacheProfile.VaryByCustom,
292+
VaryByParam = VaryByParam ?? cacheProfile.VaryByParam,
293+
VaryByHeader = VaryByHeader ?? cacheProfile.VaryByHeader,
294+
Location = (int)Location == -1 ? ((int)cacheProfile.Location == -1 ? OutputCacheLocation.Server : cacheProfile.Location) : Location,
295+
NoStore = _noStore.HasValue ? _noStore.Value : cacheProfile.NoStore,
296+
Options = Options,
286297
};
287298
}
288299

DevTrends.MvcDonutCaching/KeyGenerator.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public string GenerateKey(ControllerContext context, CacheSettings cacheSettings
8383
if (!context.IsChildAction)
8484
{
8585
// note that route values take priority over form values and form values take priority over query string values
86-
8786
if ((cacheSettings.Options & OutputCacheOptions.IgnoreFormData) != OutputCacheOptions.IgnoreFormData)
8887
{
8988
foreach (var formKey in context.HttpContext.Request.Form.AllKeys)
@@ -107,7 +106,7 @@ public string GenerateKey(ControllerContext context, CacheSettings cacheSettings
107106
{
108107
foreach (var queryStringKey in context.HttpContext.Request.QueryString.AllKeys)
109108
{
110-
// queryStringKey is null if url has qs name without value. e.g. test.com?q
109+
// queryStringKey is null if url has as name without value. e.g. test.com?q
111110
if (queryStringKey == null || routeValues.ContainsKey(queryStringKey.ToLowerInvariant()))
112111
{
113112
continue;
@@ -144,6 +143,16 @@ public string GenerateKey(ControllerContext context, CacheSettings cacheSettings
144143
context.HttpContext.ApplicationInstance.GetVaryByCustomString(HttpContext.Current, cacheSettings.VaryByCustom);
145144
}
146145

146+
if (!string.IsNullOrEmpty(cacheSettings.VaryByHeader))
147+
{
148+
var headers = cacheSettings.VaryByHeader.ToLowerInvariant().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
149+
var existingHeaders = context.HttpContext.Request.Headers.AllKeys.Where(x => headers.Contains(x.ToLowerInvariant()));
150+
foreach (var header in existingHeaders)
151+
{
152+
routeValues[header] = context.HttpContext.Request.Headers[header];
153+
}
154+
}
155+
147156
var key = _keyBuilder.BuildKey(controllerName, actionName, routeValues);
148157

149158
return key;

0 commit comments

Comments
 (0)