Presentation is loading. Please wait.

Presentation is loading. Please wait.

Revolutionizing enterprise web development Drupal Cache.

Similar presentations

Presentation on theme: "Revolutionizing enterprise web development Drupal Cache."— Presentation transcript:

1 Revolutionizing enterprise web development Drupal Cache

2 Agenda What is Caching? Why Drupal needs cache? Types of caching Drupal’s Cache API Taking Cache further Summary

3 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

4 Why should you cache? Performance Turning on cache admin is simple admin/settings/performance Baked into Drupal Simple to add to your custom modules

5 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

6 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

7 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

8 Global Variable Caching Storage is confined to request lifetime Very Easily implemented Very easily screwed up. First come, first serve. No protection

9 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.

10 Static Variable Caching Storage is confined to request lifetime Stored in memory A little more troublesome to implement { "@context": "", "@type": "ImageObject", "contentUrl": "", "name": "Static Variable Caching Storage is confined to request lifetime Stored in memory A little more troublesome to implement

11 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.

12 Drupal 6 Example drupal_add_js Static variables can become difficult to modify. { "@context": "", "@type": "ImageObject", "contentUrl": "", "name": "Drupal 6 Example drupal_add_js Static variables can become difficult to modify.", "description": "

13 drupal_static Drupal 7 tries to help 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 }

14 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. guide-caching-data-drupal-7

15 Persistent Caching Includes/ cache_set cache_get cache_clear_all D7 cache_get_multiple cache_is_empty

16 Persistent Caching: Example Asked to make a function that returns top 100 Code Monkeys in the USA. { "@context": "", "@type": "ImageObject", "contentUrl": "", "name": "Persistent Caching: Example Asked to make a function that returns top 100 Code Monkeys in the USA.", "description": "

17 Persistent Caching: Example data; } else { // Expensive Query $top_monkeys = ….; cache_set(‘code_monkeys_top:100’, $top_monkeys); } return $top_monkeys; }

18 Persistent Caching: Example Asked to make extend function. This time returns top 100 Code Monkeys in the USA by state.

19 Persistent Caching: Example Broke our cache  … data; } else { // Expensive Query: by state $top_monkeys = ….; cache_set(‘code_monkeys_top:100’, $top_monkeys); } return $top_monkeys; }

20 Persistent Caching: Example Fixed all better… data; } else { // Expensive Query: by state $top_monkeys = ….; cache_set(‘code_monkeys_top:100:’. $state, $top_monkeys); } return $top_monkeys; }

21 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

22 Persistent Caching: Example { "@context": "", "@type": "ImageObject", "contentUrl": "", "name": "Persistent Caching: Example

23 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?

24 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

25 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()

26 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’);

27 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

28 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

29 DrupalCacheInterface DrupalDatabaseCache Default Cache Implementation DrupalFakeCache Used during install when no database is available.

30 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); }

31 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’);

32 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

33 MongoDb MongoDb can handle getMultible calls native. Varied support Memory is memory MongoDb software is supported at various levels. MongDb Queue

34 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?

35 Cache Yo Dawg, I heard you like caching so we cached your cache keys so you’ll cache while you’re caching!

36 Cache Not a good idea…

37 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)

38 Thank You Dagoberto Aceves dago.aceves

Download ppt "Revolutionizing enterprise web development Drupal Cache."

Similar presentations

Ads by Google