Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Caching and You and You and You and You...

Caching and You and You and You and You...

A run through of what caching is, when to use caching, some common caching engines, and how to use caching in common PHP frameworks.

Justin Yost

May 02, 2019
Tweet

More Decks by Justin Yost

Other Decks in Programming

Transcript

  1. Caching and You and You and You and You... Justin

    Yost Lead Software Engineer Wirecutter CC BY-NC 4.0 Justin Yost 1
  2. Caching - What is it? • Storing data for faster

    access in the future. CC BY-NC 4.0 Justin Yost 2
  3. Caching - What data? • SQL Results • HTML •

    Transient Storage CC BY-NC 4.0 Justin Yost 3
  4. Caching - SQL • Store a common SQL result for

    a complex operation • Doing math for a running average, only need a new result every 15 minutes? • Determining the range of dates a business is open? • Determining the edges of a spatial data set? CC BY-NC 4.0 Justin Yost 4
  5. Caching - HTML • Cache whole static pages • Cache

    a subset of pages that's used across the site; footer, header, etc. CC BY-NC 4.0 Justin Yost 5
  6. Caching - Transient Storage • Cache and increment the number

    of failed login attempts CC BY-NC 4.0 Justin Yost 6
  7. Caching Engines • Redis • Memcached • File • Database

    • There's others at different levels for different problems (Varnish, etc) CC BY-NC 4.0 Justin Yost 7
  8. Caching Engines • Stores data basically in a Key/Value Pair

    Combination (in- memory) CC BY-NC 4.0 Justin Yost 8
  9. $key = inputsToAKey($inputs); if (isCached($key)) { $sqlResults = getFromCache($key); }

    else { $sqlResults = reallyHardProblem($inputs); cache($sqlResults, $key, $dateTimeToExpire); } CC BY-NC 4.0 Justin Yost 9
  10. Tradeoffs • Reading vs Writing • Staleness vs Freshness •

    Speed vs Complexity CC BY-NC 4.0 Justin Yost 10
  11. Memcached • Simpler and smaller • More efficient at storing

    small, staticy subsets of data • Multi-threaded (beefier servers can add more scale) • Typically better at reads over writes. CC BY-NC 4.0 Justin Yost 11
  12. Redis • Stores more complex data sets • Replication support

    • Transactional support • Pub/Sub support • Act as a queue server CC BY-NC 4.0 Justin Yost 12
  13. Caching in Laravel Cache::put('key', 'value', $seconds); // put at key,

    value for x seconds Cache::forever('key', 'value'); // put at key, value forever Cache::forget('key'); // remove value at key Cache::has('key'); // does a value at key exist Cache::get('key', 'default'); // get the value at key, with a default option CC BY-NC 4.0 Justin Yost 13
  14. Caching in Symfony - non PSR 6 $value = $cache->get('key',

    function (Symfony\Contracts\Cache\ItemInterface $item) { $item->expiresAfter($seconds); return 'value'; }); $cache->delete('key'); CC BY-NC 4.0 Justin Yost 14
  15. Caching - PSR 6 $item = $cache->getItem('key'); $item->set('value') $cache->save($item); $item

    = $cache->getItem('key'); $item->isHit(); 'item' = $item->get(); $cache->deleteItem('key'); CC BY-NC 4.0 Justin Yost 15
  16. Caching - CakePHP Cache::write('key', 'value'); Cache::read('key'); // false on no

    data Cache::remember('key', function(){ return 'value'; }); Cache::delete('key'); Cache::readMany(['key1', 'key2']); Cache::writeMany(['key1' => 'value1', 'key2' => 'value2']); Cache::clearGroup('group1', 'group2'); CC BY-NC 4.0 Justin Yost 16
  17. Caching - Phalcon $value = $cache->get('key'); // null on no

    data $cache->exists('key') $cache->save('key, 'value', $seconds); CC BY-NC 4.0 Justin Yost 17