380 likes | 548 Vues
Drupal Cache. Revolutionizing enterprise web development. Agenda. What is Caching? Why Drupal needs cache? Types of caching Drupal’s Cache API Taking Cache further Summary. What is Caching ?. The process of storing away expensive ‘ somethings ’ for future use. Cache is a trade-off
E N D
Drupal Cache Revolutionizing enterprise web development
Agenda • What is Caching? • Why Drupal needs cache? • Types of caching • Drupal’s Cache API • Taking Cache further • Summary
What is Caching? • The process of storing away expensive ‘somethings’ for future use. • Cache is a trade-off • Cache cost of creating vs Value of reuse • Cache value increases with every use • Cache is not permanent
Why should you cache? • Performance • Turning on cache admin is simple • admin/settings/performance • Baked into Drupal • Simple to add to your custom modules
When should you cache? • Tricky question • Cache works best with data that doesn’t change often. • What doesn’t change often? • menus • forms • Resized images
Drupal & Cache = Awesome • Drupal would be a Dog without it. • Drupal 6 • Cache used more extensively • Pressflow did it better • Drupal 7 • Learned from Pressflow • Improved support for cache • DrupalCacheInterface
Types of Caching • Attributes of Cache Types • How long do we want to keep it? • Where do we keep it? • Popular caching methods • Global variable caching – ignore this, kinda • Static variable caching – poor mans cache • Persistent caching – meat and potatoes
Global Variable Caching • Storage is confined to request lifetime • Very Easily implemented <?php global $code_monkey_boss; $code_monkey_boss = “DK”; ?> • Very easily screwed up. • First come, first serve. • No protection
Global Variable Caching • Judgment: • Core this is great • $base_url, $base_path, $base_root; • $user • $language • Contributed Modules should avoid… but if you must… If you need to define global variables, their name should start with a single underscore followed by the module/theme name and another underscore.
Static Variable Caching • Storage is confined to request lifetime • Stored in memory • A little more troublesome to implement <?php function code_monkey_needs_soda($monkey, $reset = FALSE) { static $monkeys_needing_soda= array(); if ($reset) $monkeys_needing_soda = array(); if (isset($monkey)) { $monkeys_needing_soda [] = $monkey; } return $monkeys_needing_soda; }
Static Variable Caching • Need a way to reset variable. • Best to add a companion “get” function • You get scope protection! Yey! • Inconsistent implementation • Isn’t always a great solution if you want to allow for variable resetting.
Drupal 6 Example drupal_add_js • Static variables can become difficult to modify. <?php function drupal_add_js($data = NULL, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE, $preprocess = TRUE) { static $javascript= array(); … • No reset… meh… • no drupal_remove_js… -_-
drupal_static • Drupal 7 tries to help • drupal_static - http://dgo.to/a/drupal_static • Provides a more consistent API for resetting any other function’s variables function finest_city_list($field = ’finest_cities') { $finest_cities = &drupal_static(__FUNCTION__); if (!isset($finest_cities)) { // If this function is being called for the first time after a reset, build list $finest_cities = array(“san diego”); } return $finest_cities }
drupal_static • This is a great step forward! • Typically this type of cache means lots of sets and gets. • 2 calls for every 1 • Can be improved? • Yes, but more elbow grease. • http://www.lullabot.com/articles/beginners-guide-caching-data-drupal-7
Persistent Caching • Includes/cache.inc • cache_set • cache_get • cache_clear_all • D7 • cache_get_multiple • cache_is_empty
Persistent Caching: Example Asked to make a function that returns top 100 Code Monkeys in the USA. <?php function code_monkeys_top_100() { // Expensive Query: complex calculation // for code monkey rank, // ordered by rank, limit 100 $top_monkeys = ….; return $top_monkeys; }
Persistent Caching: Example <?php function code_monkeys_top_100() { // Get our monkeys from cache $cache = cache_get(‘code_monkeys_top:100’); if ($cache) { $top_monkeys = $cache->data; } else { // Expensive Query $top_monkeys = ….; cache_set(‘code_monkeys_top:100’, $top_monkeys); } return $top_monkeys; }
Persistent Caching: Example Asked to make extend function. This time returns top 100 Code Monkeys in the USA by state.
Persistent Caching: Example Broke our cache … <?php function code_monkeys_top_for($state) { // Get our monkeys from cache $cache = cache_get(‘code_monkeys_top:100’); if ($cache) { $top_monkeys = $cache->data; } else { // Expensive Query: by state $top_monkeys = ….; cache_set(‘code_monkeys_top:100’, $top_monkeys); } return $top_monkeys; }
Persistent Caching: Example Fixed all better… <?php function code_monkeys_top_for($state) { // Get our monkeys from cache $cache = cache_get(‘code_monkeys_top:100:’ . $state); if ($cache) { $top_monkeys = $cache->data; } else { // Expensive Query: by state $top_monkeys = ….; cache_set(‘code_monkeys_top:100:’ . $state, $top_monkeys); } return $top_monkeys; }
Persistent Caching: Example Code Monkey started complaining that they are not moving up the ranks! • Cache data is no longer valid is a common issue • Are we caching the data for too long? • Better yet… lets invalidate the data
Persistent Caching: Example <?php function code_monkey_add_banana() { // code to add banana // clear the cache data } // Choose your poison cach_clear_all(‘code_monkeys_top:100’) cach_clear_all(‘code_monkeys_top:100:’. $state) cach_clear_all(‘code_monkeys_top:100’, ‘cache’, TRUE) cache_clear_all($cid = NULL, $bin = NULL, $wildcard = FALSE)
Persistent Caching: Keys (cid) • Key Choice is very important in when cache. • Size limits to cache keys. 255 chars • Determines range of cache scope • user? • block? • monkey?
Persistent Caching: Expiry • CACHE_PERMANENT *default* • Item should never be removed unless explicitly told to using cache_clear_all() with a cache ID. • CACHE_TEMPORARY *min cache lifetime* • Item should be removed at the next general cache wipe. • A Unix timestamp • Item should be kept at least until the given time, after which it behaves like CACHE_TEMPORARY. • hook_flush_caches
Persistent Caching: Tables • Core Tables • 'cache', 'cache_block', 'cache_filter', 'cache_page' • Cache is the default table • Great for when we need to cache a lot of items. • Removes burden of having one very large cache table. • Best to support a way to clear your tables. hook_flush_caches()
Persistent Caching: Tables • If you create your own table you need to have the same fields as cache, with the same fields. • Remember your primary_key and indexes • If we created cache_code_monkeys… • .install file for create table • Simple code change cache_set(‘code_monkeys_top:100’, array(‘dk’, ‘diddy’), ‘cache_code_monkeys’); cache_get(‘code_monkeys_top:100’, ‘cache_code_monkeys’);
Cache: Going further • Cache is a tool implemented in many ways. • Cache getting stored to db is slow but not your only choice… • Memcache • MongoDb
DrupalCacheInterface • ::__construct($bin) - Place to store stuff • ::set($cid, $data, $expire) - set one to cache • ::get($cid) - get one from cache • ::getMultiple(&$cids) - get lots from cache • ::isEmpty() - Check if the cache is empty
DrupalCacheInterface • DrupalDatabaseCache • Default Cache Implementation • DrupalFakeCache • Used during install when no database is available.
New Cache Implementation • new CodeMonkeyCache class • New functions? • Override cache set to keep count of gets and sets? • class CodeMonkeyCache extends DrupalDatabaseCache{ • function get($cid) { • // Add to our count table • code_monkey_count_get($cid); • return parent::get($cid); • } • }
Swapping Out Cache Implementation • new CodeMonkeyCache class • Swap out only for our bin cache_code_monkeys • variable_set(‘cache_class_cache_code_monkeys’, ‘CodeMonkeyCache’); • Swap out all bins • variable_set(‘cache_default_class’, ‘CodeMonkeyCache’);
Native Support Varies • Implementations limited by native support. • Memcache does not support getMultiple • Tries it’s best with what it has. • Still better then DB
MongoDb • MongoDb can handle getMultible calls native. • Varied support • Memory is memory • MongoDb software is supported at various levels. • MongDb Queue
Cache: Going further Varnish – cache... stores all the html returned for the server on another machine. Boost – poor mans varnish… but same idea... caches the page... APC – … that’s cache too! A more sophisticated cache that works improves performance on the machine level. .... What will you be caching?
Cache Yo Dawg, I heard you like caching so we cached your cache keys so you’ll cache while you’re caching!
Cache Not a good idea…
Cache: Review Cache works best when data doesn't change often. Cache can be implemented in many ways, static in memory and cache api, choose wisely... When doing persistent caching choose keys wisely. If you're caching a lot of stuffs, consider another table. Cache early, cache often, but remember to cache (varnish, memcache, APC)
Thank You DagobertoAceves www.achieveinternet.com dago.aceves