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

Reduce Revision check calls via interface #623

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions src/Microsoft.AspNetCore.Http.Features/FeatureReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,39 @@ public FeatureReferences(IFeatureCollection collection)
public TFeature Fetch<TFeature, TState>(
ref TFeature cached,
TState state,
Func<TState, TFeature> factory)
Func<TState, TFeature> factory) where TFeature : class
{
var cleared = false;
if (Revision != Collection.Revision)
var revision = Collection.Revision;
if (Revision == revision)
{
cleared = true;
Cache = default(TCache);
Revision = Collection.Revision;
// collection unchanged, use cached
return cached ?? UpdateCached(ref cached, state, factory);
}

var feature = cached;
if (feature == null || cleared)
{
feature = Collection.Get<TFeature>();
if (feature == null)
{
feature = factory(state);
// collection changed, clear cache
Cache = default(TCache);
// empty cache is current revision
Revision = revision;

Collection.Set(feature);
return UpdateCached(ref cached, state, factory);
}

Revision = Collection.Revision;
}
cached = feature;
private TFeature UpdateCached<TFeature, TState>(ref TFeature cached, TState state, Func<TState, TFeature> factory) where TFeature : class
{
cached = Collection.Get<TFeature>();
if (cached == null)
{
// create if item not in collection
cached = factory(state);
Collection.Set(cached);
// Revision changed by .Set, update revision
Revision = Collection.Revision;
}
return feature;

return cached;
}

public TFeature Fetch<TFeature>(ref TFeature cached, Func<IFeatureCollection, TFeature> factory) =>
Fetch(ref cached, Collection, factory);
public TFeature Fetch<TFeature>(ref TFeature cached, Func<IFeatureCollection, TFeature> factory)
where TFeature : class => Fetch(ref cached, Collection, factory);
}
}