You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CacheOutputConfiguration.cs 4.1KB

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