選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DefaultCacheKeyGenerator.cs 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Net.Http.Headers;
  6. using System.Web.Http.Controllers;
  7. using iiie.CacheControl.Business.HttpExtensions;
  8. using iiie.CacheControl.Business.OutputCache;
  9. namespace iiie.CacheControl.Business.CacheKey
  10. {
  11. public class DefaultCacheKeyGenerator : ICacheKeyGenerator
  12. {
  13. public virtual string MakeCacheKey(HttpActionContext context, MediaTypeHeaderValue mediaType, OutputCacheType cacheType, bool excludeQueryString = false)
  14. {
  15. var controller = context.ControllerContext.ControllerDescriptor.ControllerName;
  16. var action = context.ActionDescriptor.ActionName;
  17. var key = context.Request.GetConfiguration().CacheOutputConfiguration(cacheType).MakeBaseCachekey(controller, action);
  18. var actionParameters = context.ActionArguments.Where(x => x.Value != null).Select(x => x.Key + "=" + GetValue(x.Value));
  19. string parameters;
  20. if (!excludeQueryString)
  21. {
  22. var queryStringParameters =
  23. context.Request.GetQueryNameValuePairs()
  24. .Where(x => x.Key.ToLower() != "callback" && x.Key.ToLower() != "_")
  25. .Select(x => x.Key + "=" + x.Value);
  26. var parametersCollections = actionParameters.Union(queryStringParameters);
  27. parameters = "-" + string.Join("&", parametersCollections);
  28. var callbackValue = GetJsonpCallback(context.Request);
  29. if (!string.IsNullOrWhiteSpace(callbackValue))
  30. {
  31. var callback = "callback=" + callbackValue;
  32. if (parameters.Contains("&" + callback)) parameters = parameters.Replace("&" + callback, string.Empty);
  33. if (parameters.Contains(callback + "&")) parameters = parameters.Replace(callback + "&", string.Empty);
  34. if (parameters.Contains("-" + callback)) parameters = parameters.Replace("-" + callback, string.Empty);
  35. if (parameters.EndsWith("&")) parameters = parameters.TrimEnd('&');
  36. }
  37. }
  38. else
  39. {
  40. parameters = "-" + string.Join("&", actionParameters);
  41. }
  42. if (parameters == "-") parameters = string.Empty;
  43. var cachekey = string.Format("{0}{1}:{2}", key, parameters, mediaType.MediaType);
  44. return cachekey;
  45. }
  46. private string GetJsonpCallback(HttpRequestMessage request)
  47. {
  48. var callback = string.Empty;
  49. if (request.Method == HttpMethod.Get)
  50. {
  51. var query = request.GetQueryNameValuePairs();
  52. if (query != null)
  53. {
  54. var queryVal = query.FirstOrDefault(x => x.Key.ToLower() == "callback");
  55. if (!queryVal.Equals(default(KeyValuePair<string, string>))) callback = queryVal.Value;
  56. }
  57. }
  58. return callback;
  59. }
  60. private string GetValue(object val)
  61. {
  62. if (val is IEnumerable && !(val is string))
  63. {
  64. var concatValue = string.Empty;
  65. var paramArray = val as IEnumerable;
  66. return paramArray.Cast<object>().Aggregate(concatValue, (current, paramValue) => current + (paramValue + ";"));
  67. }
  68. return val.ToString();
  69. }
  70. }
  71. }