Working on: U4-5711 Media cache valueDictionary exception

This commit is contained in:
Shannon
2014-11-18 12:23:32 +11:00
parent c47d59a257
commit f3f6504398
3 changed files with 69 additions and 42 deletions

View File

@@ -192,6 +192,10 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache
}
}
LogHelper.Debug<PublishedMediaCache>(
"Could not retrieve media {0} from Examine index, reverting to looking up media via legacy library.GetMedia method",
() => id);
var media = global::umbraco.library.GetMedia(id, true);
if (media != null && media.Current != null)
{
@@ -213,6 +217,10 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache
return ConvertFromXPathNavigator(media.Current);
}
LogHelper.Debug<PublishedMediaCache>(
"Could not retrieve media {0} from Examine index or from legacy library.GetMedia method",
() => id);
return null;
}

View File

@@ -38,7 +38,7 @@ namespace Umbraco.Web
public IEnumerable<IPublishedContent> TypedContent(IEnumerable<int> ids)
{
return TypedDocumentsbyIds(_contentCache, ids);
return TypedDocumentsByIds(_contentCache, ids);
}
public IEnumerable<IPublishedContent> TypedContentAtXPath(string xpath, params XPathVariable[] vars)
@@ -102,7 +102,7 @@ namespace Umbraco.Web
public IEnumerable<IPublishedContent> TypedMedia(IEnumerable<int> ids)
{
return TypedDocumentsbyIds(_mediaCache, ids);
return TypedDocumentsByIds(_mediaCache, ids);
}
public IEnumerable<IPublishedContent> TypedMediaAtRoot()
@@ -148,7 +148,7 @@ namespace Umbraco.Web
// return doc;
//}
private IEnumerable<IPublishedContent> TypedDocumentsbyIds(ContextualPublishedCache cache, IEnumerable<int> ids)
private IEnumerable<IPublishedContent> TypedDocumentsByIds(ContextualPublishedCache cache, IEnumerable<int> ids)
{
return ids.Select(eachId => TypedDocumentById(eachId, cache));
}

View File

