Added a null check to IUmbracoHelperAccessor & IPublishedSnapshotAccessor

This commit is contained in:
Zeegaan
2021-08-10 10:55:29 +02:00
parent 3eb32a97a7
commit 5d264fefdb
28 changed files with 173 additions and 66 deletions

View File

@@ -125,7 +125,7 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
private CacheValues GetCacheValues(PropertyCacheLevel cacheLevel)
{
CacheValues cacheValues;
PublishedSnapshot publishedSnapshot;
IPublishedSnapshot publishedSnapshot;
IAppCache cache;
switch (cacheLevel)
{
@@ -142,7 +142,10 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
// elements cache (if we don't want to pollute the elements cache with short-lived
// data) depending on settings
// for members, always cache in the snapshot cache - never pollute elements cache
publishedSnapshot = (PublishedSnapshot) _publishedSnapshotAccessor.PublishedSnapshot;
if (_publishedSnapshotAccessor.TryGetPublishedSnapshot(out publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
cache = publishedSnapshot == null
? null
: ((_isPreviewing == false || PublishedSnapshotService.FullCacheWhenPreviewing) && (_isMember == false)
@@ -152,7 +155,10 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
break;
case PropertyCacheLevel.Snapshot:
// cache within the snapshot cache
publishedSnapshot = (PublishedSnapshot) _publishedSnapshotAccessor.PublishedSnapshot;
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
cache = publishedSnapshot?.SnapshotCache;
cacheValues = GetCacheValues(cache);
break;

View File

@@ -238,7 +238,10 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
get
{
var getById = GetGetterById();
var publishedSnapshot = _publishedSnapshotAccessor.PublishedSnapshot;
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
return getById(publishedSnapshot, IsPreviewing, ParentId);
}
}
@@ -249,7 +252,10 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
get
{
var getById = GetGetterById();
var publishedSnapshot = _publishedSnapshotAccessor.PublishedSnapshot;
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
var id = _contentNode.FirstChildContentId;
while (id > 0)
@@ -316,7 +322,10 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
// beware what you use that one for - you don't want to cache its result
private IAppCache GetAppropriateCache()
{
var publishedSnapshot = _publishedSnapshotAccessor.PublishedSnapshot;
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
var cache = publishedSnapshot == null
? null
: ((IsPreviewing == false || PublishedSnapshotService.FullCacheWhenPreviewing) && (ContentType.ItemType != PublishedItemType.Member)
@@ -327,7 +336,10 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
private IAppCache GetCurrentSnapshotCache()
{
var publishedSnapshot = _publishedSnapshotAccessor.PublishedSnapshot;
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
{
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
}
return publishedSnapshot?.SnapshotCache;
}

View File

@@ -117,7 +117,7 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
_publishedModelFactory = publishedModelFactory;
}
protected PublishedSnapshot CurrentPublishedSnapshot => (PublishedSnapshot)_publishedSnapshotAccessor.PublishedSnapshot;
protected PublishedSnapshot CurrentPublishedSnapshot { get { _publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot); return (PublishedSnapshot)publishedSnapshot; } }
// NOTE: These aren't used within this object but are made available internally to improve the IdKey lookup performance
// when nucache is enabled.