Web.PublishedCache - refactor contextual caches & inner caches management

This commit is contained in:
Stephan
2013-03-31 18:44:29 -02:00
parent 70ad3edc1f
commit b89951dd11
5 changed files with 123 additions and 47 deletions

View File

@@ -14,12 +14,14 @@ namespace Umbraco.Web.PublishedCache
internal abstract class ContextualPublishedCache
{
protected readonly UmbracoContext UmbracoContext;
private readonly IPublishedCache _cache;
protected ContextualPublishedCache(UmbracoContext umbracoContext, IPublishedCache cache)
/// <summary>
/// Initializes a new instance of the <see cref="ContextualPublishedCache"/> with a context.
/// </summary>
/// <param name="umbracoContext">The context.</param>
protected ContextualPublishedCache(UmbracoContext umbracoContext)
{
UmbracoContext = umbracoContext;
_cache = cache;
}
/// <summary>
@@ -27,19 +29,13 @@ namespace Umbraco.Web.PublishedCache
/// </summary>
/// <param name="contentId">The content unique identifier.</param>
/// <returns>The content, or null.</returns>
public virtual IPublishedContent GetById(int contentId)
{
return _cache.GetById(UmbracoContext, contentId);
}
public abstract IPublishedContent GetById(int contentId);
/// <summary>
/// Gets contents at root.
/// </summary>
/// <returns>The contents.</returns>
public virtual IEnumerable<IPublishedContent> GetAtRoot()
{
return _cache.GetAtRoot(UmbracoContext);
}
public abstract IEnumerable<IPublishedContent> GetAtRoot();
/// <summary>
/// Gets a content resulting from an XPath query.
@@ -52,10 +48,7 @@ namespace Umbraco.Web.PublishedCache
/// value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
/// </remarks>
public virtual IPublishedContent GetSingleByXPath(string xpath, params XPathVariable[] vars)
{
return _cache.GetSingleByXPath(UmbracoContext, xpath, vars);
}
public abstract IPublishedContent GetSingleByXPath(string xpath, params XPathVariable[] vars);
/// <summary>
/// Gets contents resulting from an XPath query.
@@ -68,27 +61,18 @@ namespace Umbraco.Web.PublishedCache
/// value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
/// </remarks>
public virtual IEnumerable<IPublishedContent> GetByXPath(string xpath, params XPathVariable[] vars)
{
return _cache.GetByXPath(UmbracoContext, xpath, vars);
}
public abstract IEnumerable<IPublishedContent> GetByXPath(string xpath, params XPathVariable[] vars);
/// <summary>
/// Gets an XPath navigator that can be used to navigate contents.
/// </summary>
/// <returns>The XPath navigator.</returns>
public virtual XPathNavigator GetXPathNavigator()
{
return _cache.GetXPathNavigator(UmbracoContext);
}
public abstract XPathNavigator GetXPathNavigator();
/// <summary>
/// Gets a value indicating whether the underlying non-contextual cache contains published content.
/// </summary>
/// <returns>A value indicating whether the underlying non-contextual cache contains published content.</returns>
public virtual bool HasContent()
{
return _cache.HasContent();
}
public abstract bool HasContent();
}
}

View File

