We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11681
    • 98 Posts
    Apologies if the following already has been addressed in the MODX forums. I could not find my particular Q&A there, nor in the usual Bob / Mary / Mark etc. MODX guru sites.

    I have created a getResources call that provides the desired result, and with satisfactory performance. However, I worry that responsiveness will suffer as the number of resources grows.

    I can't cache the call naïvely because it returns a result that is time-dependent.

    The call pulls a list of Event resources. It uses a TV, eventDate, to order the list. The "tvFilters" and "limit" arguments work together to discard all but one resource, the Event whose eventDate indicates it is to occur next in the future.

    I imagine that the uncached getResources call (See below.) must do a ton of work in order to return that single Event. It's a shame not to cache it, considering that the call's result doesn't change very often. Yet I don't see how to guarantee the correct result unless the call runs uncached.

    I'd like to "pay" for pulling and sorting the Events just once (i.e., cache that intermediate result), then compute uncached the sole result Event.

    Improvement ideas anyone?

    Here's the code:
                [[!getResources:default=`<h3>
                  Information on future events is currently unavailable.  Please check again later.
                  </h3>`?
                  &parents=`6`
                  &showUnpublished=`1`
                  &showDeleted=`0`
                  &includeContent=`1`
                  &tvPrefix=``
                  &includeTVs=`1`
                  &processTVs=`1`
                  &tvFilters=`eventDate>>[[currentDatetime]]`
                  &sortbyTVType=`datetime`
                  &sortbyTV=`eventDate`
                  &sortdirTV=`ASC`
                  &limit= `2`
                  &tpl_1= `home_news_or_event_RenderTpl`
                  &tpl_2= `homeMainEventsLinkTpl`
                 ]]


    BTW, the use of
    &limit= `2`
    may seem strange. It and the "default" argument form a hack to cope with the possibility that there may not be any yet-to-occur Events among the targeted resources.
      I looked just like that in 1964.
    • I would wrap your getResources call with your own Snippet and use the runSnippet() method to execute (see http://rtfm.modx.com/display/revolution20/modX.runSnippet). Sometimes, for really complex lookups like the one you described, I end up just using xPDO instead of getResources: sometimes it's faster for me to handle the query myself since getResources casts a wider net.

      Then you can "remember" your results for any amount of time using the MODX cache manager. See this quick tutorial: http://rtfm.modx.com/display/xPDO20/Caching+Tutorial+-+Lifetimes

      Hope that helps.