From a020c52ff65cef7a3f3e61a43f9a3387e2c51cdb Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 23 Jan 2020 18:10:21 +1100 Subject: [PATCH] Simplifies IdKeyMap interface, moves the weird add mapping logic in PublishedSnapshotService to a composer. --- .../Services/IIdKeyMap.cs | 1 - .../PublishedContent/NuCacheChildrenTests.cs | 1 - .../PublishedContent/NuCacheTests.cs | 1 - .../Scoping/ScopedNuCacheTests.cs | 1 - .../ContentTypeServiceVariantsTests.cs | 1 - .../PublishedCache/NuCache/NuCacheComposer.cs | 17 +++++++++++++ .../NuCache/PublishedSnapshotService.cs | 25 +++++++++++-------- src/Umbraco.Web/Runtime/WebInitialComposer.cs | 2 +- 8 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/Umbraco.Abstractions/Services/IIdKeyMap.cs b/src/Umbraco.Abstractions/Services/IIdKeyMap.cs index b08a109f5a..05d1b2283d 100644 --- a/src/Umbraco.Abstractions/Services/IIdKeyMap.cs +++ b/src/Umbraco.Abstractions/Services/IIdKeyMap.cs @@ -5,7 +5,6 @@ namespace Umbraco.Core.Services { public interface IIdKeyMap { - void SetMapper(UmbracoObjectTypes umbracoObjectType, Func id2key, Func key2id); Attempt GetIdForKey(Guid key, UmbracoObjectTypes umbracoObjectType); Attempt GetIdForUdi(Udi udi); Attempt GetUdiForId(int id, UmbracoObjectTypes umbracoObjectType); diff --git a/src/Umbraco.Tests/PublishedContent/NuCacheChildrenTests.cs b/src/Umbraco.Tests/PublishedContent/NuCacheChildrenTests.cs index 6fe8b69778..b03ee25e4a 100644 --- a/src/Umbraco.Tests/PublishedContent/NuCacheChildrenTests.cs +++ b/src/Umbraco.Tests/PublishedContent/NuCacheChildrenTests.cs @@ -150,7 +150,6 @@ namespace Umbraco.Tests.PublishedContent runtime, serviceContext, contentTypeFactory, - null, _snapshotAccessor, _variationAccesor, Mock.Of(), diff --git a/src/Umbraco.Tests/PublishedContent/NuCacheTests.cs b/src/Umbraco.Tests/PublishedContent/NuCacheTests.cs index c77f8528cf..8bf976d24e 100644 --- a/src/Umbraco.Tests/PublishedContent/NuCacheTests.cs +++ b/src/Umbraco.Tests/PublishedContent/NuCacheTests.cs @@ -192,7 +192,6 @@ namespace Umbraco.Tests.PublishedContent runtime, serviceContext, contentTypeFactory, - null, new TestPublishedSnapshotAccessor(), _variationAccesor, Mock.Of(), diff --git a/src/Umbraco.Tests/Scoping/ScopedNuCacheTests.cs b/src/Umbraco.Tests/Scoping/ScopedNuCacheTests.cs index ceb78493a0..e5aaa345f2 100644 --- a/src/Umbraco.Tests/Scoping/ScopedNuCacheTests.cs +++ b/src/Umbraco.Tests/Scoping/ScopedNuCacheTests.cs @@ -92,7 +92,6 @@ namespace Umbraco.Tests.Scoping runtimeStateMock.Object, ServiceContext, contentTypeFactory, - null, publishedSnapshotAccessor, Mock.Of(), ProfilingLogger, diff --git a/src/Umbraco.Tests/Services/ContentTypeServiceVariantsTests.cs b/src/Umbraco.Tests/Services/ContentTypeServiceVariantsTests.cs index 0f1b1fed32..6396564a3e 100644 --- a/src/Umbraco.Tests/Services/ContentTypeServiceVariantsTests.cs +++ b/src/Umbraco.Tests/Services/ContentTypeServiceVariantsTests.cs @@ -65,7 +65,6 @@ namespace Umbraco.Tests.Services runtimeStateMock.Object, ServiceContext, contentTypeFactory, - null, publishedSnapshotAccessor, Mock.Of(), ProfilingLogger, diff --git a/src/Umbraco.Web/PublishedCache/NuCache/NuCacheComposer.cs b/src/Umbraco.Web/PublishedCache/NuCache/NuCacheComposer.cs index f748fd555c..186f3908d1 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/NuCacheComposer.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/NuCacheComposer.cs @@ -1,5 +1,8 @@ using Umbraco.Core; using Umbraco.Core.Composing; +using Umbraco.Core.Models; +using Umbraco.Core.Scoping; +using Umbraco.Core.Services; using Umbraco.Web.PublishedCache.NuCache.DataSource; namespace Umbraco.Web.PublishedCache.NuCache @@ -18,6 +21,20 @@ namespace Umbraco.Web.PublishedCache.NuCache composition.Register(factory => new PublishedSnapshotServiceOptions()); composition.SetPublishedSnapshotService(); + // replace this service since we want to improve the content/media + // mapping lookups if we are using nucache. + composition.RegisterUnique(factory => + { + var idkSvc = new IdKeyMap(factory.GetInstance()); + var publishedSnapshotService = factory.GetInstance() as PublishedSnapshotService; + if (publishedSnapshotService != null) + { + idkSvc.SetMapper(UmbracoObjectTypes.Document, id => publishedSnapshotService.GetDocumentUid(id), uid => publishedSnapshotService.GetDocumentId(uid)); + idkSvc.SetMapper(UmbracoObjectTypes.Media, id => publishedSnapshotService.GetMediaUid(id), uid => publishedSnapshotService.GetMediaId(uid)); + } + return idkSvc; + }); + // add the NuCache health check (hidden from type finder) // TODO: no NuCache health check yet //composition.HealthChecks().Add(); diff --git a/src/Umbraco.Web/PublishedCache/NuCache/PublishedSnapshotService.cs b/src/Umbraco.Web/PublishedCache/NuCache/PublishedSnapshotService.cs index 4fee9a2107..7990433ea2 100755 --- a/src/Umbraco.Web/PublishedCache/NuCache/PublishedSnapshotService.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/PublishedSnapshotService.cs @@ -76,7 +76,7 @@ namespace Umbraco.Web.PublishedCache.NuCache //private static int _singletonCheck; public PublishedSnapshotService(PublishedSnapshotServiceOptions options, IMainDom mainDom, IRuntimeState runtime, - ServiceContext serviceContext, IPublishedContentTypeFactory publishedContentTypeFactory, IIdKeyMap idKeyMap, + ServiceContext serviceContext, IPublishedContentTypeFactory publishedContentTypeFactory, IPublishedSnapshotAccessor publishedSnapshotAccessor, IVariationContextAccessor variationContextAccessor, IProfilingLogger logger, IScopeProvider scopeProvider, IDocumentRepository documentRepository, IMediaRepository mediaRepository, IMemberRepository memberRepository, IDefaultCultureAccessor defaultCultureAccessor, @@ -156,17 +156,22 @@ namespace Umbraco.Web.PublishedCache.NuCache LoadCachesOnStartup(); } - - Guid GetUid(ContentStore store, int id) => store.LiveSnapshot.Get(id)?.Uid ?? default; - int GetId(ContentStore store, Guid uid) => store.LiveSnapshot.Get(uid)?.Id ?? default; - - if (idKeyMap != null) - { - idKeyMap.SetMapper(UmbracoObjectTypes.Document, id => GetUid(_contentStore, id), uid => GetId(_contentStore, uid)); - idKeyMap.SetMapper(UmbracoObjectTypes.Media, id => GetUid(_mediaStore, id), uid => GetId(_mediaStore, uid)); - } } + #region Id <-> Key methods + + // NOTE: These aren't used within this object but are made available internally to improve the IdKey lookup performance + // when nucache is enabled. + + internal int GetDocumentId(Guid udi) => GetId(_contentStore, udi); + internal int GetMediaId(Guid udi) => GetId(_mediaStore, udi); + internal Guid GetDocumentUid(int id) => GetUid(_contentStore, id); + internal Guid GetMediaUid(int id) => GetUid(_mediaStore, id); + private int GetId(ContentStore store, Guid uid) => store.LiveSnapshot.Get(uid)?.Id ?? default; + private Guid GetUid(ContentStore store, int id) => store.LiveSnapshot.Get(id)?.Uid ?? default; + + #endregion + /// /// Install phase of /// diff --git a/src/Umbraco.Web/Runtime/WebInitialComposer.cs b/src/Umbraco.Web/Runtime/WebInitialComposer.cs index e2d2e59157..f8d09a75ea 100644 --- a/src/Umbraco.Web/Runtime/WebInitialComposer.cs +++ b/src/Umbraco.Web/Runtime/WebInitialComposer.cs @@ -143,7 +143,7 @@ namespace Umbraco.Web.Runtime composition.RegisterUnique(); composition.RegisterUnique(); composition.RegisterUnique(); - composition.RegisterUnique(); + composition.RegisterUnique(); composition.RegisterUnique();