@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using Umbraco.Core.Models;
using Umbraco.Core.Xml;
namespace Umbraco.Web.PublishedCache
{
/// <summary>
/// Provides access to cached contents in a specified context.
/// </summary>
/// <typeparam name="T">The type of the underlying published cache.</typeparam>
internal abstract class ContextualPublishedCache<T> : ContextualPublishedCache
where T : IPublishedCache
{
private readonly T _cache;
/// <summary>
/// Initializes a new instance of the <see cref="ContextualPublishedCache{T}"/> with a context and a published cache.
/// </summary>
/// <param name="umbracoContext">The context.</param>
/// <param name="cache">The cache.</param>
protected ContextualPublishedCache(UmbracoContext umbracoContext, T cache)
: base(umbracoContext)
{
_cache = cache;
}
/// <summary>
/// Gets the underlying published cache.
/// </summary>
public T InnerCache { get { return _cache; } }
/// <summary>
/// Gets a content identified by its unique identifier.
/// </summary>
/// <param name="contentId">The content unique identifier.</param>
/// <returns>The content, or null.</returns>
public override IPublishedContent GetById(int contentId)
{
return _cache.GetById(UmbracoContext, contentId);
}
/// <summary>
/// Gets contents at root.
/// </summary>
/// <returns>The contents.</returns>
public override IEnumerable<IPublishedContent> GetAtRoot()
{
return _cache.GetAtRoot(UmbracoContext);
}
/// <summary>
/// Gets a content resulting from an XPath query.
/// </summary>
/// <param name="xpath">The XPath query.</param>
/// <param name="vars">Optional XPath variables.</param>
/// <returns>The content, or null.</returns>
/// <remarks>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single
/// value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
/// </remarks>
public override IPublishedContent GetSingleByXPath(string xpath, params XPathVariable[] vars)
{
return _cache.GetSingleByXPath(UmbracoContext, xpath, vars);
}
/// <summary>
/// Gets contents resulting from an XPath query.
/// </summary>
/// <param name="xpath">The XPath query.</param>
/// <param name="vars">Optional XPath variables.</param>
/// <returns>The contents.</returns>
/// <remarks>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single
/// value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
/// </remarks>
public override IEnumerable<IPublishedContent> GetByXPath(string xpath, params XPathVariable[] vars)
{
return _cache.GetByXPath(UmbracoContext, xpath, vars);
}
/// <summary>
/// Gets an XPath navigator that can be used to navigate contents.
/// </summary>
/// <returns>The XPath navigator.</returns>
public override XPathNavigator GetXPathNavigator()
{
return _cache.GetXPathNavigator(UmbracoContext);
}
/// <summary>
/// Gets a value indicating whether the underlying non-contextual cache contains published content.
/// </summary>
/// <returns>A value indicating whether the underlying non-contextual cache contains published content.</returns>
public override bool HasContent()
{
return _cache.HasContent(UmbracoContext);
}
}
}

View File

@@ -9,10 +9,8 @@ namespace Umbraco.Web.PublishedCache
/// <summary>
/// Provides access to cached documents in a specified context.
/// </summary>
internal class ContextualPublishedContentCache : ContextualPublishedCache
internal class ContextualPublishedContentCache : ContextualPublishedCache<IPublishedContentCache>
{
private readonly IPublishedContentCache _cache;
/// <summary>
/// Initializes a new instance of the <see cref="ContextualPublishedContentCache"/> class with a published content cache and a context.
/// </summary>
@@ -20,15 +18,7 @@ namespace Umbraco.Web.PublishedCache
/// <param name="umbracoContext">A context.</param>
public ContextualPublishedContentCache(IPublishedContentCache cache, UmbracoContext umbracoContext)
: base(umbracoContext, cache)
{
_cache = cache;
}
/// <summary>
/// Gets the inner IPublishedContentCache.
/// </summary>
/// <remarks>For unit tests.</remarks>
internal IPublishedContentCache InnerCache { get { return _cache; } }
{ }
/// <summary>
/// Gets content identified by a route.
@@ -39,7 +29,7 @@ namespace Umbraco.Web.PublishedCache
/// <remarks>A valid route is either a simple path eg <c>/foo/bar/nil</c> or a root node id and a path, eg <c>123/foo/bar/nil</c>.</remarks>
public IPublishedContent GetByRoute(string route, bool? hideTopLevelNode = null)
{
return _cache.GetByRoute(UmbracoContext, route, hideTopLevelNode);
return InnerCache.GetByRoute(UmbracoContext, route, hideTopLevelNode);
}
/// <summary>
@@ -49,7 +39,7 @@ namespace Umbraco.Web.PublishedCache
/// <returns>The route.</returns>
public string GetRouteById(int contentId)
{
return _cache.GetRouteById(UmbracoContext, contentId);
return InnerCache.GetRouteById(UmbracoContext, contentId);
}
}
}

View File

@@ -9,10 +9,8 @@ namespace Umbraco.Web.PublishedCache
/// <summary>
/// Provides access to cached medias in a specified context.
/// </summary>
internal class ContextualPublishedMediaCache : ContextualPublishedCache
internal class ContextualPublishedMediaCache : ContextualPublishedCache<IPublishedMediaCache>
{
private readonly IPublishedMediaCache _cache;
/// <summary>
/// Initializes a new instance of the <see cref="ContextualPublishedMediaCache"/> class with a published media cache and a context.
/// </summary>
@@ -20,8 +18,6 @@ namespace Umbraco.Web.PublishedCache
/// <param name="umbracoContext">A context.</param>
public ContextualPublishedMediaCache(IPublishedMediaCache cache, UmbracoContext umbracoContext)
: base(umbracoContext, cache)
{
_cache = cache;
}
{ }
}
}