generaxion / axio-starter

Superior WordPress starter theme with modern build tools by Generaxion (previously Aucor Starter). 250+ hours of development over 6 years to make the greatest starting point for WordPress site.
https://axio.generax.io
GNU General Public License v2.0
166 stars 25 forks source link

Avoid duplicate query for mobile menu #40

Open kauaicreative opened 3 years ago

kauaicreative commented 3 years ago

Here is my fix:

class Aucor_Menu_Primary extends Aucor_Component {

  public static $cache;

  public static function frontend($data) {
    ?>

    <nav <?php parent::render_attributes($data['attr']); ?>>

      <?php
      if (!self::$cache) self::$cache = wp_nav_menu([
        'theme_location' => 'primary',
        'container'      => '',
        'menu_class'     => 'primary-navigation__items',
        'link_before'    => '',
        'link_after'     => '',
        'fallback_cb'    => '',
        'echo'           => false,
      ]);

      echo self::$cache;
      ?>
...
TeemuSuoranta commented 3 years ago

I've been concidering different ways of adding cache support for components in general as there could be need to:

a) cache response for the same request b) cache response in transient for a short time

For the same request version static variable might be a good solution as the variable scope is more protected than globals but gives a similar flexibility. To keep track of variations of same component you could json encode the $args variable as it should most of the time hold the changing bits of the component (of course there are some "not so dumb" components like hero that uses the context insted of just arguments).

For the generic solution we could either have some reserved arguments like '__cache-transient' => 15 * MINUTE_IN_SECONDS or use PHP7 chaining like Component::transient(['expire' => 3600])::render(['foo' => 'bar]);

So I'd like to fix this issue after implementing a generic solution to caching and in the mean time something like that does work. WP does have some built-in DB caching that might actually prevent doing double DB queries already (would need to be tested) and if there is some kind of page cache in place, this should really cause much issues although not optimal.

kauaicreative commented 3 years ago

Good ideas! And learning about the wp_cache_get() and set functions right now.