Skip to content

Commit 5aaed84

Browse files
committed
Added the NoCacheLookupForPosts option.
This allows the developer to never use the previously cached value if the incomming request is a POST. We are still caching the result for subsequent requests (that are not POSTs). This is handy when a POST to an action is generating a lot of computation and further GET requests would generate the same result.
1 parent 17e8871 commit 5aaed84

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

DevTrends.MvcDonutCaching/DonutOutputCacheAttribute.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,17 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
126126
// Are we actually storing data on the server side ?
127127
if (_cacheSettings.IsServerCachingEnabled)
128128
{
129-
var cachedItem = _outputCacheManager.GetItem(cacheKey);
129+
CacheItem cachedItem = null;
130+
131+
// If the request is a POST, we lookup for NoCacheLookupForPosts option
132+
// We are fetching the stored value only if the option has not been set and the request is not a POST
133+
if (
134+
(_cacheSettings.Options & OutputCacheOptions.NoCacheLookupForPosts) != OutputCacheOptions.NoCacheLookupForPosts ||
135+
filterContext.HttpContext.Request.HttpMethod != "POST"
136+
)
137+
{
138+
cachedItem = _outputCacheManager.GetItem(cacheKey);
139+
}
130140

131141
// We have a cached version on the server side
132142
if (cachedItem != null)

DevTrends.MvcDonutCaching/OutputCacheOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ public enum OutputCacheOptions
1414
/// No matter what, never use the Post data to generate the cache key name
1515
/// </summary>
1616
IgnoreFormData = 0x2,
17+
/// <summary>
18+
/// If the request is a POST, don't lookup for a cached result, execute the the result normally,
19+
/// caching it for subsequent GET (or any other non POST verb).
20+
/// </summary>
21+
NoCacheLookupForPosts = 0x4,
1722
}
1823
}

0 commit comments

Comments
 (0)