MenuCaching snippet for MODX Revolution

The idea of ​​a snippet is very simple. It is often required to make a menu on a site that remains unchanged on all pages. Only the CSS class of the active menu item changes. Why do we need to make queries in the database on each page and generate a menu if we only need to change the CSS class? And if this is a large menu with drop-down sublevels, too many resources can be spent on its generation. The menuCaching snippet allows you to save the server from this extra work.



Snippet description

The snippet allows you to not generate a menu for each page separately just in order to put the CSS class on the active menu item.
A menu is generated only once for the entire site, but active items are marked.

Usage example


[[menuCaching?
&snippetToCache=`Wayfinder`
&cacheSuffix=`top_menu`
&contexts=`web`
&startId=`0`
&level=`1`
&hereClass=`active`
&rowTpl=`@CODE: 
  • [[+wf.linktext]]
  • ` ]]


    It is recommended to create a set of parameters (for example, "top_menu") in order, again, to save the MODX parser from unnecessary work:

    [[menuCaching@top_menu]]


    What happens here, I think, you already guessed. The menu is stored in the cache as follows:



    It remains only to replace labels of type {{active1}} with the class of the active element, and remove the rest, which does the snippet. The snippet kit includes a plugin that clears the cache when you click the "Update Site" button in the admin panel.

    An important snippet parameter is cacheSuffix . In the example, a cache is created for the entire site, but you can create a cache for individual parent categories, and inside one category everything will be taken from the cache:

    &cacheSuffix=`[[*parent]]`


    This snippet can be used not only for menus, but for example for different sliders, etc. blocks that the site does not change on different pages.

    Snippet menuCaching repository the MODX: http://modx.com/extras/package/menucaching

    Added

    test results with a large menu. The calls of both snippets are uncached, i.e. [[! snippet]] (for the test, in reality, you need to cache).

    Wayfinder
    The first call to any of the pages where there is a menu: 2.5815 sec.
    Go to any other page with the same menu: 2.4566 sec. (i.e. praised cacheResults does not help) Refreshing the
    page: 1.0021 sec. (only in this case cacheResults saves)

    The same Wayfinder, through menuCaching.
    The first call to any of the pages where there is a menu: 2.4842 sec. (within the margin of error, of course there is no difference)
    Go to any other page with the same menu: 0.7008 sec. (the result is the same, but the time is different) We refresh the
    page: 0.4187 sec.

    Also popular now: