Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan 1243 posts 3708 karma points admin c-trib
    Mar 17, 2019 @ 14:03
    Dan
    0

    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?

    Many thanks.

  • David Brendel 749 posts 2816 karma points c-trib
    Mar 17, 2019 @ 18:49
    David Brendel
    0

    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.

  • Søren Gregersen 299 posts 1182 karma points MVP c-trib
    Mar 17, 2019 @ 21:20
    Søren Gregersen
    0

    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.

  • Dan 1243 posts 3708 karma points admin c-trib
    Mar 17, 2019 @ 22:03
    Dan
    1

    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.

  • Søren Gregersen 299 posts 1182 karma points MVP c-trib
    Mar 17, 2019 @ 22:13
    Søren Gregersen
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft