Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid duplicate query for mobile menu #40

Open
kauaicreative opened this issue Jan 30, 2021 · 2 comments
Open

Avoid duplicate query for mobile menu #40

kauaicreative opened this issue Jan 30, 2021 · 2 comments
Milestone

Comments

@kauaicreative
Copy link

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 TeemuSuoranta added this to the 6.1.0 milestone Jan 31, 2021
@TeemuSuoranta
Copy link
Contributor

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants