wordpress kitchen 2014 - Александр Стриха: Кеширование в wordpress
DESCRIPTION
Если ваш блог читает не только мама и одноклассники, то вы наверняка сталкивались с проблемой нагрузки на ваш трехдолларовый хостинг. Мы расскажем как решить эту проблему, используя встроенные функции кеширования WordPress.TRANSCRIPT
WordPressKitchen 2014
Александр Стриха[email protected]
WordPressКеширование в
IsCache = ???
IsCache = 1Money
W P_Object_Cache
wp_cache_set( 'key', 'value' );
$value = wp_cache_get( 'key' );
function get_free_products_count(){ if ( false === ( $count = wp_cache_get( 'free-products' ) ) ) { $count = big query wp_cache_set( 'free-products', $count ); } return $count;}
get_free_products_count(); // SQLget_free_products_count(); // Cacheget_free_products_count(); // Cache
wp-content/object-cache.php
Memcached https://wordpress.org/plugins/memcached/
Redishttps://github.com/ericmann/Redis-Object-Cache
Transients API
set_transient( 'key', 'value', $time );
$value = get_transient( 'key' );
if ( false === ( $value = get_transient( 'key' ) ) ) { $value = wp_remote_get( $url );
set_transient( 'key', $value, DAY_IN_SECONDS );}
if ( false === ( $value = get_transient( 'key' ) ) ) { $value = BIG QUERY
set_transient( 'key', $value, DAY_IN_SECONDS );}
add_action( 'save_post', function ( $post_id ) {
delete_transient( 'key' );
});
if ( false === ( $value = get_transient( 'key' ) ) ) {
ob_start();
get_sidebar();
$value = ob_get_clean();
set_transient( 'key', $value, DAY_IN_SECONDS );
}
add_action( 'cron_event', function( ) {
$value = wp_remote_get( $url );
set_transient( 'key', $value, DAY_IN_SECONDS );
});
wp_cache_postload( );
Advanced Cache
wp-content/advanced-cache.phpdefine( 'WP_CACHE', true );
WP Super Cachehttps://wordpress.org/plugins/wp-super-cache/
W3 Total Cachehttps://wordpress.org/plugins/w3-total-cache/
Batcachehttps://wordpress.org/plugins/batcache/
function wp_cache_postload( ) {
$key = 'page_cache' . $_SERVER[ 'REQUEST_URI' ];
if ( $html = get_transient( $key ) ) {
echo $html;
exit;
}
ob_ ob_start( function( $html ) use ( $key ) {
set_transient( $key, $html, HOUR_IN_SECONDS );
return $html;
});
}
Clean WP: 99
Object Cache: 116
Full Page Cache: 1
Response time (sec)
KEEPCALM
AND
USECACHE