Skip to content

Commit 5100c53

Browse files
author
dmalanij
committed
Refactor CacheKey Generation
Extracted logic from the MakeCacheKey method into different methods to improve extensibility
1 parent 37bc932 commit 5100c53

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/WebApi.OutputCache.V2/DefaultCacheKeyGenerator.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
using System.Collections;
22
using System.Collections.Generic;
3-
using System.Globalization;
43
using System.Linq;
54
using System.Net.Http;
65
using System.Net.Http.Headers;
7-
using System.Text;
86
using System.Web.Http.Controllers;
97

108
namespace WebApi.OutputCache.V2
119
{
1210
public class DefaultCacheKeyGenerator : ICacheKeyGenerator
1311
{
1412
public virtual string MakeCacheKey(HttpActionContext context, MediaTypeHeaderValue mediaType, bool excludeQueryString = false)
13+
{
14+
var key = MakeBaseKey(context);
15+
var parameters = FormatParameters(context, excludeQueryString);
16+
17+
return string.Format("{0}{1}:{2}", key, parameters, mediaType);
18+
}
19+
20+
protected virtual string MakeBaseKey(HttpActionContext context)
1521
{
1622
var controller = context.ControllerContext.ControllerDescriptor.ControllerType.FullName;
1723
var action = context.ActionDescriptor.ActionName;
18-
var key = context.Request.GetConfiguration().CacheOutputConfiguration().MakeBaseCachekey(controller, action);
24+
return context.Request.GetConfiguration().CacheOutputConfiguration().MakeBaseCachekey(controller, action);
25+
}
26+
27+
protected virtual string FormatParameters(HttpActionContext context, bool excludeQueryString)
28+
{
1929
var actionParameters = context.ActionArguments.Where(x => x.Value != null).Select(x => x.Key + "=" + GetValue(x.Value));
2030

2131
string parameters;
@@ -45,9 +55,7 @@ public virtual string MakeCacheKey(HttpActionContext context, MediaTypeHeaderVal
4555
}
4656

4757
if (parameters == "-") parameters = string.Empty;
48-
49-
var cachekey = string.Format("{0}{1}:{2}", key, parameters, mediaType);
50-
return cachekey;
58+
return parameters;
5159
}
5260

5361
private string GetJsonpCallback(HttpRequestMessage request)

0 commit comments

Comments
 (0)