using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.PublishedCache;
using Umbraco.Cms.Infrastructure.PublishedCache.Persistence;
using Umbraco.Core.Services;
namespace Umbraco.Extensions
{
///
/// Extension methods for for the Umbraco's NuCache
///
public static class UmbracoBuilderExtensions
{
///
/// Adds Umbraco NuCache dependencies
///
public static IUmbracoBuilder AddNuCache(this IUmbracoBuilder builder)
{
// register the NuCache database data source
builder.Services.TryAddSingleton();
builder.Services.TryAddSingleton();
builder.Services.TryAddSingleton();
// register the NuCache published snapshot service
// must register default options, required in the service ctor
builder.Services.TryAddTransient(factory => new PublishedSnapshotServiceOptions());
builder.SetPublishedSnapshotService();
// Add as itself
builder.Services.TryAddSingleton();
builder.Services.TryAddSingleton();
// replace this service since we want to improve the content/media
// mapping lookups if we are using nucache.
// TODO: Gotta wonder how much this does actually improve perf? It's a lot of weird code to make this happen so hope it's worth it
builder.Services.AddUnique(factory =>
{
var idkSvc = new IdKeyMap(factory.GetRequiredService());
if (factory.GetRequiredService() is PublishedSnapshotService publishedSnapshotService)
{
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();
return builder;
}
}
}