2016-05-27 14:26:28 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Xml.XPath;
|
2019-04-12 16:05:43 +02:00
|
|
|
|
using Umbraco.Core;
|
2016-05-30 19:54:36 +02:00
|
|
|
|
using Umbraco.Core.Cache;
|
2016-05-27 14:26:28 +02:00
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
|
|
|
|
|
using Umbraco.Core.Xml;
|
|
|
|
|
|
using Umbraco.Core.Xml.XPath;
|
|
|
|
|
|
using Umbraco.Web.PublishedCache.NuCache.Navigable;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.PublishedCache.NuCache
|
|
|
|
|
|
{
|
2016-11-05 12:22:57 +01:00
|
|
|
|
internal class MediaCache : PublishedCacheBase, IPublishedMediaCache, INavigableData, IDisposable
|
2016-05-27 14:26:28 +02:00
|
|
|
|
{
|
2017-07-12 14:09:31 +02:00
|
|
|
|
private readonly ContentStore.Snapshot _snapshot;
|
2019-01-17 11:01:23 +01:00
|
|
|
|
private readonly IAppCache _snapshotCache;
|
|
|
|
|
|
private readonly IAppCache _elementsCache;
|
2016-05-27 14:26:28 +02:00
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
public MediaCache(bool previewDefault, ContentStore.Snapshot snapshot, IAppCache snapshotCache, IAppCache elementsCache)
|
2016-05-27 14:26:28 +02:00
|
|
|
|
: base(previewDefault)
|
|
|
|
|
|
{
|
|
|
|
|
|
_snapshot = snapshot;
|
2016-05-30 19:54:36 +02:00
|
|
|
|
_snapshotCache = snapshotCache;
|
2017-10-31 12:48:24 +01:00
|
|
|
|
_elementsCache = elementsCache;
|
2016-05-27 14:26:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Get, Has
|
|
|
|
|
|
|
|
|
|
|
|
public override IPublishedContent GetById(bool preview, int contentId)
|
|
|
|
|
|
{
|
2017-12-07 13:22:32 +01:00
|
|
|
|
// ignore preview, there's only draft for media
|
2016-05-27 14:26:28 +02:00
|
|
|
|
var n = _snapshot.Get(contentId);
|
2019-01-28 14:15:47 +01:00
|
|
|
|
return n?.PublishedModel;
|
2016-05-27 14:26:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
|
public override IPublishedContent GetById(bool preview, Guid contentId)
|
|
|
|
|
|
{
|
2017-12-07 13:22:32 +01:00
|
|
|
|
// ignore preview, there's only draft for media
|
2016-11-05 12:22:57 +01:00
|
|
|
|
var n = _snapshot.Get(contentId);
|
2019-01-28 14:15:47 +01:00
|
|
|
|
return n?.PublishedModel;
|
2016-11-03 10:31:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-12 16:05:43 +02:00
|
|
|
|
public override IPublishedContent GetById(bool preview, Udi contentId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var guidUdi = contentId as GuidUdi;
|
|
|
|
|
|
if (guidUdi == null)
|
|
|
|
|
|
throw new ArgumentException($"Udi must be of type {typeof(GuidUdi).Name}.", nameof(contentId));
|
|
|
|
|
|
|
|
|
|
|
|
if (guidUdi.EntityType != Constants.UdiEntityType.Media)
|
|
|
|
|
|
throw new ArgumentException($"Udi entity type must be \"{Constants.UdiEntityType.Media}\".", nameof(contentId));
|
|
|
|
|
|
|
|
|
|
|
|
// ignore preview, there's only draft for media
|
|
|
|
|
|
var n = _snapshot.Get(guidUdi.Guid);
|
|
|
|
|
|
return n?.PublishedModel;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-27 14:26:28 +02:00
|
|
|
|
public override bool HasById(bool preview, int contentId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var n = _snapshot.Get(contentId);
|
|
|
|
|
|
return n != null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<IPublishedContent> GetAtRoot(bool preview)
|
|
|
|
|
|
{
|
2017-10-31 12:48:24 +01:00
|
|
|
|
if (PublishedSnapshotService.CacheContentCacheRoots == false)
|
2017-12-07 13:22:32 +01:00
|
|
|
|
return GetAtRootNoCache();
|
2016-05-27 14:26:28 +02:00
|
|
|
|
|
2017-10-31 12:48:24 +01:00
|
|
|
|
var cache = preview == false || PublishedSnapshotService.FullCacheWhenPreviewing
|
|
|
|
|
|
? _elementsCache
|
|
|
|
|
|
: _snapshotCache;
|
2016-05-27 14:26:28 +02:00
|
|
|
|
|
|
|
|
|
|
if (cache == null)
|
2017-12-07 13:22:32 +01:00
|
|
|
|
return GetAtRootNoCache();
|
2016-05-27 14:26:28 +02:00
|
|
|
|
|
|
|
|
|
|
// note: ToArray is important here, we want to cache the result, not the function!
|
2019-01-17 11:01:23 +01:00
|
|
|
|
return (IEnumerable<IPublishedContent>)cache.Get(
|
2017-12-07 13:22:32 +01:00
|
|
|
|
CacheKeys.MediaCacheRoots(false), // ignore preview, only 1 key!
|
|
|
|
|
|
() => GetAtRootNoCache().ToArray());
|
2016-05-27 14:26:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-07 13:22:32 +01:00
|
|
|
|
private IEnumerable<IPublishedContent> GetAtRootNoCache()
|
2016-05-27 14:26:28 +02:00
|
|
|
|
{
|
|
|
|
|
|
var c = _snapshot.GetAtRoot();
|
|
|
|
|
|
|
2017-12-07 13:22:32 +01:00
|
|
|
|
// ignore preview, there's only draft for media
|
2019-01-28 14:15:47 +01:00
|
|
|
|
return c.Select(n => n.PublishedModel);
|
2016-05-27 14:26:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool HasContent(bool preview)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _snapshot.IsEmpty == false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region XPath
|
|
|
|
|
|
|
|
|
|
|
|
public override IPublishedContent GetSingleByXPath(bool preview, string xpath, XPathVariable[] vars)
|
|
|
|
|
|
{
|
|
|
|
|
|
var navigator = CreateNavigator(preview);
|
|
|
|
|
|
var iterator = navigator.Select(xpath, vars);
|
|
|
|
|
|
return GetSingleByXPath(iterator);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IPublishedContent GetSingleByXPath(bool preview, XPathExpression xpath, XPathVariable[] vars)
|
|
|
|
|
|
{
|
|
|
|
|
|
var navigator = CreateNavigator(preview);
|
|
|
|
|
|
var iterator = navigator.Select(xpath, vars);
|
|
|
|
|
|
return GetSingleByXPath(iterator);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IPublishedContent GetSingleByXPath(XPathNodeIterator iterator)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iterator.MoveNext() == false) return null;
|
|
|
|
|
|
|
|
|
|
|
|
var xnav = iterator.Current as NavigableNavigator;
|
|
|
|
|
|
if (xnav == null) return null;
|
|
|
|
|
|
|
|
|
|
|
|
var xcontent = xnav.UnderlyingObject as NavigableContent;
|
|
|
|
|
|
return xcontent == null ? null : xcontent.InnerContent;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<IPublishedContent> GetByXPath(bool preview, string xpath, XPathVariable[] vars)
|
|
|
|
|
|
{
|
|
|
|
|
|
var navigator = CreateNavigator(preview);
|
|
|
|
|
|
var iterator = navigator.Select(xpath, vars);
|
|
|
|
|
|
return GetByXPath(iterator);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<IPublishedContent> GetByXPath(bool preview, XPathExpression xpath, XPathVariable[] vars)
|
|
|
|
|
|
{
|
|
|
|
|
|
var navigator = CreateNavigator(preview);
|
|
|
|
|
|
var iterator = navigator.Select(xpath, vars);
|
|
|
|
|
|
return GetByXPath(iterator);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<IPublishedContent> GetByXPath(XPathNodeIterator iterator)
|
|
|
|
|
|
{
|
|
|
|
|
|
while (iterator.MoveNext())
|
|
|
|
|
|
{
|
|
|
|
|
|
var xnav = iterator.Current as NavigableNavigator;
|
|
|
|
|
|
if (xnav == null) continue;
|
|
|
|
|
|
|
|
|
|
|
|
var xcontent = xnav.UnderlyingObject as NavigableContent;
|
|
|
|
|
|
if (xcontent == null) continue;
|
|
|
|
|
|
|
|
|
|
|
|
yield return xcontent.InnerContent;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override XPathNavigator CreateNavigator(bool preview)
|
|
|
|
|
|
{
|
|
|
|
|
|
var source = new Source(this, preview);
|
|
|
|
|
|
var navigator = new NavigableNavigator(source);
|
|
|
|
|
|
return navigator;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override XPathNavigator CreateNodeNavigator(int id, bool preview)
|
|
|
|
|
|
{
|
|
|
|
|
|
var source = new Source(this, preview);
|
|
|
|
|
|
var navigator = new NavigableNavigator(source);
|
|
|
|
|
|
return navigator.CloneWithNewRoot(id, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Content types
|
|
|
|
|
|
|
2019-04-15 13:04:14 +02:00
|
|
|
|
public override IPublishedContentType GetContentType(int id)
|
2016-05-27 14:26:28 +02:00
|
|
|
|
{
|
|
|
|
|
|
return _snapshot.GetContentType(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-15 13:04:14 +02:00
|
|
|
|
public override IPublishedContentType GetContentType(string alias)
|
2016-05-27 14:26:28 +02:00
|
|
|
|
{
|
|
|
|
|
|
return _snapshot.GetContentType(alias);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region IDisposable
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
_snapshot.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|