Skip to content

Commit 7492953

Browse files
author
DevTrends
committed
HttpPost support.
Added RemoveItems overload that accepts routeValues. Fix for VarybyCustom key issue.
1 parent 0add367 commit 7492953

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Web.Routing;
1+
using System.Collections.Generic;
2+
using System.Web.Routing;
23

34
namespace DevTrends.MvcDonutCaching
45
{
@@ -7,5 +8,6 @@ public interface IKeyBuilder
78
string BuildKey(string controllerName);
89
string BuildKey(string controllerName, string actionName);
910
string BuildKey(string controllerName, string actionName, RouteValueDictionary routeValues);
11+
string BuildKeyFragment(KeyValuePair<string, object> routeValue);
1012
}
1113
}

DevTrends.MvcDonutCaching/IOutputCacheManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public interface IOutputCacheManager
99
void RemoveItem(string controllerName, string actionName, RouteValueDictionary routeValues);
1010
void RemoveItems();
1111
void RemoveItems(string controllerName);
12-
void RemoveItems(string controllerName, string actionName);
12+
void RemoveItems(string controllerName, string actionName);
13+
void RemoveItems(string controllerName, string actionName, RouteValueDictionary routeValues);
1314
}
1415
}

DevTrends.MvcDonutCaching/KeyBuilder.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System.Collections.Generic;
2+
using System.Text;
23
using System.Web.Routing;
34

45
namespace DevTrends.MvcDonutCaching
@@ -35,11 +36,16 @@ public string BuildKey(string controllerName, string actionName, RouteValueDicti
3536
{
3637
foreach (var routeValue in routeValues)
3738
{
38-
builder.AppendFormat("{0}={1}#", routeValue.Key.ToLowerInvariant(), routeValue.Value.ToString().ToLowerInvariant());
39+
builder.Append(BuildKeyFragment(routeValue));
3940
}
4041
}
4142

4243
return builder.ToString();
4344
}
45+
46+
public string BuildKeyFragment(KeyValuePair<string, object> routeValue)
47+
{
48+
return string.Format("{0}={1}#", routeValue.Key.ToLowerInvariant(), routeValue.Value.ToString().ToLowerInvariant());
49+
}
4450
}
4551
}

DevTrends.MvcDonutCaching/KeyGenerator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public string GenerateKey(ControllerContext context, CacheSettings cacheSettings
3838
{
3939
routeValues.Add(queryStringKey.ToLowerInvariant(), context.HttpContext.Request.QueryString[queryStringKey].ToLowerInvariant());
4040
}
41+
42+
foreach (var formKey in context.HttpContext.Request.Form.AllKeys)
43+
{
44+
routeValues.Add(formKey.ToLowerInvariant(), context.HttpContext.Request.Form[formKey].ToLowerInvariant());
45+
}
4146
}
4247

4348
if (!string.IsNullOrEmpty(cacheSettings.VaryByParam))
@@ -55,7 +60,7 @@ public string GenerateKey(ControllerContext context, CacheSettings cacheSettings
5560

5661
if (!string.IsNullOrEmpty(cacheSettings.VaryByCustom))
5762
{
58-
routeValues.Add("custom", context.HttpContext.ApplicationInstance.GetVaryByCustomString(HttpContext.Current, cacheSettings.VaryByCustom));
63+
routeValues.Add(cacheSettings.VaryByCustom.ToLowerInvariant(), context.HttpContext.ApplicationInstance.GetVaryByCustomString(HttpContext.Current, cacheSettings.VaryByCustom));
5964
}
6065

6166
var key = _keyBuilder.BuildKey(controllerName, actionName, routeValues);

DevTrends.MvcDonutCaching/OutputCacheManager.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void RemoveItem(string controllerName, string actionName, RouteValueDicti
7272
/// </summary>
7373
public void RemoveItems()
7474
{
75-
RemoveItems(null, null);
75+
RemoveItems(null, null, null);
7676
}
7777

7878
/// <summary>
@@ -81,7 +81,7 @@ public void RemoveItems()
8181
/// <param name="controllerName">The name of the controller.</param>
8282
public void RemoveItems(string controllerName)
8383
{
84-
RemoveItems(controllerName, null);
84+
RemoveItems(controllerName, null, null);
8585
}
8686

8787
/// <summary>
@@ -90,6 +90,17 @@ public void RemoveItems(string controllerName)
9090
/// <param name="controllerName">The name of the controller that contains the action method.</param>
9191
/// <param name="actionName">The name of the controller action method.</param>
9292
public void RemoveItems(string controllerName, string actionName)
93+
{
94+
RemoveItems(controllerName, actionName, null);
95+
}
96+
97+
/// <summary>
98+
/// Removes all output cache entries for the specified controller, action and parameters.
99+
/// </summary>
100+
/// <param name="controllerName">The name of the controller that contains the action method.</param>
101+
/// <param name="actionName">The name of the controller action method.</param>
102+
/// <param name="routeValues">A dictionary that contains the parameters for a route.</param>
103+
public void RemoveItems(string controllerName, string actionName, RouteValueDictionary routeValues)
93104
{
94105
var enumerableCache = _outputCacheProvider as IEnumerable<KeyValuePair<string, object>>;
95106

@@ -102,6 +113,15 @@ public void RemoveItems(string controllerName, string actionName)
102113

103114
var keysToDelete = enumerableCache.Where(x => x.Key.StartsWith(key)).Select(x => x.Key);
104115

116+
if (routeValues != null)
117+
{
118+
foreach (var routeValue in routeValues)
119+
{
120+
var value = routeValue;
121+
keysToDelete = keysToDelete.Where(x => x.Contains(_keyBuilder.BuildKeyFragment(value)));
122+
}
123+
}
124+
105125
foreach (var keyToDelete in keysToDelete)
106126
{
107127
_outputCacheProvider.Remove(keyToDelete);

0 commit comments

Comments
 (0)