您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CacheOutputConfiguration.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Net.Http;
  5. using System.Reflection;
  6. using System.Web.Http;
  7. using iiie.CacheControl.Business.CacheKey;
  8. using iiie.CacheControl.Business.OutputCache;
  9. namespace iiie.CacheControl.Business.HttpExtensions
  10. {
  11. public class CacheOutputConfiguration
  12. {
  13. private readonly HttpConfiguration _configuration;
  14. private OutputCacheType _cacheType;
  15. public CacheOutputConfiguration(HttpConfiguration configuration, OutputCacheType type)
  16. {
  17. _configuration = configuration;
  18. _cacheType = type;
  19. }
  20. public void RegisterCacheOutputProvider(Func<IOutputCache> provider)
  21. {
  22. _configuration.Properties.GetOrAdd(typeof(IOutputCache), x => provider);
  23. }
  24. public void RegisterCacheKeyGeneratorProvider<T>(Func<T> provider)
  25. where T : ICacheKeyGenerator
  26. {
  27. _configuration.Properties.GetOrAdd(typeof(T), x => provider);
  28. }
  29. public void RegisterDefaultCacheKeyGeneratorProvider(Func<ICacheKeyGenerator> provider)
  30. {
  31. RegisterCacheKeyGeneratorProvider(provider);
  32. }
  33. public string MakeBaseCachekey(string controller, string action)
  34. {
  35. return string.Format("{0}-{1}", controller.ToLower(), action.ToLower());
  36. }
  37. public string MakeBaseCachekey<T, U>(Expression<Func<T, U>> expression)
  38. {
  39. var method = expression.Body as MethodCallExpression;
  40. if (method == null) throw new ArgumentException("Expression is wrong");
  41. var methodName = method.Method.Name;
  42. var nameAttribs = method.Method.GetCustomAttributes(typeof(ActionNameAttribute), false);
  43. if (nameAttribs.Any())
  44. {
  45. var actionNameAttrib = (ActionNameAttribute)nameAttribs.FirstOrDefault();
  46. if (actionNameAttrib != null)
  47. {
  48. methodName = actionNameAttrib.Name;
  49. }
  50. }
  51. return string.Format("{0}-{1}", typeof(T).Name.Replace("Controller", string.Empty).ToLower(), methodName.ToLower());
  52. }
  53. private static ICacheKeyGenerator TryActivateCacheKeyGenerator(Type generatorType)
  54. {
  55. var hasEmptyOrDefaultConstructor =
  56. generatorType.GetConstructor(Type.EmptyTypes) != null ||
  57. generatorType.GetConstructors(BindingFlags.Instance | BindingFlags.Public)
  58. .Any(x => x.GetParameters().All(p => p.IsOptional));
  59. return hasEmptyOrDefaultConstructor
  60. ? Activator.CreateInstance(generatorType) as ICacheKeyGenerator
  61. : null;
  62. }
  63. public ICacheKeyGenerator GetCacheKeyGenerator(HttpRequestMessage request, Type generatorType)
  64. {
  65. generatorType = generatorType ?? typeof(ICacheKeyGenerator);
  66. object cache;
  67. _configuration.Properties.TryGetValue(generatorType, out cache);
  68. var cacheFunc = cache as Func<ICacheKeyGenerator>;
  69. var generator = cacheFunc != null
  70. ? cacheFunc()
  71. : request.GetDependencyScope().GetService(generatorType) as ICacheKeyGenerator;
  72. return generator
  73. ?? TryActivateCacheKeyGenerator(generatorType)
  74. ?? new DefaultCacheKeyGenerator();
  75. }
  76. public IOutputCache GetCacheOutputProvider(HttpRequestMessage request)
  77. {
  78. object cache;
  79. _configuration.Properties.TryGetValue(typeof(IOutputCache), out cache);
  80. var cacheFunc = cache as Func<IOutputCache>;
  81. var cacheOutputProvider = cacheFunc != null ? cacheFunc() : request.GetDependencyScope().GetService(typeof(IOutputCache)) as IOutputCache;
  82. if (cacheOutputProvider == null)
  83. {
  84. if (_cacheType == OutputCacheType.File)
  85. cacheOutputProvider = new FileOuputCache();
  86. else
  87. cacheOutputProvider = new MemoryOutputCache();
  88. }
  89. return cacheOutputProvider;
  90. }
  91. }
  92. }