Transcript
Page 1: Cache All the Things

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

Cache All The Things!Josh Pollock, @josh412

Page 2: Cache All the Things

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?

Page 3: Cache All the Things

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

Page 4: Cache All the Things

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.

Page 5: Cache All the Things

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

Page 6: Cache All the Things

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

Page 7: Cache All the Things

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 )

Page 8: Cache All the Things

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 = '' )

Page 9: Cache All the Things

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}" );

}

Page 10: Cache All the Things

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/

Page 11: Cache All the Things

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.

Page 12: Cache All the Things

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 )

Page 13: Cache All the Things

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 );

} }}

Page 14: Cache All the Things

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; } }

Page 15: Cache All the Things

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

Questions?


Top Related