/
index.html
524 lines (423 loc) · 12.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<!DOCTYPE html>
<!--
Google HTML5 slide template
Authors: Luke Mahé (code)
Marcin Wichary (code and design)
Dominic Mazzoni (browser compatibility)
Charles Chen (ChromeVox support)
URL: http://code.google.com/p/html5slides/
-->
<html>
<head>
<title>Caching for Coders | How, What, and When to Cache in WordPress</title>
<meta charset='utf-8'>
<script src='slides.js'></script>
</head>
<body style='display: none;'>
<section class='slides layout-regular template-default'>
<article class='title-page'>
<h1>Caching for Coders: <br /> How, What, and When to Cache in WordPress</h1>
<h3 style="margin-top:30px;">Zack Tollman, 10up LLC</h3>
<h4>@Zack_Dev, 10up.com</h4>
<h4>Slides: tollmanz.github.com/caching-for-coders</h4>
<h4>Conversation: github.com/tollmanz/caching-for-coders</h4>
</article>
<article>
<h3>Metaphor</h3>
<p style="text-align:center;margin-top:50px;">
<img src="images/turtles.jpg" />
</p>
<p style="text-align:right;margin-top:60px;font-size:20px;">
Source: <a href="http://toblender.com/memcached-story-part-1/" title="The Memcache Story" target="_blank">http://toblender.com/memcached-story-part-1/</a>
</p>
</article>
<article>
<h3>Metaphor</h3>
<p style="text-align:center;margin-top:50px;" class="build">
<img src="images/ninja-turtle.jpg" />
</p>
</article>
<article>
<h3>What We Are Talking About</h3>
<ul>
<li>
Improving site performance through object (fragment) caching
<ul>
<li>How to cache</li>
<li>What to cache</li>
<li>When to cache</li>
</ul>
</li>
<li>
No more cookie cutter approaches to caching
</li>
<li>
Not talking about caching plugins or server setups
<ul>
<li>
<a href="http://wordpress.tv/2010/07/10/scott-oshaughnessy-caching-wordpress-boulder10/">
Chris Scott and Sean O’Shaughnessy: Caching in WordPress
</a>
</li>
</ul>
</li>
</ul>
</article>
<article>
<h3>How to Cache</h3>
<p>Transients <a href="http://codex.wordpress.org/Transients_API" target="_blank"><img src="images/link.png" /></a></p>
<ul>
<li>set_transient, get_transient, delete_transient</li>
<li>Uses the database if an object cache is not present</li>
</ul>
<pre>
function zt_display_leaders() {
$leaders = get_transient( 'zt-leaders' );
if ( false === $leaders ) {
$leaders = zt_get_leaders();
set_transient( 'zt-leaders', $leaders, 300 );
}
return $leaders;
}</pre>
</article>
<article>
<h3>How to Cache</h3>
<p>WP_Object_Cache <a href="http://codex.wordpress.org/Class_Reference/WP_Object_Cache" target="_blank"><img src="images/link.png" /></a></p>
<ul>
<li>wp_cache_add, wp_cache_get, wp_cache_delete (amongst others)</li>
<li>Success depends on server environment</li>
</ul>
<pre>
function zt_display_leaders() {
$leaders = wp_cache_get( 'zt-leaders', 'zt-stats' );
if ( false === $leaders ) {
$leaders = zt_get_leaders();
wp_cache_set( 'zt-leaders', $leaders, 'zt-stats', 300 );
}
return $leaders;
}</pre>
<p>Discussion of some differences by Otto <a href="http://wordpress.stackexchange.com/questions/26796/using-transients-in-conjunction-with-memcached/26804#26804" title="WPSE Discussion" target="_blank"><img src="images/link.png" /></a></p>
</article>
<article>
<h3>What to Cache</h3>
<ul>
<li>
Requests to external services
<ul>
<li>wp_remote_*</li>
</ul>
</li>
<li>
Expensive queries
<ul>
<li>'post__not_in', 'meta_query', 'tax_query'</li>
<li>Identify slow queries with Debug Bar/Extender</li>
</ul>
</li>
<li>
Repeated routines/queries
<ul>
<li>First image in post, dealing with child taxonomy terms</li>
</ul>
</li>
<li>
Infrequently updated data
<ul>
<li>menus <a href="http://hitchhackerguide.com/2011/10/07/caching-wordpress-navigation-menus-wp_nav_menu-wrapper/" target="_blank"><img src="images/link.png" /></a>, related posts, lists of data</li>
</ul>
</li>
</ul>
</article>
<article>
<h3>When to Cache</h3>
<ul>
<li>The goal is to reduce the server's workload</li>
<li>How can you remove queries or expensive processes?</li>
</ul>
<h3 class="second-header" style="margin-top:40px;">Gameplan</h3>
<ul>
<li>Ideally, refresh cache ONLY when data is added/changed</li>
<li>Minimize running intensive processes</li>
<li>Avoid caching on front end page requests</li>
<li>Design to fail gracefully</li>
</ul>
</article>
<article>
<h3>When to Cache — A Common Solution</h3>
<p>Cache when the data is not cached</p>
<pre>
// functions.php
function zt_get_tweet() {
if ( false === ( $tweet = get_transient( 'zt-tweet' ) ) ) {
$tweet = zt_request_tweet(); // Talk to Twitter
set_transient( 'zt-tweet', $tweet, 300 );
}
return $tweet;
}
// sidebar.php
<h3>Latest Tweet:</h3>
<p><?php echo zt_get_tweet(); ?></p></pre>
</article>
<article>
<h3>When to Cache — When the Data Changes</h3>
<ul>
<li>
Generate the data on an admin event
<ul>
<li>Generates data only when changed</li>
<li>Reduces the chance of front end generation</li>
</ul>
</li>
<li>
Helpful Events/Hooks
<ul>
<li>Publishing/Updating posts: publish_post</li>
<li>Changing post status: transition_post_status</li>
<li>Creating terms: created_{$taxonomy}</li>
<li>Updating terms: edited_{$taxonomy}</li>
</ul>
</li>
</ul>
</article>
<article>
<h3>When to Cache — When the Data Changes</h3>
<pre>
function zt_set_best_games( $post_id, $post ) {
if ( 'zt-game' != $post->post_type )
return false;
zt_determine_best_games();
}
add_action( 'publish_post', 'zt_set_best_games', 99, 2 );
function zt_determine_best_games() {
/* Get best reviewed games; set to $top_games */
set_transient( 'zt-top-games', $top_games );
return $top_games;
}</pre>
<p><em>In 2 years</em></p>
<p>1 post per day: 730 runs</p>
<p>Common solution: 210,240 runs</p>
</article>
<article>
<h3>When to Cache — On an Interval</h3>
<ul>
<li>
Use scheduled events to cache unpredictable data
<ul>
<li>e.g., external data</li>
</ul>
</li>
<li>Avoids unnecessary requests</li>
<li>Reduces the chance of front end caching</li>
</ul>
</article>
<article>
<h3>When to Cache — On an Interval</h3>
<pre>
function zt_schedule_tweet_retrieval() {
if ( ! wp_next_scheduled( 'zt_tweet_sched' ) )
wp_schedule_event( time(), 'hourly', 'zt_tweet_sched' );
add_action( 'zt_tweet_sched', 'zt_request_tweet' );
}
add_action( 'init', 'zt_schedule_tweet_retrieval' );
function zt_request_tweet() {
// Talk to Twitter
set_transient( 'zt-tweet', $tweet );
return $tweet;
}
function zt_get_tweet() {
if ( false === ( $tweet = get_transient( 'zt-tweet' ) ) ) {
$tweet = zt_request_tweet(); // Talk to Twitter
set_transient( 'zt-tweet', $tweet );
}
return $tweet;
}</pre>
</article>
<article>
<h3>When to Cache — Failing Gracefully</h3>
<ul>
<li>Never trust that data is in the cache</li>
<li>
Have a backup plan
<ul>
<li>Display placeholder text</li>
<li>Regenerate data</li>
<li>Backup storage (Options API)</li>
</ul>
</li>
</ul>
</article>
<article>
<h3>When to Cache — Failing Gracefully</h3>
<p>Placeholder text</p>
<pre>
function zt_display_tweet() {
if ( $tweet = get_transient( 'zt-tweet' ) )
return $tweet;
else
return 'Latest Tweet Unavailable';
}</pre>
<p>Regenerate data</p>
<pre>
function zt_display_tweet() {
if ( $tweet = get_transient( 'zt-tweet' ) )
return $tweet;
else
return zt_get_tweet(); // Talk to Twitter
}</pre>
</article>
<article>
<h3>When to Cache — Failing Gracefully</h3>
<p>Storing a "backup"</p>
<pre>
function zt_schedule_tweet_retrieval() {
if ( ! wp_next_scheduled( 'zt_tweet_sched' ) )
wp_schedule_event( time(), 'hourly', 'zt_tweet_sched' );
add_action( 'zt_tweet_sched', 'zt_generate_tweet' );
}
add_action( 'init', 'zt_schedule_tweet_retrieval' );
function zt_generate_tweet() {
$tweet = zt_request_tweet(); // Talk to Twitter
set_transient( 'zt-tweet', $tweet );
if ( ! get_option( 'zt-tweet' ) )
add_option( 'zt-tweet', $tweet, '', 'no' );
else
update_option( 'zt-tweet', $tweet );
return $tweet;
}</pre>
</article>
<article>
<h3>When to Cache — Failing Gracefully</h3>
<p>Using the "backup"</p>
<pre>
function zt_display_tweet() {
if ( $tweet = get_transient( 'zt-tweet' ) ) {
return $tweet;
} elseif ( $tweet = get_option( 'zt-tweet' ) {
set_transient( 'zt-tweet', $tweet );
return $tweet;
} else {
return zt_generate_tweet();
}
}</pre>
</article>
<article>
<h3>When to Cache — Avoid Front End Caching</h3>
<ul>
<li>Flooding
<ul>
<li>Many concurrent users cause overload</li>
</ul>
</li>
<li>Consider "locking" techniques if generating on front end</li>
<li>
<a href="https://github.com/markjaquith/WP-TLC-Transients" title="TLC Transients" target="_blank">
Mark Jaquith's WP-TLC-Transients
</a>
</li>
</ul>
</article>
<article>
<h3>Beer List</h3>
<pre>
function zt_schedule_generate_beer() {
if ( ! wp_next_scheduled( 'zt_beer_sched' ) )
wp_schedule_event( time(), 'daily', 'zt_beer_sched' );
add_action( 'zt_beer_sched', 'zt_generate_beer_list' );
}
add_action( 'init', 'zt_schedule_generate_beer' );
function zt_generate_beer_list() {
// Query API and generate HTML
$beer_list = zt_get_beer_list();
set_transient( 'zt-beers', $beer_list );
delete_transient( 'zt-beer-sched-single' );
return $beer_list;
}
function zt_generate_beer_on_publish( $post_id, $post ) {
zt_generate_beer_list();
}
add_action( 'publish_post', 'zt_generate_beer_on_publish', 10, 2 );</pre>
</article>
<article>
<h3>Beer List</h3>
<pre>
// Same function from previous slide
function zt_generate_beer_list() {
// Query API and generate HTML
$beer_list = zt_get_beer_list();
set_transient( 'zt-beers', $beer_list );
delete_transient( 'zt-beer-sched-single' );
return $beer_list;
}
function zt_display_all_the_beers() {
if ( $beers = get_transient( 'zt-beers' ) ) {
return $beers;
} else {
if ( ! get_transient( 'zt-beer-sched-single' ) ) {
set_transient( 'zt-beer-sched-single', 'yes' );
wp_schedule_single_event( time(), 'zt_beer_sched' );
}
return 'Beer list is being generated.';
}
}</pre>
</article>
<article class="beer-list">
<h3>Beer List</h3>
<section style="float:left;width:50%;margin-top:20px;">
<h4>Mission Brewery</h4>
<ul>
<li>Shipwrecked Double IPA</li>
<li>Amber Ale</li>
<li>Blonde Ale</li>
<li>Hefeweizen</li>
<li>IPA</li>
</ul>
<h4>The Beer Company</h4>
<ul>
<li>Beer list not available.</li>
</ul>
<h4>Butcher's Brewing Company</h4>
<ul>
<li>Mucho Aloha HPA</li>
<li>Mucho Aloha Imperial IPA</li>
<li>Nut Brown Ale</li>
<li>Pig's Premium Nut Brown</li>
</ul>
</section>
<section style="float:left;width:50%;margin-top:20px;">
<h4>Coronado Brewing Company</h4>
<ul>
<li>Islander IPA</li>
<li>Point Loma</li>
<li>Uptown Brown</li>
<li>Mermaid Red</li>
<li>Four Brothers Pale Ale</li>
<li>Coronado Golden Ale</li>
<li>Mermaids Red Ale</li>
<li>Bugger Brown</li>
<li>Outlet Stout</li>
<li>Idiot IPA</li>
<li>Islandweizen</li>
<li>Black Ops IPA</li>
<li>Frog's Breath IPA</li>
<li>Hoppy Daze</li>
<li>Black Pearl</li>
<li>Hop Scotch</li>
<li>North Island</li>
<li>Saison By the Sea</li>
<li>Stoopid Stout</li>
<li>Down N out</li>
<li>Barrel Aged Barley Wine Ale</li>
</ul>
</section>
</article>
<article>
<h3>Thank You</h3>
<p>Andrew Nacin (@nacin)</p>
<p>Daniel Bachhuber (@danielbachhuber)</p>
<p>Helen Hou-Sandi (@helenhousandi)</p>
<p>Laura Milewski (@l</p>
<p>Steve Locker (@slocker3)</p>
</article>
</section>
</body>
</html>