File cache (template cache)

Time:2021-7-26

File cache (template cache)

Bigpipe technology from page fragment cache to Facebook

  • Divide the page into small pieces
  • Using ob_ Flush () and flush () output the contents of the buffer to the browser in advance
  • The browser continuously receives and renders to the page in a request, and displays it block by block.

    • Note: JS part, which does not need to be executed immediately, can be Eval entered finally

The process of program execution is:
program data -> php buffer -> tcp buffer -> client browers

method effect explain
ob_start() Open output buffer
ob_flush() Send the contents of the PHP buffer to the TCP buffer Call ob_ After flush (), the contents of the buffer will be discarded.
flush() Send the contents in the TCP buffer so far to the user’s browser. The flush () function has no effect on the caching mode of the server or client browser. Therefore, OB must be used at the same time_ Flush () and flush () functions to flush the output buffer.
ob_get_contents() Returns the contents of the internal buffer Only get the contents of the output buffer, but do not clear it. If it is not activated, false is returned
ob_end_clean() Delete the contents of the internal buffer and close the internal buffer.
ob_end_flush() Send the contents of the internal buffer to the browser and close the output buffer
ob_get_length() Returns the length of the internal buffer

How to use this technology?

  • Nginx needs to be set

sudo vim /etc/nginx/nginx.conf

proxy_buffering off;
fastcgi_keep_conn on;

gzip off;  //  Compression is turned on by default and needs to be turned off
  • PHP needs to be set

sudo vim /etc/php/7.0/fpm/php.ini


;output_buffering = 4096
output_buffering = off
  • Restart nginx and PHP

sudo service nginx restart
sudo service php7.0-fpm restart
  • Use in template
<?php
//Open buffer
ob_start();
?>

<?php
//Slow execution of simulation program
sleep(1);
//Send what PHP has executed to nginx or Apache
ob_flush();
//Nginx or Apache sends the content to the client
flush();
?>

<?php

//Before closing, you also need to send the last content to the client
ob_flush();
flush();

//Clear buffer
ob_end_clean();
?>

Effects and advantages

When there are many block contents in the page, you can brush many block contents to the client in batches, so that the client does not need to wait for all the contents to be loaded to see the contents. And this technology only has one request. If you use ajax, you may think that each block content uses Ajax to request once, so there will be multiple requests.

Original address

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]