Slide 1

Slide 1 text

Caching and You and You and You and You... Justin Yost Lead Software Engineer Wirecutter CC BY-NC 4.0 Justin Yost 1

Slide 2

Slide 2 text

Caching - What is it? • Storing data for faster access in the future. CC BY-NC 4.0 Justin Yost 2

Slide 3

Slide 3 text

Caching - What data? • SQL Results • HTML • Transient Storage CC BY-NC 4.0 Justin Yost 3

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Caching - Transient Storage • Cache and increment the number of failed login attempts CC BY-NC 4.0 Justin Yost 6

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Caching Engines • Stores data basically in a Key/Value Pair Combination (in- memory) CC BY-NC 4.0 Justin Yost 8

Slide 9

Slide 9 text

$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

Slide 10

Slide 10 text

Tradeoffs • Reading vs Writing • Staleness vs Freshness • Speed vs Complexity CC BY-NC 4.0 Justin Yost 10

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Thanks Questions? • twitter.com/justinyost • github.com/justinyost • justinyost.com • linkedin.com/learning/instructors/justin-yost CC BY-NC 4.0 Justin Yost 18