Heya
So I am just working on a bug in Content Lock package where it throws a null reference because I forgot to take into account people trying to lock unpublished nodes and was using IPublishedContentQuery
Goal
For my code I simply need to know the name of the node from the GUID/Key of the document so I want it to be the most efficient way to get it.
Using the full blown IContentService seems overkill as I need no CRUD operations and extra properties being looked up from the database.
IContentService
var contentService = _contentService.GetById(contentKey);
IPublishedContentCache
var contentCache = await _publishedContentCache.GetByIdAsync(contentKey, true);
IEntityService
var entity = _entityService.Get(contentKey, UmbracoObjectTypes.Document) as IDocumentEntitySlim;
IDocumentCacheService
var docCache = await _documentCacheService.GetByKeyAsync(contentKey, true);
Thoughts?
What is the best approach and efficient way to get just the node name or is there another approach I have not found/thought of?
With IEntityService you’re guaranteed to hit the DB on each request.
IPublishedContentCache utilizes IDocumentCacheService under the hood, so they’re comparabe.
IDocumentCacheService will try to get the unpublished document from the (hybrid) cache, and fallback to rebuilding the draft IPublishedContent from the DB.
Now. At first glance it’d seem like a no-brainer to use IDocumentCacheService, because it might hit the cache instead of the DB. But… constructing an IPublishedContent on a cache miss is quite CPU intensive, if you compare it to the construction of the IDocumentEntitySlim from IEntityService.
All in all it boils down to weighing off pros and cons.
I would probably use IDocumentCacheService if this is expected to happen a lot, because that’d mean a higher chance of a cache hit. If this happens less frequently, the IEntityService would be my choice.
As a side note, IEntityService powers a lot of the backoffice.
As for my use case this is for an Umbraco package and this part of the codebase is purely for the backoffice, then it makes sense for me to align with the backoffice already and use the IEntityService approach.
IEntityService
var entity = _entityService.Get(contentKey, UmbracoObjectTypes.Document) as IDocumentEntitySlim;
But its good to know if I needed something similar for the frontend rendering that I would lean on IDocumentCacheService/IPublishedContentCache instead.