Content helper in v8 to query and cache e.g. blog posts
Hi,
I have a few templates/partials on my site which need to access the same blog posts. Rather than querying for the blog posts in each template individually I'd like to create a helper class which looks them up once and caches the result so I can call it in my templates like:
var blogPosts = MyCode.MyContentHelper.GetBlogPosts();
What would be the recommended approach to do this in v8?
I suppose you sort the posts, and then take the 3 last published.
Depending on how much traffic your site gets, I would’nt bother with a cache.
With that said, if your site suddenly has a thousand blogposts your query could get slower. A way to get around this is to cache the IDs of the posts, and then query the cache for the specific posts. The query could then be done with examine to speed things up.
Okay, so forgetting about the cache, and running a query against the full content cache to return just the blog posts, potentially several times per page request, what would be the recommended way to do just that part in v8, from a custom Class (i.e. not from within a view)? Typically in v7 you'd use UmbracoHelper with the current context, like this:
public static IOrderedEnumerable<IPublishedContent> GetBlogPosts()
{
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
return umbracoHelper.TypedContentAtXPath("//blogPost[@isDoc and umbracoNaviHide != '1']").OrderByDescending(x => x.GetPropertyValue<DateTime>("date"));
}
I'm struggling to find the recommended approach for doing something like this in the v8 docs.
First of all, pass the umbracoHelper in as a parameter. You already have it where you call that method.
I would suggest not using xpath, since it will use some conversion internalisering that will redouce performance.
For this specific question, v7 and v8 works sort of the same, with the xpath not being able to navigate a “Real” XML tree, but having to construct one / rewrite the query, to do the same work. Using examine would be a better performing solution, especially for solutions with alot of content.
Content helper in v8 to query and cache e.g. blog posts
Hi,
I have a few templates/partials on my site which need to access the same blog posts. Rather than querying for the blog posts in each template individually I'd like to create a helper class which looks them up once and caches the result so I can call it in my templates like:
What would be the recommended approach to do this in v8?
Many thanks.
Hi Dan,
Ich wouldn't cache them again as they are already cached. Introducing your own Cache only results in overhead invalidating the Cache.
Writting a custom wrapper for the same query is fine enough.
I suppose you sort the posts, and then take the 3 last published.
Depending on how much traffic your site gets, I would’nt bother with a cache.
With that said, if your site suddenly has a thousand blogposts your query could get slower. A way to get around this is to cache the IDs of the posts, and then query the cache for the specific posts. The query could then be done with examine to speed things up.
Okay, so forgetting about the cache, and running a query against the full content cache to return just the blog posts, potentially several times per page request, what would be the recommended way to do just that part in v8, from a custom Class (i.e. not from within a view)? Typically in v7 you'd use
UmbracoHelper
with the current context, like this:I'm struggling to find the recommended approach for doing something like this in the v8 docs.
First of all, pass the umbracoHelper in as a parameter. You already have it where you call that method.
I would suggest not using xpath, since it will use some conversion internalisering that will redouce performance.
For this specific question, v7 and v8 works sort of the same, with the xpath not being able to navigate a “Real” XML tree, but having to construct one / rewrite the query, to do the same work. Using examine would be a better performing solution, especially for solutions with alot of content.
Hi Dan,
I'm looking to achieve a similar thing, which approach did you take?
Many thanks Paul
is working on a reply...