Moves cache initialization the the request middleware

This commit is contained in:
Shannon
2020-12-14 16:55:45 +11:00
parent 776df77dfe
commit fc16669a91
2 changed files with 32 additions and 20 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
@@ -8,6 +9,7 @@ using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Logging;
using Umbraco.Web.Common.Lifetime;
using Umbraco.Web.PublishedCache.NuCache;
namespace Umbraco.Web.Common.Middleware
{
@@ -16,7 +18,12 @@ namespace Umbraco.Web.Common.Middleware
/// Manages Umbraco request objects and their lifetime
/// </summary>
/// <remarks>
/// <para>
/// This is responsible for initializing the content cache
/// </para>
/// <para>
/// This is responsible for creating and assigning an <see cref="IUmbracoContext"/>
/// </para>
/// </remarks>
public class UmbracoRequestMiddleware : IMiddleware
{
@@ -25,6 +32,10 @@ namespace Umbraco.Web.Common.Middleware
private readonly IUmbracoContextFactory _umbracoContextFactory;
private readonly IRequestCache _requestCache;
private readonly IBackOfficeSecurityFactory _backofficeSecurityFactory;
private readonly PublishedSnapshotServiceEventHandler _publishedSnapshotServiceEventHandler;
private static bool s_cacheInitialized = false;
private static bool s_cacheInitializedFlag = false;
private static object s_cacheInitializedLock = new object();
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoRequestMiddleware"/> class.
@@ -34,13 +45,15 @@ namespace Umbraco.Web.Common.Middleware
IUmbracoRequestLifetimeManager umbracoRequestLifetimeManager,
IUmbracoContextFactory umbracoContextFactory,
IRequestCache requestCache,
IBackOfficeSecurityFactory backofficeSecurityFactory)
IBackOfficeSecurityFactory backofficeSecurityFactory,
PublishedSnapshotServiceEventHandler publishedSnapshotServiceEventHandler)
{
_logger = logger;
_umbracoRequestLifetimeManager = umbracoRequestLifetimeManager;
_umbracoContextFactory = umbracoContextFactory;
_requestCache = requestCache;
_backofficeSecurityFactory = backofficeSecurityFactory;
_publishedSnapshotServiceEventHandler = publishedSnapshotServiceEventHandler;
}
/// <inheritdoc/>
@@ -55,6 +68,8 @@ namespace Umbraco.Web.Common.Middleware
return;
}
EnsureContentCacheInitialized();
_backofficeSecurityFactory.EnsureBackOfficeSecurity(); // Needs to be before UmbracoContext, TODO: Why?
UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext();
@@ -109,16 +124,15 @@ namespace Umbraco.Web.Common.Middleware
/// <summary>
/// Any object that is in the HttpContext.Items collection that is IDisposable will get disposed on the end of the request
/// </summary>
/// <param name="http"></param>
/// <param name="requestCache"></param>
/// <param name="requestUri"></param>
private static void DisposeRequestCacheItems(ILogger<UmbracoRequestMiddleware> logger, IRequestCache requestCache, Uri requestUri)
{
// do not process if client-side request
if (requestUri.IsClientSideRequest())
{
return;
}
//get a list of keys to dispose
// get a list of keys to dispose
var keys = new HashSet<string>();
foreach (var i in requestCache)
{
@@ -127,7 +141,7 @@ namespace Umbraco.Web.Common.Middleware
keys.Add(i.Key);
}
}
//dispose each item and key that was found as disposable.
// dispose each item and key that was found as disposable.
foreach (var k in keys)
{
try
@@ -149,6 +163,17 @@ namespace Umbraco.Web.Common.Middleware
}
}
/// <summary>
/// Initializes the content cache one time
/// </summary>
private void EnsureContentCacheInitialized() => LazyInitializer.EnsureInitialized(
ref s_cacheInitialized,
ref s_cacheInitializedFlag,
ref s_cacheInitializedLock,
() =>
{
_publishedSnapshotServiceEventHandler.Start();
return true;
});
}
}