See the cache in action

  1. <?php
  2. /**
  3.  * Cache controller
  4.  *
  5.  * @author Mark Nielsen
  6.  * @version $Id$
  7.  * @package blocks/helloworld
  8.  */
  9.  
  10. defined('MOODLE_INTERNAL'or die('Direct access to this script is forbidden.');
  11.  
  12. class block_helloworld_controller_cache extends mr_controller_block {
  13.     /**
  14.      * Default screen
  15.      *
  16.      * Demo of cache
  17.      */
  18.     public function view_action({
  19.         $this->print_header();
  20.         echo $this->output->heading('Demo of mr_cache');
  21.         $this->helper->highlight(__CLASS____FUNCTION__);
  22.         echo $this->output->box_start('generalbox boxaligncenter boxwidthnormal');
  23.  
  24.         #### DEMO CODE ####
  25.         // Note: you can make your very own instance of mr_cache!
  26.         // $this->helper->cache is EXACTLY the same as:
  27.         // $cache = new mr_cache('blocks_helloworld_');
  28.  
  29.         // Try to load cache ID = time_string from cache
  30.         if (!$string $this->helper->cache->load('time_string')) {
  31.             // Failed to get from cache, create a new string to cache
  32.             $string 'Last cache: '.userdate(time()'%A, %d %B %Y, %r');
  33.  
  34.             // Save the string to cache using cache ID = time_string
  35.             $this->helper->cache($string'time_string');
  36.  
  37.             // Note: the above line is EXACTLY the same as:
  38.             // $this->helper->cache->save($string, 'time_string');
  39.         }
  40.         $this->helper->dump($string'Currently cached value');
  41.  
  42.         // See if a cached ID exists or not
  43.         if (!$this->helper->cache->test('time_string')) {
  44.             // In this case, if we get here, its bad, cache isn't working
  45.             $this->notify->add_string('cache doesnt exist');
  46.         }
  47.  
  48.         // EXAMPLE:
  49.         // Slightly different code when storing data that fails to pass is_string()
  50.         // Data like this, gets serialized, need to tell load method to unserialize it like so
  51.         // if (!$array = $this->helper->cache->load('array_example', true)) {  // ADDED: true
  52.         //     $array = array('foo', 'bar', $string);
  53.         //
  54.         //     // Save as we normally do, mr_cache will automatically serialize $array
  55.         //     $this->helper->cache($array, 'array_example');
  56.         // }
  57.         // print_object($array);
  58.         #### DEMO CODE ####
  59.  
  60.         echo $this->output->single_button($this->url->out(falsearray('action' => 'delete'))'Delete cache');
  61.         echo $this->output->box_end();
  62.         $this->print_footer();
  63.     }
  64.  
  65.     /**
  66.      * Delete cached item
  67.      *
  68.      * Demo of cache
  69.      */
  70.     public function delete_action({
  71.         // Remove the cached item
  72.         $this->helper->cache->remove('time_string');
  73.  
  74.         $this->notify->add_string('cache reset'mr_notify::GOOD);
  75.         redirect($this->url);
  76.     }
  77. }

Documentation generated on Thu, 28 Jun 2012 16:33:47 -0700 by phpDocumentor 1.4.3