1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
- using System . Collections . Concurrent ;
5
4
using System . Collections . Generic ;
6
5
using System . Linq ;
7
- using Microsoft . AspNetCore . Mvc . Abstractions ;
8
6
using Microsoft . AspNetCore . Mvc . Controllers ;
9
7
using Microsoft . AspNetCore . Mvc . Filters ;
10
- using Microsoft . AspNetCore . Mvc . Infrastructure ;
11
8
using Microsoft . AspNetCore . Mvc . ModelBinding ;
12
9
using Microsoft . Extensions . Internal ;
13
10
using Microsoft . Extensions . Options ;
@@ -16,25 +13,21 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure
16
13
{
17
14
internal class ControllerActionInvokerCache
18
15
{
19
- private readonly IActionDescriptorCollectionProvider _collectionProvider ;
20
16
private readonly ParameterBinder _parameterBinder ;
21
17
private readonly IModelBinderFactory _modelBinderFactory ;
22
18
private readonly IModelMetadataProvider _modelMetadataProvider ;
23
19
private readonly IFilterProvider [ ] _filterProviders ;
24
20
private readonly IControllerFactoryProvider _controllerFactoryProvider ;
25
21
private readonly MvcOptions _mvcOptions ;
26
- private volatile InnerCache _currentCache ;
27
22
28
23
public ControllerActionInvokerCache (
29
- IActionDescriptorCollectionProvider collectionProvider ,
30
24
ParameterBinder parameterBinder ,
31
25
IModelBinderFactory modelBinderFactory ,
32
26
IModelMetadataProvider modelMetadataProvider ,
33
27
IEnumerable < IFilterProvider > filterProviders ,
34
28
IControllerFactoryProvider factoryProvider ,
35
29
IOptions < MvcOptions > mvcOptions )
36
30
{
37
- _collectionProvider = collectionProvider ;
38
31
_parameterBinder = parameterBinder ;
39
32
_modelBinderFactory = modelBinderFactory ;
40
33
_modelMetadataProvider = modelMetadataProvider ;
@@ -43,30 +36,16 @@ public ControllerActionInvokerCache(
43
36
_mvcOptions = mvcOptions . Value ;
44
37
}
45
38
46
- private InnerCache CurrentCache
47
- {
48
- get
49
- {
50
- var current = _currentCache ;
51
- var actionDescriptors = _collectionProvider . ActionDescriptors ;
52
-
53
- if ( current == null || current . Version != actionDescriptors . Version )
54
- {
55
- current = new InnerCache ( actionDescriptors . Version ) ;
56
- _currentCache = current ;
57
- }
58
-
59
- return current ;
60
- }
61
- }
62
-
63
39
public ( ControllerActionInvokerCacheEntry cacheEntry , IFilterMetadata [ ] filters ) GetCachedResult ( ControllerContext controllerContext )
64
40
{
65
- var cache = CurrentCache ;
66
41
var actionDescriptor = controllerContext . ActionDescriptor ;
67
42
68
43
IFilterMetadata [ ] filters ;
69
- if ( ! cache . Entries . TryGetValue ( actionDescriptor , out var cacheEntry ) )
44
+
45
+ var cacheEntry = actionDescriptor . CacheEntry ;
46
+
47
+ // We don't care about thread safety here
48
+ if ( cacheEntry is null )
70
49
{
71
50
var filterFactoryResult = FilterFactory . GetAllFilters ( _filterProviders , controllerContext ) ;
72
51
filters = filterFactoryResult . Filters ;
@@ -97,7 +76,8 @@ private InnerCache CurrentCache
97
76
propertyBinderFactory ,
98
77
objectMethodExecutor ,
99
78
actionMethodExecutor ) ;
100
- cacheEntry = cache . Entries . GetOrAdd ( actionDescriptor , cacheEntry ) ;
79
+
80
+ actionDescriptor . CacheEntry = cacheEntry ;
101
81
}
102
82
else
103
83
{
@@ -107,18 +87,5 @@ private InnerCache CurrentCache
107
87
108
88
return ( cacheEntry , filters ) ;
109
89
}
110
-
111
- private class InnerCache
112
- {
113
- public InnerCache ( int version )
114
- {
115
- Version = version ;
116
- }
117
-
118
- public ConcurrentDictionary < ActionDescriptor , ControllerActionInvokerCacheEntry > Entries { get ; } =
119
- new ConcurrentDictionary < ActionDescriptor , ControllerActionInvokerCacheEntry > ( ) ;
120
-
121
- public int Version { get ; }
122
- }
123
90
}
124
91
}
0 commit comments