slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Revolutionizing enterprise web development PowerPoint Presentation
Download Presentation
Revolutionizing enterprise web development

Loading in 2 Seconds...

play fullscreen
1 / 38

Revolutionizing enterprise web development - PowerPoint PPT Presentation


  • 137 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Revolutionizing enterprise web development' - penny


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

Drupal Cache

Revolutionizing

enterprise web development

agenda
Agenda
  • What is Caching?
  • Why Drupal needs cache?
  • Types of caching
  • Drupal’s Cache API
  • Taking Cache further
  • Summary
what is caching
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
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
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 & 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
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
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 caching1
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
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 caching1
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 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_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 static1
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
Persistent Caching
  • Includes/cache.inc
    • cache_set
    • cache_get
    • cache_clear_all
  • D7
    • cache_get_multiple
    • cache_is_empty
persistent caching example
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 example1
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 example2
Persistent Caching: Example

Asked to make extend function.

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

persistent caching example3
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 example4
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 example5
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 example6
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
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
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
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 tables1
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: 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
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
drupalcacheinterface1
DrupalCacheInterface
  • DrupalDatabaseCache
    • Default Cache Implementation
  • DrupalFakeCache
    • Used during install when no database is available.
new cache implementation
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
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
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
  • MongoDb can handle getMultible calls native.
    • Varied support
  • Memory is memory
    • MongoDb software is supported at various levels.
    • MongDb Queue
cache going further1
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
Cache

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

cache1
Cache

Not a good idea…

cache review
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

Thank You

DagobertoAceves

www.achieveinternet.com

dago.aceves