PHP speed promotion_PHP tutorial

WBOY
Release: 2016-07-13 17:48:27
Original
786 people have browsed it

We have discussed PHP caching technology in depth before, which mainly mentioned data caching. Data caching mainly refers to database query caching. Every time you access a page, it will first detect whether the corresponding cached data exists. If it does not exist, connect to the database, obtain the data, and serialize the query results and save them to a file. The same query results are obtained directly from the cache table or file.

The most widely used example is the search function of Discuz, which caches the result ID into a table and searches the cache table first when searching for the same keyword next time.

As a common method, when multiple tables are associated, the contents in the attached table are generated into an array and saved in a field of the main table. When necessary, the array is decomposed. The advantage of this is that only one table can be read, but the disadvantage is that There will be many more steps to synchronize the two data. The database is always the bottleneck. Trading the hard disk for speed is the key point of this.

Page caching

Every time you visit a page, it will first check whether the corresponding cached page file exists. If it does not exist, connect to the database, get the data, display the page and generate the cached page file at the same time, so that the page file will be the same the next time you visit. It worked. (Template engines and some common cache classes on the Internet usually have this function)

Time triggered cache

Check whether the file exists and the timestamp is less than the set expiration time. If the file modification timestamp is greater than the current timestamp minus the expiration timestamp, then use the cache, otherwise update the cache.

Content triggered caching

When data is inserted or updated, the cache is forced to be updated.

Static cache

The static cache mentioned here refers to static caching, which directly generates text files such as HTML or xml, and regenerates them when there are updates. It is suitable for pages that do not change much, so I won’t talk about it here.

Memory cache

Memcached is a high-performance, distributed memory object caching system used to reduce database load and improve access speed in dynamic applications.

 

$memcache = new Memcache;

 $memcache->connect(‘localhost’, 11211) or die (“Could not connect”);

$version = $memcache->getVersion();

echo “Server’s version: “.$version.”n”;

 $tmp_object = new stdClass;

 $tmp_object->str_attr = ‘test’;

 $tmp_object->int_attr = 123;

 $memcache->set(‘key’, $tmp_object, false, 10) or die (“Failed to save data at the server”);

echo “Store data in the cache (data will expire in 10 seconds)n”;

$get_result = $memcache->get(‘key’);

echo “Data from the cache:n”;

var_dump($get_result);

 ?>

Example of reading the database:

 

 $sql = ‘SELECT * FROM users’;

 $key = md5($sql); //memcached object identifier www.2cto.com

  if ( !($datas = $mc->get($key)) ) {

  // If cached data is not obtained in memcached, use database query to obtain the record set

echo “n”.str_pad(‘Read datas from MySQL.’, 60, ‘_’).”n”;

 $conn = mysql_connect(‘localhost’, ‘test’, ‘test’);

mysql_select_db(‘test’);

$result = mysql_query($sql);

while ($row = mysql_fetch_object($result))

$datas[] = $row;

  // Save the result set data obtained from the database to memcached for use during the next visit

$mc->add($key, $datas);

 } else {

echo “n”.str_pad(‘Read datas from memcached.’, 60, ‘_’).”n”;

 }

var_dump($datas);

 ?>

PHP buffer

For example, eaccelerator, apc, phpa, xcache, etc.

MySQL cache

This is also considered non-code level. Classic databases use this method. Look at the running time below, it is like 0.09xxx.

 [client]

 ……

default-character-set=gbk

default-storage-engine=MYISAM

max_connections=600

max_connect_errors=500

back_log=200

interactive_timeout=7200

query_cache_size=64M

 ……

table_cache=512

 ……

myisam_max_sort_file_size=100G

myisam_max_extra_sort_file_size=100G

myisam_sort_buffer_size=128M

key_buffer_size=1024M

read_buffer_size=512M

 ……

thread_concurrency=8

Web cache based on reverse proxy

Such as Nginx, SQUID, mod_PRoxy (apache2 and above are divided into mod_proxy and mod_cache)

Example of NGINX:

 

  #user nobody;

  worker_processes 4;

  error_log logs/error.log crit;

  pid logs/nginx.pid;

  worker_rlimit_nofile 10240;

  events {

  use epoll;

  worker_connections 51200;

  }

  http {

  include mime.types;

  default_type application/octet-stream;

  sendfile on;

  keepalive_timeout 65;

  tcp_nodelay on;

  # server pool

  upstream bspfrontsvr {

  server 10.10.10.224:80 weight=1;

  server 10.10.10.221:80 weight=1;

  }

  upstream bspimgsvr {

  server 10.10.10.201:80 weight=1;

  }

  upstream bspstylesvr {

  server 10.10.10.202:80 weight=1;

  }

  upstream bsphelpsvr {

  server 10.10.10.204:80 weight=1;

  }

  upstream bspwsisvr {

  server 10.10.10.203:80 weight=1;

  }

  upstream bspadminsvr {

  server 10.10.10.222:80 weight=1;

  }

  upstream bspbuyersvr {

  server 10.10.10.223:80 weight=1;

  }

  upstream bspsellersvr {

  server 10.10.10.225:80 weight=1;

  }

  upstream bsploginsvr {

  server 10.10.10.220:443 weight=1;

  }

  upstream bspregistersvr {

  server 10.10.10.220:80 weight=1;

  }

  log_format test_com ‘$remote_addr – $remote_user [$time_local] “$request” ‘

  ‘$status $body_bytes_sent “$http_referer” “$http_user_agent” ‘;

  #——————————————————————–

  #img.test.com

  server {

  listen 10.10.10.230:80;

  server_name img.test.com;

  location / {

  proxy_pass http://bspimgsvr;

  include proxy_setting.conf;

  }

  access_log logs/img.log test_com;

  }

  #style.test.com

  server {

  listen 10.10.10.230:80;

  server_name style.test.com;

  location / {

  proxy_pass http://bspstylesvr;

  include proxy_setting.conf;

  }

  access_log logs/style.log test_com;

  }

  #help.test.com

  server {

  listen 10.10.10.230:80;

  server_name help.test.com;

  location / {

  proxy_pass http://bsphelpsvr;

  include proxy_setting.conf;

  }

  access_log logs/help.log test_com;

  }

  #admin.test.com

  server {

  listen 10.10.10.230:80;

  server_name admin.test.com;

  location / {

  proxy_pass http://bspadminsvr;

  include proxy_setting.conf;

  }

  access_log logs/admin.log test_com;

  }

  #buyer.test.com

  server {

  listen 10.10.10.230:80;

  server_name buyer.test.com;

  location / {

  proxy_pass http://bspbuyersvr;

  include proxy_setting.conf;

  }

  access_log logs/buyer.log test_com;

  }

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template