Using memcache to optimize wordpress and speed up

Environmental Science:

A local development environment

One is deployed in the virtual machine (the virtual machine is installed with memcache cache), and then the optimized version is compared with the local development environment

wordpress version: 4.9.5

PHP: version 5.6

1. First, in the functions.php file of the topic, add the following monitoring statistics code

function performance( $visible = true ) {
    $stat = sprintf(  '%d queries in %.3f seconds, using %.2fMB memory',
        get_num_queries(),
        timer_stop( 0, 3 ),
        memory_get_peak_usage() / 1024 / 1024
    );
    // echo $visible ? $stat : "<!-- {$stat} -->" ;
    echo "<div class='container_12'><p class='grid_12 footer clearfix'>$stat</p></div>";
}

add_action( 'wp_footer', 'performance', 20 );

The style can be customized according to its own theme. This code can count how many database queries the page has. How many seconds and memory did it take, similar to the following pattern:

16 queries in 1.306 seconds, using 12.98MB memory

2. Install memcached server

[root@bogon wp-content]# ps -ef | grep memcached
root      5050  3466  0 14:44 pts/1    00:00:00 grep memcached
root     29269     1  0 13:57 ?        00:00:02 /usr/local/memcached/bin/memcached -d -l 127.0.0.1 -p 11211 -u root -m 64 -c 1024 -P /var/run/memcached.pid

3. Install the php memcache extension

wget http://pecl.php.net/get/memcache-3.0.8.tgz, install in phpize plug-in mode. After installation, enable it in php.ini, restart PHP, and make sure that Memcache has been loaded

memcache
memcache support    enabled
Version    3.0.8
Revision    $Revision: 329835 $
Directive    Local Value    Master Value
memcache.allow_failover    1    1
memcache.chunk_size    32768    32768
memcache.compress_threshold    20000    20000
memcache.default_port    11211    11211
memcache.hash_function    crc32    crc32
memcache.hash_strategy    consistent    consistent
memcache.lock_timeout    15    15
memcache.max_failover_attempts    20    20
memcache.protocol    ascii    ascii
memcache.redundancy    1    1

4. Download the wordpress memcache plug-in

wget https://downloads.wordpress.org/plugin/memcached.3.0.1.zip

Unzip object-cache.php to the WP content directory

[root@bogon wp-content]# ls
index.php  languages  object-cache.php  package.xml  plugins  themes  upgrade  uploads
[root@bogon wp-content]# 

Last compare server

The version of memcache is installed. The homepage performance:

3 queries in 0.451 seconds, using 13.56MB memory

No cache enabled system, homepage performance:

16 queries in 1.336 seconds, using 12.98MB memory

It can be seen that there is a significant improvement. Other pages will be optimized and cached to

Enable opcache and speed up again:

3 queries in 0.129 seconds, using 3.48MB memory

opcache in php.ini needs to be loaded as follows:

zend_extension=opcache.so

opcache related configuration:

[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1

; Determines if Zend OPCache is enabled for the CLI version of PHP
opcache.enable_cli=1

; The OPcache shared memory storage size.
opcache.memory_consumption=528

; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=8

; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 100000 are allowed.
opcache.max_accelerated_files=10000

; The maximum percentage of "wasted" memory until a restart is scheduled.
;opcache.max_wasted_percentage=5
"php.ini" 2051L, 74201C written                                                                                      

  
[ldap]
; Sets the maximum number of open links or -1 for unlimited.
ldap.max_links = -1

[mcrypt]
; For more information about mcrypt settings see http://php.net/mcrypt-module-open

; Directory where to load mcrypt algorithms
; Default: Compiled in into libmcrypt (usually /usr/local/lib/libmcrypt)
;mcrypt.algorithms_dir=

; Directory where to load mcrypt modes
; Default: Compiled in into libmcrypt (usually /usr/local/lib/libmcrypt)
;mcrypt.modes_dir=

[dba]
;dba.default_handler=

[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1

; Determines if Zend OPCache is enabled for the CLI version of PHP
opcache.enable_cli=1

; The OPcache shared memory storage size.
opcache.memory_consumption=528

; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=8

; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 100000 are allowed.
opcache.max_accelerated_files=10000

Tags: PHP ascii Database xml

Posted on Fri, 03 Apr 2020 19:01:07 -0700 by recurzion