@@ -9,6 +9,7 @@ using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using Newtonsoft.Json;
using Umbraco.Core;
@@ -16,21 +17,23 @@ using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web;
using Umbraco.Web.Cache;
using Umbraco.Web.Templates;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.media;
using umbraco.cms.businesslogic.member;
using umbraco.cms.businesslogic.propertytype;
using umbraco.cms.businesslogic.relation;
using umbraco.cms.businesslogic.web;
using umbraco.cms.helpers;
using umbraco.scripting;
using umbraco.DataLayer;
using umbraco.cms.businesslogic.language;
using Umbraco.Core.IO;
using Language = umbraco.cms.businesslogic.language.Language;
using Media = umbraco.cms.businesslogic.media.Media;
using Member = umbraco.cms.businesslogic.member.Member;
using PropertyType = umbraco.cms.businesslogic.propertytype.PropertyType;
using Relation = umbraco.cms.businesslogic.relation.Relation;
using UmbracoContext = umbraco.presentation.UmbracoContext;
namespace umbraco
@@ -490,46 +493,54 @@ namespace umbraco
{
if (UmbracoConfig.For.UmbracoSettings().Content.UmbracoLibraryCacheDuration > 0)
{
XPathNodeIterator retVal = ApplicationContext.Current.ApplicationCache.GetCacheItem(
var xml = ApplicationContext.Current.ApplicationCache.GetCacheItem(
string.Format(
"{0}_{1}_{2}", CacheKeys.MediaCacheKey, MediaId, Deep),
TimeSpan.FromSeconds(UmbracoConfig.For.UmbracoSettings().Content.UmbracoLibraryCacheDuration),
() => GetMediaDo(MediaId, Deep));
if (retVal != null)
return retVal;
if (xml != null)
{
return FromXElement(xml, xml.Attribute("nodeTypeAlias").Value);
}
}
else
{
return GetMediaDo(MediaId, Deep);
var xml = GetMediaDo(MediaId, Deep);
return FromXElement(xml, xml.Attribute("nodeTypeAlias").Value);
}
}
catch
catch(Exception ex)
{
LogHelper.Error<library>("An error occurred looking up media", ex);
}
XmlDocument xd = new XmlDocument();
LogHelper.Debug<library>("No media result for id {0}", () => MediaId);
var xd = new XmlDocument();
xd.LoadXml(string.Format("<error>No media is maching '{0}'</error>", MediaId));
return xd.CreateNavigator().Select("/");
}
private static XPathNodeIterator GetMediaDo(int mediaId, bool deep)
private static XElement GetMediaDo(int mediaId, bool deep)
{
var m = new Media(mediaId);
if (m.nodeObjectType == Media._objectType)
{
var mXml = new XmlDocument();
var xml = m.ToXml(mXml, deep);
//This will be null if the media isn't public (meaning it is in the trash)
if (xml == null) return null;
//TODO: This is an aweful way of loading in XML - it is very slow.
mXml.LoadXml(xml.OuterXml);
var xp = mXml.CreateNavigator();
var xpath = UmbracoConfig.For.UmbracoSettings().Content.UseLegacyXmlSchema ? "/node" : String.Format("/{0}", Casing.SafeAliasWithForcingCheck(m.ContentType.Alias));
return xp.Select(xpath);
}
return null;
var media = ApplicationContext.Current.Services.MediaService.GetById(mediaId);
if (media == null) return null;
var serializer = new EntityXmlSerializer();
var serialized = serializer.Serialize(
ApplicationContext.Current.Services.MediaService, ApplicationContext.Current.Services.DataTypeService,
ApplicationContext.Current.Services.UserService, media, deep);
return serialized;
}
private static XPathNodeIterator FromXElement(XNode xml, string mediaContentType)
{
var xp = xml.CreateNavigator();
var xpath = UmbracoConfig.For.UmbracoSettings().Content.UseLegacyXmlSchema
? "/node"
: String.Format("/{0}", Casing.SafeAliasWithForcingCheck(mediaContentType));
return xp.Select(xpath);
}
/// <summary>
@@ -545,35 +556,42 @@ namespace umbraco
{
if (UmbracoConfig.For.UmbracoSettings().Content.UmbracoLibraryCacheDuration > 0)
{
var retVal = ApplicationContext.Current.ApplicationCache.GetCacheItem(
var xml = ApplicationContext.Current.ApplicationCache.GetCacheItem(
string.Format(
"{0}_{1}", CacheKeys.MemberLibraryCacheKey, MemberId),
TimeSpan.FromSeconds(UmbracoConfig.For.UmbracoSettings().Content.UmbracoLibraryCacheDuration),
() => GetMemberDo(MemberId));
if (retVal != null)
return retVal.CreateNavigator().Select("/");
if (xml != null)
{
return xml.CreateNavigator().Select("/");
}
}
else
{
return GetMemberDo(MemberId).CreateNavigator().Select("/");
}
}
catch
catch (Exception ex)
{
LogHelper.Error<library>("An error occurred looking up member", ex);
}
XmlDocument xd = new XmlDocument();
LogHelper.Debug<library>("No member result for id {0}", () => MemberId);
var xd = new XmlDocument();
xd.LoadXml(string.Format("<error>No member is maching '{0}'</error>", MemberId));
return xd.CreateNavigator().Select("/");
}
private static XmlDocument GetMemberDo(int MemberId)
private static XElement GetMemberDo(int MemberId)
{
Member m = new Member(MemberId);
XmlDocument mXml = new XmlDocument();
mXml.LoadXml(m.ToXml(mXml, false).OuterXml);
return mXml;
var member = ApplicationContext.Current.Services.MemberService.GetById(MemberId);
if (member == null) return null;
var serializer = new EntityXmlSerializer();
var serialized = serializer.Serialize(
ApplicationContext.Current.Services.DataTypeService, member);
return serialized;
}
/// <summary>
@@ -1365,8 +1383,9 @@ namespace umbraco
nav.MoveToId(HttpContext.Current.Items["pageID"].ToString());
return nav.Select(".");
}
catch
catch (Exception ex)
{
LogHelper.Error<library>("Could not retrieve current xml node", ex);
}
XmlDocument xd = new XmlDocument();