Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Made url caching lazy-load content item

  • Loading branch information...
commit c185363a2c3bd4f0eba6a96a458bffcea88fe408 1 parent 77f0739
@libardo libardo authored
View
13 src/Framework/N2/Web/CachingUrlParserDecorator.cs
@@ -133,15 +133,16 @@ public PathData ResolvePath(Url url, ContentItem startNode = null, string remain
PathData data;
if (cachedPathData.TryGetValue(urlKey, out data))
{
+ data = data.Attach(persister);
if (data == null || data.ID == 0)
{
// Cached path has to CMS content
- return PathData.Empty;
+ return data;
}
logger.Debug("Retrieving " + url + " from cache");
- data = data.Attach(persister);
- data.UpdateParameters(Url.Parse(url).GetQueries());
+ if (!string.IsNullOrEmpty(url.Query))
+ data.UpdateParameters(Url.Parse(url).GetQueries());
}
else
{
@@ -155,11 +156,9 @@ public PathData ResolvePath(Url url, ContentItem startNode = null, string remain
string path = remainingPath;
var pathData = GetStartNode(url, cachedPathData, ref path, 0);
- var contentItem = persister.Get(pathData.ID);
-
data = pathData.ID == 0
- ? inner.ResolvePath(url)
- : inner.ResolvePath(url, contentItem, remainingPath.Replace(path, ""));
+ ? inner.ResolvePath(url)
+ : inner.ResolvePath(url, persister.Get(pathData.ID), remainingPath.Substring(path.Length, remainingPath.Length - path.Length));
if (data.IsCacheable)
{
View
49 src/Framework/N2/Web/PathData.cs
@@ -74,6 +74,8 @@ public static string SelectedQueryKey
ContentItem currentPage;
ContentItem currentItem;
+ ContentItem stopItem;
+ Persistence.IPersister persister;
public PathData(ContentItem item, string templateUrl, string action, string arguments)
: this()
@@ -116,27 +118,23 @@ public PathData()
/// <summary>The item behind this path.</summary>
public ContentItem CurrentItem
{
- get { return currentItem; }
- set
- {
- currentItem = value;
- ID = value != null ? value.ID : 0;
- }
+ get { return Get(ref currentItem, ID); }
+ set { ID = Set(ref currentItem, value); }
}
/// <summary>The page behind this path (might differ from CurrentItem when the path leads to a part).</summary>
public ContentItem CurrentPage
{
- get { return currentPage ?? CurrentItem; }
- set
- {
- currentPage = value;
- PageID = value != null ? value.ID : 0;
- }
+ get { return Get(ref currentPage, PageID) ?? CurrentItem; }
+ set { PageID = Set(ref currentPage, value); }
}
/// <summary>The item reporting that the path isn't a match.</summary>
- public ContentItem StopItem { get; set; }
+ public ContentItem StopItem
+ {
+ get { return Get(ref stopItem, StopID); }
+ set { StopID = Set(ref stopItem, value); }
+ }
/// <summary>The template handling this path.</summary>
public string TemplateUrl { get; set; }
@@ -147,6 +145,9 @@ public ContentItem CurrentPage
/// <summary>The identifier of the content page behind this path.</summary>
public int PageID { get; set; }
+ /// <summary>The identifier of the content page this path data originates from.</summary>
+ public int StopID { get; set; }
+
/// <summary>?</summary>
public string Path { get; set; }
@@ -223,7 +224,8 @@ public virtual PathData Detach()
// clear persistent objects before caching
data.currentItem = null;
data.currentPage = null;
- data.StopItem = null;
+ data.stopItem = null;
+ data.persister = null;
return data;
}
@@ -236,9 +238,7 @@ public virtual PathData Attach(N2.Persistence.IPersister persister)
// reload persistent objects and clone non-immutable objects
data.QueryParameters = new Dictionary<string, string>(QueryParameters);
- data.CurrentItem = persister.Repository.Get(ID);
- if (PageID != 0)
- data.CurrentPage = persister.Repository.Get(PageID);
+ data.persister = persister;
return data;
}
@@ -255,5 +255,20 @@ public virtual bool IsEmpty()
{
return CurrentItem == null;
}
+
+ private ContentItem Get(ref ContentItem item, int id)
+ {
+ if (item != null)
+ return item;
+ if (persister != null && id != 0)
+ return item = persister.Get(id);
+ return null;
+ }
+
+ private int Set(ref ContentItem currentPage, ContentItem value)
+ {
+ currentPage = value;
+ return value != null ? value.ID : 0;
+ }
}
}
View
2  src/Framework/Tests/Web/UrlParsing/CachingUrlParserTests.cs
@@ -90,6 +90,8 @@ public void CanCache_ResolvedTemplate()
var data = parser.ResolvePath("/item1/item1_1");
+ var forcesLazyLoadButOtherwizeIgnored = data.CurrentPage;
+
Assert.That(repository.lastOperation, Is.EqualTo("Get(3)"), "Should have loaded the parsed item directly.");
Assert.That(data.CurrentItem, Is.EqualTo(item1_1));
}
Please sign in to comment.
Something went wrong with that request. Please try again.