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

Commit d8c3f0b

Browse files
committed
Use FeatureCacheHelpers
1 parent ecbe9a2 commit d8c3f0b

11 files changed

+172
-273
lines changed

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

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Microsoft.AspNet.Http.Authentication.Internal
1414
{
15-
public class DefaultAuthenticationManager : AuthenticationManager
15+
public class DefaultAuthenticationManager : AuthenticationManager, IFeatureCache
1616
{
1717
private readonly IFeatureCollection _features;
1818
private int _cachedFeaturesRevision = -1;
@@ -25,7 +25,7 @@ public DefaultAuthenticationManager(IFeatureCollection features)
2525
_features = features;
2626
}
2727

28-
void CheckFeaturesRevision()
28+
void IFeatureCache.CheckFeaturesRevision()
2929
{
3030
if (_cachedFeaturesRevision != _features.Revision)
3131
{
@@ -39,31 +39,17 @@ private IHttpAuthenticationFeature HttpAuthenticationFeature
3939
{
4040
get
4141
{
42-
CheckFeaturesRevision();
43-
if (_authentication == null)
44-
{
45-
_authentication = _features.Get<IHttpAuthenticationFeature>();
46-
if (_authentication == null)
47-
{
48-
_authentication = new HttpAuthenticationFeature();
49-
_features.Set(_authentication);
50-
}
51-
}
52-
return _authentication;
42+
return FeatureCacheHelpers .GetOrCreateAndCache(
43+
this,
44+
_features,
45+
() => new HttpAuthenticationFeature(),
46+
ref _authentication);
5347
}
5448
}
5549

5650
private IHttpResponseFeature HttpResponseFeature
5751
{
58-
get
59-
{
60-
CheckFeaturesRevision();
61-
if (_response == null)
62-
{
63-
_response = _features.Get<IHttpResponseFeature>();
64-
}
65-
return _response;
66-
}
52+
get { return FeatureCacheHelpers.GetAndCache(this, _features, ref _response); }
6753
}
6854

6955
public override IEnumerable<AuthenticationDescription> GetAuthenticationSchemes()

src/Microsoft.AspNet.Http/DefaultConnectionInfo.cs

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Microsoft.AspNet.Http.Internal
1212
{
13-
public class DefaultConnectionInfo : ConnectionInfo
13+
public class DefaultConnectionInfo : ConnectionInfo, IFeatureCache
1414
{
1515
private readonly IFeatureCollection _features;
1616
private int _cachedFeaturesRevision = -1;
@@ -23,7 +23,7 @@ public DefaultConnectionInfo(IFeatureCollection features)
2323
_features = features;
2424
}
2525

26-
void CheckFeaturesRevision()
26+
void IFeatureCache.CheckFeaturesRevision()
2727
{
2828
if (_cachedFeaturesRevision != _features.Revision)
2929
{
@@ -37,41 +37,23 @@ private IHttpConnectionFeature HttpConnectionFeature
3737
{
3838
get
3939
{
40-
CheckFeaturesRevision();
41-
42-
var connection = _connection;
43-
if (connection == null)
44-
{
45-
connection = _features.Get<IHttpConnectionFeature>();
46-
if (connection == null)
47-
{
48-
connection = new HttpConnectionFeature();
49-
_features.Set(connection);
50-
}
51-
_connection = connection;
52-
}
53-
return connection;
40+
return FeatureCacheHelpers.GetOrCreateAndCache(
41+
this,
42+
_features,
43+
() => new HttpConnectionFeature(),
44+
ref _connection);
5445
}
5546
}
5647

5748
private ITlsConnectionFeature TlsConnectionFeature
5849
{
5950
get
6051
{
61-
CheckFeaturesRevision();
62-
63-
var tlsConnection = _tlsConnection;
64-
if (tlsConnection == null)
65-
{
66-
tlsConnection = _features.Get<ITlsConnectionFeature>();
67-
if (tlsConnection == null)
68-
{
69-
tlsConnection = new TlsConnectionFeature();
70-
_features.Set(tlsConnection);
71-
}
72-
_tlsConnection = tlsConnection;
73-
}
74-
return tlsConnection;
52+
return FeatureCacheHelpers.GetOrCreateAndCache(
53+
this,
54+
_features,
55+
() => new TlsConnectionFeature(),
56+
ref _tlsConnection);
7557
}
7658
}
7759

src/Microsoft.AspNet.Http/DefaultHttpContext.cs

Lines changed: 22 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace Microsoft.AspNet.Http.Internal
1616
{
17-
public class DefaultHttpContext : HttpContext
17+
public class DefaultHttpContext : HttpContext, IFeatureCache
1818
{
1919
private readonly HttpRequest _request;
2020
private readonly HttpResponse _response;
@@ -47,7 +47,7 @@ public DefaultHttpContext(IFeatureCollection features)
4747
_authenticationManager = new DefaultAuthenticationManager(features);
4848
}
4949

50-
void CheckFeaturesRevision()
50+
void IFeatureCache.CheckFeaturesRevision()
5151
{
5252
if (_cachedFeaturesRevision !=_features.Revision)
5353
{
@@ -64,98 +64,52 @@ IItemsFeature ItemsFeature
6464
{
6565
get
6666
{
67-
CheckFeaturesRevision();
68-
69-
var items = _items;
70-
if (items == null)
71-
{
72-
items = _features.Get<IItemsFeature>();
73-
if (items == null)
74-
{
75-
items = new ItemsFeature();
76-
_features.Set(items);
77-
}
78-
_items = items;
79-
}
80-
return items;
67+
return FeatureCacheHelpers.GetOrCreateAndCache(
68+
this,
69+
_features,
70+
() => new ItemsFeature(),
71+
ref _items);
8172
}
8273
}
8374

8475
IServiceProvidersFeature ServiceProvidersFeature
8576
{
8677
get
8778
{
88-
CheckFeaturesRevision();
89-
90-
var serviceProviders = _serviceProviders;
91-
if (serviceProviders == null)
92-
{
93-
serviceProviders = _features.Get<IServiceProvidersFeature>();
94-
if (serviceProviders == null)
95-
{
96-
serviceProviders = new ServiceProvidersFeature();
97-
_features.Set(serviceProviders);
98-
_serviceProviders = serviceProviders;
99-
}
100-
}
101-
return serviceProviders;
79+
return FeatureCacheHelpers.GetOrCreateAndCache(
80+
this,
81+
_features,
82+
() => new ServiceProvidersFeature(),
83+
ref _serviceProviders);
10284
}
10385
}
10486

10587
private IHttpAuthenticationFeature HttpAuthenticationFeature
10688
{
10789
get
10890
{
109-
CheckFeaturesRevision();
110-
111-
var authentication = _authentication;
112-
if (authentication == null)
113-
{
114-
authentication = _features.Get<IHttpAuthenticationFeature>();
115-
if (authentication == null)
116-
{
117-
authentication = new HttpAuthenticationFeature();
118-
_features.Set(authentication);
119-
}
120-
_authentication = authentication;
121-
}
122-
return authentication;
91+
return FeatureCacheHelpers.GetOrCreateAndCache(
92+
this,
93+
_features,
94+
() => new HttpAuthenticationFeature(),
95+
ref _authentication);
12396
}
12497
}
12598

12699
private IHttpRequestLifetimeFeature LifetimeFeature
127100
{
128101
get
129102
{
130-
CheckFeaturesRevision();
131-
132-
var lifetime = _lifetime;
133-
if (lifetime == null)
134-
{
135-
lifetime = _features.Get<IHttpRequestLifetimeFeature>();
136-
if (lifetime == null)
137-
{
138-
lifetime = new HttpRequestLifetimeFeature();
139-
_features.Set(lifetime);
140-
}
141-
_lifetime = lifetime;
142-
}
143-
return lifetime;
103+
return FeatureCacheHelpers.GetOrCreateAndCache(this,
104+
_features,
105+
() => new HttpRequestLifetimeFeature(),
106+
ref _lifetime);
144107
}
145108
}
146109

147110
private ISessionFeature SessionFeature
148111
{
149-
get
150-
{
151-
CheckFeaturesRevision();
152-
153-
if (_session == null)
154-
{
155-
_session = _features.Get<ISessionFeature>();
156-
}
157-
return _session;
158-
}
112+
get { return FeatureCacheHelpers.GetAndCache(this, _features, ref _session); }
159113
set
160114
{
161115
_features.Set(value);

src/Microsoft.AspNet.Http/DefaultHttpRequest.cs

Lines changed: 19 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Microsoft.AspNet.Http.Internal
1313
{
14-
public class DefaultHttpRequest : HttpRequest
14+
public class DefaultHttpRequest : HttpRequest, IFeatureCache
1515
{
1616
private readonly DefaultHttpContext _context;
1717
private readonly IFeatureCollection _features;
@@ -28,7 +28,7 @@ public DefaultHttpRequest(DefaultHttpContext context, IFeatureCollection feature
2828
_features = features;
2929
}
3030

31-
void CheckFeaturesRevision()
31+
void IFeatureCache.CheckFeaturesRevision()
3232
{
3333
if (_cachedFeaturesRevision != _features.Revision)
3434
{
@@ -42,80 +42,43 @@ void CheckFeaturesRevision()
4242

4343
private IHttpRequestFeature HttpRequestFeature
4444
{
45-
get
46-
{
47-
CheckFeaturesRevision();
48-
49-
var request = _request;
50-
if (request == null)
51-
{
52-
request = _features.Get<IHttpRequestFeature>();
53-
_request = request;
54-
}
55-
return request;
56-
}
45+
get { return FeatureCacheHelpers.GetAndCache(this, _features, ref _request); }
5746
}
5847

5948
private IQueryFeature QueryFeature
6049
{
6150
get
6251
{
63-
CheckFeaturesRevision();
64-
65-
var query = _query;
66-
if (query == null)
67-
{
68-
query = _features.Get<IQueryFeature>();
69-
if (query == null)
70-
{
71-
query = new QueryFeature(_features);
72-
_features.Set(query);
73-
}
74-
_query = query;
75-
}
76-
return query;
52+
return FeatureCacheHelpers.GetOrCreateAndCache(
53+
this,
54+
_features,
55+
(f) => new QueryFeature(f),
56+
ref _query);
7757
}
7858
}
7959

8060
private IFormFeature FormFeature
8161
{
8262
get
8363
{
84-
CheckFeaturesRevision();
85-
86-
var form = _form;
87-
if (form == null)
88-
{
89-
form = _features.Get<IFormFeature>();
90-
if (form == null)
91-
{
92-
form = new FormFeature(this);
93-
_features.Set(form);
94-
}
95-
_form = form;
96-
}
97-
return form;
64+
return FeatureCacheHelpers.GetOrCreateAndCache(
65+
this,
66+
_features,
67+
this,
68+
(r) => new FormFeature(r),
69+
ref _form);
9870
}
9971
}
10072

10173
private IRequestCookiesFeature RequestCookiesFeature
10274
{
10375
get
10476
{
105-
CheckFeaturesRevision();
106-
107-
var cookies = _cookies;
108-
if (cookies == null)
109-
{
110-
cookies = _features.Get<IRequestCookiesFeature>();
111-
if (cookies == null)
112-
{
113-
cookies = new RequestCookiesFeature(_features);
114-
_features.Set(cookies);
115-
}
116-
_cookies = cookies;
117-
}
118-
return cookies;
77+
return FeatureCacheHelpers.GetOrCreateAndCache(
78+
this,
79+
_features,
80+
(f) => new RequestCookiesFeature(f),
81+
ref _cookies);
11982
}
12083
}
12184

0 commit comments

Comments
 (0)