cache all the things

Post on 24-Jun-2015

829 Views

Category:

Software

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Caching Pods Objects, caching function / method results, fragment caching portions of your front-end output… cache all the things. In this talk, learn how to use Pods' caching system Pods View along with WordPress' native caching system to improve the performance of Pods and WordPress. Go beyond traditional, full page caching systems and learn how to leverage a persistent object cache to provide the best experience for end users, especially logged-in users on highly dynamic sites. Pods Community manager Josh Pollock will walk you through Pods View and show you the patterns he uses to optimize his code.

TRANSCRIPT

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Cache All The Things!Josh Pollock, @josh412

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Why Cache?

○ Why Cache?■ Make it faster!

○ Why Not Cache?■ Is it more work/queries then it's worth?■ Use Pods::find() select for optimization instead?

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Caching Options

○ Basic Types■ Object■ Transient■ File based

○ Full page

○ Fragment caching○ Object caching○ Pods object caching

○ Caching function/method results

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Caching Pods Objects

//use object cache

$params = array(

'where' => 'home_planet.meta_value = "Corellia"',

'limit' => 20,

'expires' => DAY_IN_SECONDS,

);

$pods = pods( 'jedi', $params );

//use transient cache

$params = array(

'where' => 'home_planet.meta_value = "Corellia"',

'limit' => 20,

'expires' => DAY_IN_SECONDS,

);

$pods = pods( 'jedi', $params );

Almost automatic!Just need to set $params['expires']

Defaults to object caching.

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Caching Pods ObjectsWhich cache type to use?● Object

○ Stores in memory○ Best option, if you have a persistent object cache○ Has Groups

● Transient○ Stores in database

● Site-Transient○ Accessible across a multi-site network

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Pods View

Pods' Caching System

Used for Object and Fragment Caching

Clears With Pod Cache Clear & Item Update

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Pods View Helper Functions

Object CacheSet:pods_cache_set ( $key, $value, $group = '', $expires = 0)

Get:pods_cache_get ( $key, $group = '', $callback = null )

Clear:pods_cache_clear ( $key = true, $group = '' )

Transient CacheSet:pods_transient_set ( $key, $value, $expires = 0 )

Get:pods_transient_get ( $key, $callback = null )

Clear:pods_transient_clear ( $key = true )

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Pods View Helper Functions

Any Cache TypeSet:pods_view_set ( $key, $value, $expires = 0, $cache_mode = 'cache', $group = '' )

Get:pods_view_get ( $key, $cache_mode = 'cache', $group = '', $callback = null )

Clear:pods_view_clear ( $key = true, $cache_mode = 'cache', $group = '' )

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Using Cache Groups

Setting In Groupspods_cache_set( 'key1', $value, 'group1', 600 );

pods_cache_set( 'key2', $value2, 'group1', 600 );

pods_cache_set( 'key3', $value3, 'group2', 600 );

pods_cache_set( 'key4', $value4, 'group2', 600 );

//clear only items in group1:

pods_cache_clear( true, 'group1' );

//clear only key 4

pods_cache_clear( 'key4' );

Clearing With A Filteradd_action('pods_api_post_save_pod_item', 'slug_cache_clear', 10, 3);

function my_post_save_function($pieces, $is_new_item, $id ) {

$pod_name = $pieces['params']->pod;

if ( $pod_name == 'films' ) {

pods_cache_clear( true, 'films_cache' );

}

pods_cache_clear( true, "{$pod_name}_{$id}" );

}

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Fragment Caching

● Perfect for dynamic sites, especially membership sites.● Cache Query Heavy Parts Of Your Site● http://pods.io/tutorials/partial-page-caching-smart-

template-parts-pods/

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Fragment Caching With pods_view()

function pods_view( $view, $data = null, $expires = false, $cache_mode = 'cache', $return = false )

● $view is a file● $view is assumed to be in (child) theme directory unless

full path is given.● $data is an array of variables to "scope" into the view.

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Scoping Data Into A Template Part

● Allows you to use a variable in included view.● Enhances separation of concerns.$pods = pods( 'jedi', 99 );

$name = $pods->display( 'post_title' );

$home_planet = $pods->display( 'home_planet.post_title' );

$data = compact( array( 'name', 'home_planet' ) );

pods_view( $view, $data = null, $expires = false, $cache_mode = 'cache', $return = false )

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Better Sidebars With pods_view()https://github.com/pods-framework/pods_s/blob/master/inc/pods_s.php#L9-L49

function pods_s_get_sidebar( $name = null ) { $name = apply_filters( 'pods_s_get_sidebar', $name ); if ( apply_filters( 'pods_s_no_sidebar', false ) === false ) { if ( function_exists( 'pods_view' ) ) { $name = 'sidebar.php'; if ( ! is_null( $name ) && file_exists( 'sidebar-'.$name.'.php' ) ) { $name = 'sidebar-'.$name.'.php'; } pods_view( $name, null, pods_s_cache_expires(), pods_s_cache_mode() ); } else { get_sidebar( $name );

} }}

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Caching Function/Method Resultsfunction slug_get_all _unique( $pod_name, $raw_field, $display_field ) { $key = "fmf_{$pod_name}_{$raw_field}_{$unique}"; if ( false == ( $items = pods_transient_get( $key ) ) ) { $pods = pods( $pod_name, array( 'limit' => -1 ) ); if ( $pods->total() > 0 ) { while( $pods->fetch( ) ) { $raw = $pods->field( $raw_field ); $display = $pods->display( $display_field ); if ( $raw && $display ) { $items[ $raw ] = $display; } } } if ( is_array( $items ) ) {

$items = array_unique( $items ); pods_transient_set( $key, $items, WEEK_IN_SECONDS ); } } if ( isset( $items ) ) { return $items; } }

Cache ALL the Things! // Josh Pollock // PodsCamp 2014

Questions?

top related