revolutionizing enterprise web development

38
Revolutionizi ng enterprise web development Drupal Cache

Upload: penny

Post on 23-Feb-2016

34 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Revolutionizing enterprise web development

Revolutionizingenterprise web

development

Drupal Cache

Page 2: Revolutionizing enterprise web development

Agenda

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

Page 3: Revolutionizing enterprise web development

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

Page 4: Revolutionizing enterprise web development

Why should you cache?

• Performance• Turning on cache admin is simple• admin/settings/performance

• Baked into Drupal• Simple to add to your custom modules

Page 5: Revolutionizing enterprise web development

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

Page 6: Revolutionizing enterprise web development

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

Page 7: Revolutionizing enterprise web development

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

Page 8: Revolutionizing enterprise web development

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

Page 9: Revolutionizing enterprise web development

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.

Page 10: Revolutionizing enterprise web development

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;

}

Page 11: Revolutionizing enterprise web development

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.

Page 12: Revolutionizing enterprise web development

Drupal 6 Exampledrupal_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… -_-

Page 13: Revolutionizing enterprise web development

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

}

Page 14: Revolutionizing enterprise web development

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

Page 15: Revolutionizing enterprise web development

Persistent Caching• Includes/cache.inc• cache_set• cache_get• cache_clear_all

• D7• cache_get_multiple• cache_is_empty

Page 16: Revolutionizing enterprise web development

Persistent Caching: Example

Asked to make a function that returns top 100 Code Monkeys in the USA.<?phpfunction code_monkeys_top_100() { // Expensive Query: complex calculation // for code monkey rank, // ordered by rank, limit 100 $top_monkeys = ….; return $top_monkeys;}

Page 17: Revolutionizing enterprise web development

Persistent Caching: Example<?phpfunction 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;}

Page 18: Revolutionizing enterprise web development

Persistent Caching: Example

Asked to make extend function.

This time returns top 100 Code Monkeys in the USA by state.

Page 19: Revolutionizing enterprise web development

Persistent Caching: Example

Broke our cache …<?phpfunction 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;}

Page 20: Revolutionizing enterprise web development

Persistent Caching: Example

Fixed all better…<?phpfunction 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;}

Page 21: Revolutionizing enterprise web development

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

Page 22: Revolutionizing enterprise web development

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)

Page 23: Revolutionizing enterprise web development

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?

Page 24: Revolutionizing enterprise web development

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

Page 25: Revolutionizing enterprise web development

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

Page 26: Revolutionizing enterprise web development

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

Page 27: Revolutionizing enterprise web development

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

Page 28: Revolutionizing enterprise web development

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

Page 29: Revolutionizing enterprise web development

DrupalCacheInterface

• DrupalDatabaseCache• Default Cache Implementation

• DrupalFakeCache• Used during install when no database is

available.

Page 30: Revolutionizing enterprise web development

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

}

}

Page 31: Revolutionizing enterprise web development

Swapping Out Cache Implementation

• new CodeMonkeyCache class• Swap out only for our bin cache_code_monkeysvariable_set(‘cache_class_cache_code_monkeys’, ‘CodeMonkeyCache’);

• Swap out all binsvariable_set(‘cache_default_class’, ‘CodeMonkeyCache’);

Page 32: Revolutionizing enterprise web development

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

Page 33: Revolutionizing enterprise web development

MongoDb

•MongoDb can handle getMultible calls native.• Varied support

•Memory is memory• MongoDb software is supported at various

levels.• MongDb Queue

Page 34: Revolutionizing enterprise web development

Cache: Going furtherVarnish – 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?

Page 35: Revolutionizing enterprise web development

Cache

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

Page 36: Revolutionizing enterprise web development

Cache

Not a good idea…

Page 37: Revolutionizing enterprise web development

Cache: ReviewCache 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)

Page 38: Revolutionizing enterprise web development

Thank YouDagoberto Aceves

www.achieveinternet.comdago.aceves