Cache Rules Everything Around Me
- 12. The marketing/biz dev team managed to get us featured on
the homepage of a major web portal!
(And by featured I mean the featured story on their homepage)
- 20. We had to call Yahoo! and have them remove the story
because we couldn't handle the traffic…
- 25. CACHE
“a component that transparently stores data
so that future requests for that data can be
served faster.”
— via Wikipedia, Cache (Computing)
- 26. A cache may contain values computed earlier or duplicate
values stored elsewhere.
- 27. A cache hit can complete the request by reading the value
from the cache.
- 28. A cache miss has to be recomputed or fetched from
another location which is much slower.
- 29. The more requests that can be served from a cache, the
faster the system performs.
The faster the system, the more requests it can handle.
- 31. You could write 5 separate essays
OR
You could write one essay and use it for all of your classes
- 33. There are many (potential) layers of caching involved in a
single web request.
- 46. Your logo image isn't going to change when going from your
homepage to a single blog post…
So why make the browser even request it?
- 48. WHAT DOES THAT LOOK
LIKE?
Add this to your file:.htaccess
<ifmodulemod_expires.c="">
ExpiresActiveon
ExpiresDefault "accessplus1month"
ExpiresByTypeimage/gif "accessplus1month"
ExpiresByTypeimage/jpeg "accessplus1month"
ExpiresByTypeimage/png "accessplus1month"
</ifmodule>
- 49. The HTML5 Boiler Plate project's .htaccess file
Read the docs
View the source
- 51. >
And then you have to clear the browser cache or hard
refresh the page (Shift + Refresh).
- 53. But then every time you update your JavaScript or CSS you
need to change the $vervariable to bust the cache…
- 57. CDNs serve their copy of a file, if available, sparing your
server from the traffic.
- 58. If the CDN doesn't have a copy of the file, it passes the
request back to the origin.
- 59. TWO TYPES OF CDNS
1. Push - assets get uploaded to the CDN manually, you link
directly to it
2. Pull - The CDN is a proxy saving requests that are passed
through it
See Push vs. Pull CDNs
- 62. The CDN uses the Expires header to determine if cached
asset is stale or not.
Stale requests get passed to the origin server
- 63. Some CDNs and proxies
. So avoid it if you can.
don't cache URLs with a query
string
BAD
http://www.example.com/js/file.js?2013-09-12
GOOD
http://www.example.com/js/2013-09-12/file.js
http://www.example.com/js/file.2013-09-12.js
- 64. If you cache your HTML via a CDN then if your origin server
goes down your site will still be served. Visitors won't even
know.
- 68. Full page caching takes the result of a page request from
WordPress and saves it as a static HTML file that will be
served up the next time.
- 71. CACHING PLUGIN TIPS
Don't cache pages for logged in users
Don't cache POST requests
Do serve cached files from mod_rewrite(not PHP)
Don't serve a separate mobile version (use responsive
design)
Make sure it is working!
- 74. TRANSIENTS API
Used to store data that can expire at any time
Has an expiration to invalidate the data
Uses the wp_options table or an object cache
See http://codex.wordpress.org/Transients_API
- 78. TIME CONSTANTS
As of WordPress 3.5 several constants were introduced to
easily express time
MINUTE_IN_SECONDS =60(seconds)
HOUR_IN_SECONDS =60*MINUTE_IN_SECONDS
DAY_IN_SECONDS =24*HOUR_IN_SECONDS
WEEK_IN_SECONDS =7*DAY_IN_SECONDS
YEAR_IN_SECONDS =365*DAY_IN_SECONDS
- 82. WP_CACHE FUNCTIONS
Don't call WP_Object_Cache Class directly!
wp_cache_add($key,$data,$group,$expire);
wp_cache_set($key,$data,$group,$expire);
wp_cache_get($key,$group='',$force=false,$found=null);
wp_cache_delete($key,$group);
wp_cache_replace($key,$data,$group,$expire)
- 87. HOW DO I ENABLE OBJET
CACHING?
1. Download and install or or on
your server
2. Add the appropriate object-cache.phpfile to your wp-
content folder
Memcached Redis APC
See:
or
or
Memcached Object Cache Plugin
WordPress Redis Backend
APC Object Cache Backend
- 94. BENCHMARKS
PHP (No Opcode Cache) ~40
APC ~260
Zend Optimizer + ~307
Number of Requests per second
See http://www.ricardclau.com/2013/03/apc-vs-zend-optimizer-benchmarks-
with-symfony2/
- 98. Leverage browser caching via HTTP headers
Use a CDN if you can or a full-page caching plugin
WordPress' caching APIs are helpful
Set-up object caching on your server
Utilize an opcode cache to speed up PHP
Make sure MySQL's query_cacheis turned on
- 99. MORE READING
1. by Zack Tollman
2. by Joseph Scott
3. by Ryan
Burnette
4. by WordPress.com VIP
5.
by Erick Hitter
6. by Scott Taylor
Core Caching Concepts in WordPress
Scaling WordPress
WordPress Fragment Caching Revisited
Caching
Caching, Scaling, and What I've Learned Programming
for WordPress.com VIP
WordPress + Memcached