Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit cc549e2

Browse files
committed
Allow feature cache to be updated+invalidated
1 parent dfe2d41 commit cc549e2

12 files changed

+330
-47
lines changed

src/Microsoft.AspNet.Http/Authentication/DefaultAuthenticationManager.cs

+21-10
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,43 @@ namespace Microsoft.AspNet.Http.Authentication.Internal
1414
{
1515
public class DefaultAuthenticationManager : AuthenticationManager, IFeatureCache
1616
{
17-
private readonly IFeatureCollection _features;
17+
private IFeatureCollection _features;
1818
private int _cachedFeaturesRevision = -1;
1919

2020
private IHttpAuthenticationFeature _authentication;
21-
private IHttpResponseFeature _response;
2221

2322
public DefaultAuthenticationManager(IFeatureCollection features)
2423
{
2524
_features = features;
25+
((IFeatureCache)this).SetFeaturesRevision();
2626
}
2727

2828
void IFeatureCache.CheckFeaturesRevision()
2929
{
3030
if (_cachedFeaturesRevision != _features.Revision)
3131
{
32-
_authentication = null;
33-
_response = null;
34-
_cachedFeaturesRevision = _features.Revision;
32+
ResetFeatures();
3533
}
3634
}
3735

36+
void IFeatureCache.SetFeaturesRevision()
37+
{
38+
_cachedFeaturesRevision = _features.Revision;
39+
}
40+
41+
public void UpdateFeatures(IFeatureCollection features)
42+
{
43+
_features = features;
44+
ResetFeatures();
45+
}
46+
47+
private void ResetFeatures()
48+
{
49+
_authentication = null;
50+
51+
((IFeatureCache)this).SetFeaturesRevision();
52+
}
53+
3854
private IHttpAuthenticationFeature HttpAuthenticationFeature
3955
{
4056
get
@@ -47,11 +63,6 @@ private IHttpAuthenticationFeature HttpAuthenticationFeature
4763
}
4864
}
4965

50-
private IHttpResponseFeature HttpResponseFeature
51-
{
52-
get { return FeatureHelpers.GetAndCache(this, _features, ref _response); }
53-
}
54-
5566
public override IEnumerable<AuthenticationDescription> GetAuthenticationSchemes()
5667
{
5768
var handler = HttpAuthenticationFeature.Handler;

src/Microsoft.AspNet.Http/DefaultConnectionInfo.cs

+22-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Http.Internal
1212
{
1313
public class DefaultConnectionInfo : ConnectionInfo, IFeatureCache
1414
{
15-
private readonly IFeatureCollection _features;
15+
private IFeatureCollection _features;
1616
private int _cachedFeaturesRevision = -1;
1717

1818
private IHttpConnectionFeature _connection;
@@ -21,18 +21,36 @@ public class DefaultConnectionInfo : ConnectionInfo, IFeatureCache
2121
public DefaultConnectionInfo(IFeatureCollection features)
2222
{
2323
_features = features;
24+
((IFeatureCache)this).SetFeaturesRevision();
2425
}
2526

2627
void IFeatureCache.CheckFeaturesRevision()
2728
{
2829
if (_cachedFeaturesRevision != _features.Revision)
2930
{
30-
_connection = null;
31-
_tlsConnection = null;
32-
_cachedFeaturesRevision = _features.Revision;
31+
ResetFeatures();
3332
}
3433
}
3534

35+
void IFeatureCache.SetFeaturesRevision()
36+
{
37+
_cachedFeaturesRevision = _features.Revision;
38+
}
39+
40+
public void UpdateFeatures(IFeatureCollection features)
41+
{
42+
_features = features;
43+
ResetFeatures();
44+
}
45+
46+
private void ResetFeatures()
47+
{
48+
_connection = null;
49+
_tlsConnection = null;
50+
51+
((IFeatureCache)this).SetFeaturesRevision();
52+
}
53+
3654
private IHttpConnectionFeature HttpConnectionFeature
3755
{
3856
get

src/Microsoft.AspNet.Http/DefaultHttpContext.cs

+38-11
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ namespace Microsoft.AspNet.Http.Internal
1616
{
1717
public class DefaultHttpContext : HttpContext, IFeatureCache
1818
{
19-
private readonly HttpRequest _request;
20-
private readonly HttpResponse _response;
21-
private ConnectionInfo _connection;
22-
private AuthenticationManager _authenticationManager;
19+
private readonly DefaultHttpRequest _request;
20+
private readonly DefaultHttpResponse _response;
21+
22+
private DefaultAuthenticationManager _authenticationManager;
23+
private DefaultConnectionInfo _connection;
24+
private DefaultWebSocketManager _websockets;
2325

2426
private IItemsFeature _items;
2527
private IServiceProvidersFeature _serviceProviders;
2628
private IHttpAuthenticationFeature _authentication;
2729
private IHttpRequestLifetimeFeature _lifetime;
2830
private ISessionFeature _session;
29-
private WebSocketManager _websockets;
3031

3132
private IFeatureCollection _features;
3233
private int _cachedFeaturesRevision = -1;
@@ -36,28 +37,54 @@ public DefaultHttpContext()
3637
{
3738
_features.Set<IHttpRequestFeature>(new HttpRequestFeature());
3839
_features.Set<IHttpResponseFeature>(new HttpResponseFeature());
40+
((IFeatureCache)this).SetFeaturesRevision();
3941
}
4042

4143
public DefaultHttpContext(IFeatureCollection features)
4244
{
4345
_features = features;
4446
_request = new DefaultHttpRequest(this, features);
4547
_response = new DefaultHttpResponse(this, features);
48+
((IFeatureCache)this).SetFeaturesRevision();
4649
}
4750

4851
void IFeatureCache.CheckFeaturesRevision()
4952
{
5053
if (_cachedFeaturesRevision !=_features.Revision)
5154
{
52-
_items = null;
53-
_serviceProviders = null;
54-
_authentication = null;
55-
_lifetime = null;
56-
_session = null;
57-
_cachedFeaturesRevision = _features.Revision;
55+
ResetFeatures();
5856
}
5957
}
6058

59+
void IFeatureCache.SetFeaturesRevision()
60+
{
61+
_cachedFeaturesRevision = _features.Revision;
62+
}
63+
64+
public void UpdateFeatures(IFeatureCollection features)
65+
{
66+
_features = features;
67+
ResetFeatures();
68+
69+
_request.UpdateFeatures(features);
70+
_response.UpdateFeatures(features);
71+
72+
_authenticationManager?.UpdateFeatures(features);
73+
_connection?.UpdateFeatures(features);
74+
_websockets?.UpdateFeatures(features);
75+
}
76+
77+
private void ResetFeatures()
78+
{
79+
_items = null;
80+
_serviceProviders = null;
81+
_authentication = null;
82+
_lifetime = null;
83+
_session = null;
84+
85+
((IFeatureCache)this).SetFeaturesRevision();
86+
}
87+
6188
IItemsFeature ItemsFeature
6289
{
6390
get

src/Microsoft.AspNet.Http/DefaultHttpRequest.cs

+24-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Microsoft.AspNet.Http.Internal
1414
public class DefaultHttpRequest : HttpRequest, IFeatureCache
1515
{
1616
private readonly DefaultHttpContext _context;
17-
private readonly IFeatureCollection _features;
17+
private IFeatureCollection _features;
1818
private int _cachedFeaturesRevision = -1;
1919

2020
private IHttpRequestFeature _request;
@@ -26,20 +26,38 @@ public DefaultHttpRequest(DefaultHttpContext context, IFeatureCollection feature
2626
{
2727
_context = context;
2828
_features = features;
29+
((IFeatureCache)this).SetFeaturesRevision();
2930
}
3031

3132
void IFeatureCache.CheckFeaturesRevision()
3233
{
3334
if (_cachedFeaturesRevision != _features.Revision)
3435
{
35-
_request = null;
36-
_query = null;
37-
_form = null;
38-
_cookies = null;
39-
_cachedFeaturesRevision = _features.Revision;
36+
ResetFeatures();
4037
}
4138
}
4239

40+
void IFeatureCache.SetFeaturesRevision()
41+
{
42+
_cachedFeaturesRevision = _features.Revision;
43+
}
44+
45+
public void UpdateFeatures(IFeatureCollection features)
46+
{
47+
_features = features;
48+
ResetFeatures();
49+
}
50+
51+
private void ResetFeatures()
52+
{
53+
_request = null;
54+
_query = null;
55+
_form = null;
56+
_cookies = null;
57+
58+
((IFeatureCache)this).SetFeaturesRevision();
59+
}
60+
4361
private IHttpRequestFeature HttpRequestFeature
4462
{
4563
get { return FeatureHelpers.GetAndCache(this, _features, ref _request); }

src/Microsoft.AspNet.Http/DefaultHttpResponse.cs

+22-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.AspNet.Http.Internal
1313
public class DefaultHttpResponse : HttpResponse, IFeatureCache
1414
{
1515
private readonly DefaultHttpContext _context;
16-
private readonly IFeatureCollection _features;
16+
private IFeatureCollection _features;
1717
private int _cachedFeaturesRevision = -1;
1818

1919
private IHttpResponseFeature _response;
@@ -23,18 +23,36 @@ public DefaultHttpResponse(DefaultHttpContext context, IFeatureCollection featur
2323
{
2424
_context = context;
2525
_features = features;
26+
((IFeatureCache)this).SetFeaturesRevision();
2627
}
2728

2829
void IFeatureCache.CheckFeaturesRevision()
2930
{
3031
if (_cachedFeaturesRevision != _features.Revision)
3132
{
32-
_response = null;
33-
_cookies = null;
34-
_cachedFeaturesRevision = _features.Revision;
33+
ResetFeatures();
3534
}
3635
}
3736

37+
void IFeatureCache.SetFeaturesRevision()
38+
{
39+
_cachedFeaturesRevision = _features.Revision;
40+
}
41+
42+
public void UpdateFeatures(IFeatureCollection features)
43+
{
44+
_features = features;
45+
ResetFeatures();
46+
}
47+
48+
private void ResetFeatures()
49+
{
50+
_response = null;
51+
_cookies = null;
52+
53+
((IFeatureCache)this).SetFeaturesRevision();
54+
}
55+
3856
private IHttpResponseFeature HttpResponseFeature
3957
{
4058
get { return FeatureHelpers.GetAndCache(this, _features, ref _response); }

src/Microsoft.AspNet.Http/DefaultWebSocketManager.cs

+21-3
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,36 @@ public class DefaultWebSocketManager : WebSocketManager, IFeatureCache
2121
public DefaultWebSocketManager(IFeatureCollection features)
2222
{
2323
_features = features;
24+
((IFeatureCache)this).SetFeaturesRevision();
2425
}
2526

2627
void IFeatureCache.CheckFeaturesRevision()
2728
{
2829
if (_cachedFeaturesRevision != _features.Revision)
2930
{
30-
_request = null;
31-
_webSockets = null;
32-
_cachedFeaturesRevision = _features.Revision;
31+
ResetFeatures();
3332
}
3433
}
3534

35+
void IFeatureCache.SetFeaturesRevision()
36+
{
37+
_cachedFeaturesRevision = _features.Revision;
38+
}
39+
40+
public void UpdateFeatures(IFeatureCollection features)
41+
{
42+
_features = features;
43+
ResetFeatures();
44+
}
45+
46+
private void ResetFeatures()
47+
{
48+
_request = null;
49+
_webSockets = null;
50+
51+
((IFeatureCache)this).SetFeaturesRevision();
52+
}
53+
3654
private IHttpRequestFeature HttpRequestFeature
3755
{
3856
get { return FeatureHelpers.GetAndCache(this, _features, ref _request); }

src/Microsoft.AspNet.Http/Features/FeatureHelpers.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ public static T GetOrCreateAndCache<T>(
5656
if (obj == null)
5757
{
5858
obj = factory();
59-
cachedObject = obj;
60-
features.Set(obj);
6159
}
60+
cachedObject = obj;
61+
features.Set(obj);
62+
cache.SetFeaturesRevision();
6263
}
6364
return obj;
6465
}
@@ -79,9 +80,10 @@ public static T GetOrCreateAndCache<T>(
7980
if (obj == null)
8081
{
8182
obj = factory(features);
82-
cachedObject = obj;
83-
features.Set(obj);
8483
}
84+
cachedObject = obj;
85+
features.Set(obj);
86+
cache.SetFeaturesRevision();
8587
}
8688
return obj;
8789
}
@@ -103,9 +105,10 @@ public static T GetOrCreateAndCache<T>(
103105
if (obj == null)
104106
{
105107
obj = factory(request);
106-
cachedObject = obj;
107-
features.Set(obj);
108108
}
109+
cachedObject = obj;
110+
features.Set(obj);
111+
cache.SetFeaturesRevision();
109112
}
110113
return obj;
111114
}

src/Microsoft.AspNet.Http/Features/IFeatureCache.cs

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ namespace Microsoft.AspNet.Http.Features
66
internal interface IFeatureCache
77
{
88
void CheckFeaturesRevision();
9+
void SetFeaturesRevision();
910
}
1011
}

0 commit comments

Comments
 (0)