-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathCacheOutputConfiguration.cs
99 lines (82 loc) · 3.66 KB
/
CacheOutputConfiguration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;
using WebApi.OutputCache.Core.Cache;
namespace WebApi.OutputCache.V2
{
public class CacheOutputConfiguration
{
private readonly HttpConfiguration _configuration;
public CacheOutputConfiguration(HttpConfiguration configuration)
{
_configuration = configuration;
}
public void RegisterCacheOutputProvider(Func<IApiOutputCache> provider)
{
_configuration.Properties.GetOrAdd(typeof(IApiOutputCache), x => provider);
}
public void RegisterCacheKeyGeneratorProvider<T>(Func<T> provider)
where T: ICacheKeyGenerator
{
_configuration.Properties.GetOrAdd(typeof (T), x => provider);
}
public void RegisterDefaultCacheKeyGeneratorProvider(Func<ICacheKeyGenerator> provider)
{
RegisterCacheKeyGeneratorProvider(provider);
}
public string MakeBaseCachekey(string controller, string action)
{
return string.Format("{0}-{1}", controller.ToLower(), action.ToLower());
}
public string MakeBaseCachekey<T, U>(Expression<Func<T, U>> expression)
{
var method = expression.Body as MethodCallExpression;
if (method == null) throw new ArgumentException("Expression is wrong");
var methodName = method.Method.Name;
var nameAttribs = method.Method.GetCustomAttributes(typeof(ActionNameAttribute), false);
if (nameAttribs.Any())
{
var actionNameAttrib = (ActionNameAttribute) nameAttribs.FirstOrDefault();
if (actionNameAttrib != null)
{
methodName = actionNameAttrib.Name;
}
}
return string.Format("{0}-{1}", typeof(T).FullName.ToLower(), methodName.ToLower());
}
private static ICacheKeyGenerator TryActivateCacheKeyGenerator(Type generatorType)
{
var hasEmptyOrDefaultConstructor =
generatorType.GetConstructor(Type.EmptyTypes) != null ||
generatorType.GetConstructors(BindingFlags.Instance | BindingFlags.Public)
.Any (x => x.GetParameters().All (p => p.IsOptional));
return hasEmptyOrDefaultConstructor
? Activator.CreateInstance(generatorType) as ICacheKeyGenerator
: null;
}
public ICacheKeyGenerator GetCacheKeyGenerator(HttpRequestMessage request, Type generatorType)
{
generatorType = generatorType ?? typeof (ICacheKeyGenerator);
object cache;
_configuration.Properties.TryGetValue(generatorType, out cache);
var cacheFunc = cache as Func<ICacheKeyGenerator>;
var generator = cacheFunc != null
? cacheFunc()
: request.GetDependencyScope().GetService(generatorType) as ICacheKeyGenerator;
return generator
?? TryActivateCacheKeyGenerator(generatorType)
?? new DefaultCacheKeyGenerator();
}
public IApiOutputCache GetCacheOutputProvider(HttpRequestMessage request)
{
object cache;
_configuration.Properties.TryGetValue(typeof(IApiOutputCache), out cache);
var cacheFunc = cache as Func<IApiOutputCache>;
var cacheOutputProvider = cacheFunc != null ? cacheFunc() : request.GetDependencyScope().GetService(typeof(IApiOutputCache)) as IApiOutputCache ?? new MemoryCacheDefault();
return cacheOutputProvider;
}
}
}