R
R
River00512019-05-14 20:43:40
PHP
River0051, 2019-05-14 20:43:40

How can I cache only part of the html code on the page?

Good afternoon, help me solve the problem, I don’t know what to do anymore.
There are a number of pages on which quite a lot of actions are performed, both in a cycle and in queries to the database, page generation can take up to 7 seconds. I turned on Redis on the hosting and installed the W3 Total Cache plugin, turned on the creation of the page cache for everyone except authorized ones, the problem disappeared, because there should not have been ordinary users according to the technical specification, then it was necessary to connect comments and here problems began, for correct operation it was necessary turn off the cache for authorized users and then everything works, the cache is reset by itself when adding a comment, but then the page for such users loads up to 7 seconds
If you enable the cache for authorized non-admins, then everything seems to be normal, except that if an authorized user is the first to enter and generate a page, then a guest without authorization sees the block of the one who generated the cache
. The only thing that comes to mind is to somehow cache only 1 block on the page, which is generated up to 7 seconds, I read somewhere that this is possible, but I could not find it, please help

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Stalker_RED, 2019-05-14
@River0051

$key = "my_heavy_part";

if ($redis->exists($key)) {
  return $redis->get($key);
} else {
  // вычисляете свой кусок, затем
  $redis->set($key, $data);
  $redis->expire($key, 3600); // expires in 1 hour
}

D
Dmitry, 2019-05-14
@dimasmagadan


WordPress has https://codex.wordpress.org/Transients_API for this purpose,
better use it

T
terka43, 2019-05-15
@terka43

And why can't you send html without "long-playing" pieces, and pull up these ajax pieces when ready?

R
River0051, 2019-05-15
@River0051

I did it and everything worked

function get20($bild_20,$pname)
{
  global $wpdb;
  $current_user = wp_get_current_user();
  ob_start();
  foreach( $bild_20 as $_bild_20 ) { include '20n.php';  }
  return ob_get_clean();
}
$redis = new Redis();
// подключение к серверу
$redis->connect( 'localhost' );
if ( $bild_20 ) {
  echo '<div class="20s '.$pname.'">блок 1</div>'; 
  $key = $pname.'_20_';
  if ($redis->exists($key)) {
    echo $redis->get($key);
  } else {
  $content20 = get20($bild_20,$pname);
    // вычисляете свой кусок, затем
    $redis->set($key, $content20);
    $redis->expire($key, 3600); // expires in 1 hour
    echo $redis->get($key);
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question