// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; namespace Microsoft.AspNet.Http.Features { internal static class FeatureHelpers { public static T GetAndCache( IFeatureCache cache, IFeatureCollection features, ref T cachedObject) where T : class { cache.CheckFeaturesRevision(); T obj = cachedObject; if (obj == null) { obj = features.Get(); cachedObject = obj; } return obj; } public static T GetOrCreate( IFeatureCollection features, Func factory) where T : class { T obj = features.Get(); if (obj == null) { obj = factory(); features.Set(obj); } return obj; } public static T GetOrCreateAndCache( IFeatureCache cache, IFeatureCollection features, Func factory, ref T cachedObject) where T : class { cache.CheckFeaturesRevision(); T obj = cachedObject; if (obj == null) { obj = features.Get(); if (obj == null) { obj = factory(); } cachedObject = obj; features.Set(obj); cache.SetFeaturesRevision(); } return obj; } public static T GetOrCreateAndCache( IFeatureCache cache, IFeatureCollection features, Func factory, ref T cachedObject) where T : class { cache.CheckFeaturesRevision(); T obj = cachedObject; if (obj == null) { obj = features.Get(); if (obj == null) { obj = factory(features); } cachedObject = obj; features.Set(obj); cache.SetFeaturesRevision(); } return obj; } public static T GetOrCreateAndCache( IFeatureCache cache, IFeatureCollection features, HttpRequest request, Func factory, ref T cachedObject) where T : class { cache.CheckFeaturesRevision(); T obj = cachedObject; if (obj == null) { obj = features.Get(); if (obj == null) { obj = factory(request); } cachedObject = obj; features.Set(obj); cache.SetFeaturesRevision(); } return obj; } } }