Files
Umbraco-CMS/src/Umbraco.Web/PublishedCache/PublishedElementPropertyBase.cs

170 lines
7.6 KiB
C#
Raw Normal View History

using System;
2017-09-29 15:51:33 +02:00
using Umbraco.Core.Cache;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
namespace Umbraco.Web.PublishedCache
{
2017-09-29 15:51:33 +02:00
internal class PublishedElementPropertyBase : PublishedPropertyBase
{
private readonly object _locko = new object();
private readonly object _sourceValue;
2017-10-31 12:48:24 +01:00
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
2017-09-29 15:51:33 +02:00
protected readonly IPublishedElement Element;
protected readonly bool IsPreviewing;
protected readonly bool IsMember;
private bool _interInitialized;
private object _interValue;
private CacheValues _cacheValues;
2017-09-29 15:51:33 +02:00
private string _valuesCacheKey;
2017-09-29 15:51:33 +02:00
// define constant - determines whether to use cache when previewing
// to store eg routes, property converted values, anything - caching
// means faster execution, but uses memory - not sure if we want it
// so making it configureable.
private const bool FullCacheWhenPreviewing = true;
2017-10-31 12:48:24 +01:00
public PublishedElementPropertyBase(PublishedPropertyType propertyType, IPublishedElement element, bool previewing, PropertyCacheLevel referenceCacheLevel, object sourceValue = null, IPublishedSnapshotAccessor publishedSnapshotAccessor = null)
: base(propertyType, referenceCacheLevel)
{
_sourceValue = sourceValue;
2017-10-31 12:48:24 +01:00
_publishedSnapshotAccessor = publishedSnapshotAccessor;
2017-09-29 15:51:33 +02:00
Element = element;
IsPreviewing = previewing;
IsMember = propertyType.ContentType.ItemType == PublishedItemType.Member;
}
2018-04-21 09:57:28 +02:00
public override bool HasValue(string culture = null, string segment = null)
2017-09-29 15:51:33 +02:00
=> _sourceValue != null && (!(_sourceValue is string s) || !string.IsNullOrWhiteSpace(s));
// used to cache the CacheValues of this property
// ReSharper disable InconsistentlySynchronizedField
internal string ValuesCacheKey => _valuesCacheKey
?? (_valuesCacheKey = PropertyCacheValues(Element.Key, Alias, IsPreviewing));
2017-09-29 15:51:33 +02:00
// ReSharper restore InconsistentlySynchronizedField
protected class CacheValues
{
public bool ObjectInitialized;
public object ObjectValue;
public bool XPathInitialized;
public object XPathValue;
}
2017-09-29 15:51:33 +02:00
public static string PropertyCacheValues(Guid contentUid, string typeAlias, bool previewing)
{
2017-10-31 12:48:24 +01:00
return "PublishedSnapshot.Property.CacheValues[" + (previewing ? "D:" : "P:") + contentUid + ":" + typeAlias + "]";
2017-09-29 15:51:33 +02:00
}
private void GetCacheLevels(out PropertyCacheLevel cacheLevel, out PropertyCacheLevel referenceCacheLevel)
{
// based upon the current reference cache level (ReferenceCacheLevel) and this property
// cache level (PropertyType.CacheLevel), determines both the actual cache level for the
// property, and the new reference cache level.
// if the property cache level is 'shorter-termed' that the reference
// then use it and it becomes the new reference, else use Content and
// don't change the reference.
//
// examples:
2017-10-31 12:48:24 +01:00
// currently (reference) caching at published snapshot, property specifies
// elements, ok to use element. OTOH, currently caching at elements,
// property specifies snapshot, need to use snapshot.
//
2017-07-27 11:09:53 +02:00
if (PropertyType.CacheLevel > ReferenceCacheLevel || PropertyType.CacheLevel == PropertyCacheLevel.None)
{
cacheLevel = PropertyType.CacheLevel;
referenceCacheLevel = cacheLevel;
}
else
{
2017-10-31 12:48:24 +01:00
cacheLevel = PropertyCacheLevel.Element;
referenceCacheLevel = ReferenceCacheLevel;
}
}
2017-09-29 15:51:33 +02:00
private ICacheProvider GetSnapshotCache()
{
2017-10-31 12:48:24 +01:00
// cache within the snapshot cache, unless previewing, then use the snapshot or
// elements cache (if we don't want to pollute the elements cache with short-lived
2017-09-29 15:51:33 +02:00
// data) depending on settings
2017-10-31 12:48:24 +01:00
// for members, always cache in the snapshot cache - never pollute elements cache
var publishedSnapshot = _publishedSnapshotAccessor?.PublishedSnapshot;
if (publishedSnapshot == null) return null;
2017-09-29 15:51:33 +02:00
return (IsPreviewing == false || FullCacheWhenPreviewing) && IsMember == false
2017-10-31 12:48:24 +01:00
? publishedSnapshot.ElementsCache
: publishedSnapshot.SnapshotCache;
2017-09-29 15:51:33 +02:00
}
private CacheValues GetCacheValues(PropertyCacheLevel cacheLevel)
{
CacheValues cacheValues;
switch (cacheLevel)
{
case PropertyCacheLevel.None:
// never cache anything
cacheValues = new CacheValues();
break;
2017-10-31 12:48:24 +01:00
case PropertyCacheLevel.Element:
// cache within the property object itself, ie within the content object
cacheValues = _cacheValues ?? (_cacheValues = new CacheValues());
break;
2017-10-31 12:48:24 +01:00
case PropertyCacheLevel.Elements:
// cache within the elements cache, depending...
2017-09-29 15:51:33 +02:00
var snapshotCache = GetSnapshotCache();
cacheValues = (CacheValues) snapshotCache?.GetCacheItem(ValuesCacheKey, () => new CacheValues()) ?? new CacheValues();
break;
2017-10-31 12:48:24 +01:00
case PropertyCacheLevel.Snapshot:
// cache within the snapshot cache
var facadeCache = _publishedSnapshotAccessor?.PublishedSnapshot?.SnapshotCache;
2017-09-29 15:51:33 +02:00
cacheValues = (CacheValues) facadeCache?.GetCacheItem(ValuesCacheKey, () => new CacheValues()) ?? new CacheValues();
break;
default:
throw new InvalidOperationException("Invalid cache level.");
}
return cacheValues;
}
private object GetInterValue()
{
if (_interInitialized) return _interValue;
2017-09-29 15:51:33 +02:00
_interValue = PropertyType.ConvertSourceToInter(Element, _sourceValue, IsPreviewing);
_interInitialized = true;
return _interValue;
}
2018-04-21 09:57:28 +02:00
public override object GetSourceValue(string culture = null, string segment = null) => _sourceValue;
2018-04-21 09:57:28 +02:00
public override object GetValue(string culture = null, string segment = null)
{
2017-12-06 11:51:35 +01:00
GetCacheLevels(out var cacheLevel, out var referenceCacheLevel);
lock (_locko)
{
2017-12-06 11:51:35 +01:00
var cacheValues = GetCacheValues(cacheLevel);
if (cacheValues.ObjectInitialized) return cacheValues.ObjectValue;
cacheValues.ObjectValue = PropertyType.ConvertInterToObject(Element, referenceCacheLevel, GetInterValue(), IsPreviewing);
cacheValues.ObjectInitialized = true;
return cacheValues.ObjectValue;
}
}
2018-04-21 09:57:28 +02:00
public override object GetXPathValue(string culture = null, string segment = null)
{
2017-12-06 11:51:35 +01:00
GetCacheLevels(out var cacheLevel, out var referenceCacheLevel);
lock (_locko)
{
2017-12-06 11:51:35 +01:00
var cacheValues = GetCacheValues(cacheLevel);
if (cacheValues.XPathInitialized) return cacheValues.XPathValue;
cacheValues.XPathValue = PropertyType.ConvertInterToXPath(Element, referenceCacheLevel, GetInterValue(), IsPreviewing);
cacheValues.XPathInitialized = true;
return cacheValues.XPathValue;
}
}
}
2017-07-20 11:21:28 +02:00
}