Dec 08 2010

How-to install memcache in ubuntu and Zend Framework cache configuration

Publicado por octante en 3:30 am bajo PHP,programación

Install Memcache in ubuntu:

To install memcache in ubutu we need to install it:
sudo apt-get install memcached

Install php memcache module to use it in php scripts:
sudo apt-get install php5-memcache

Edit memcache configuration file /etc/php5/apache2/conf.d/memcache.ini, delete semi-colon in line: “extension=memcache.so”, and restart memcache daemon:

sudo /etc/init.d/memcache restart

To use memcache in Zend Framework we can initialize it in bootstrap file, in this case we use memcache to store routing configuration file.

protected function _initRoutes () {
   $backendType = 'Memcached';
   $backendOptions = array ();
   //$backendOptions = array('cache_dir' => './tmp/'); // Optional
 
   $frontendOptions = array(
      'automatic_serialization' => true,
      'master_files' => array(APPLICATION_PATH . '/configs/router.xml'),
      'lifetime' => 120,                   // cache lifetime of 120 seconds
   );
 
   // Instantiate a caching object for caching the routes
   $cache = Zend_Cache::factory('File', $backendType,
      $frontendOptions,
      $backendOptions
   );
 
   $frontController = Zend_Controller_Front::getInstance();
 
   if(!$router = $cache->load('router')) {
      // Load up .xml file and put the results in the cache
      $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/router.xml');
      $router = Zend_Controller_Front::getInstance()->getRouter();
      $router->addConfig($config, 'routes');
 
      $cache->save($router, 'router');
   } else {
      // Use cached version
      $frontController->setRouter($router);
   }
}

We can use memcache to optimize database connections:

if( ($result = $cache->load('myresult')) === false ) {
   // cache miss; connect to the database
   $db = Zend_Db::factory( [...] );
   $result = $db->fetchAll('SELECT * FROM huge_table');
   $cache->save($result, 'myresult');
} else {
   // cache hit! shout so that we know
   echo "This one is from cache!\n\n";
}

To save html in memcache:

Controller code:

$frontendOptions = array(
   'lifetime' => 30,                   // cache lifetime of 30 seconds
   'automatic_serialization' => false  // this is the default anyways
);
 
$backendOptions = array();
 
$this->view->cache = Zend_Cache::factory('Output',
                                   'Memcached',
                                   $frontendOptions,
                                   $backendOptions);

View code:

if(!$this->cache->start('page_section')) {
      // output as usual:
      echo 'Hello world! ';
      echo time ();
 
      $this->cache->end(); // the output is saved and sent to the browser
}

To remove an item from cache:
$cache->remove('idToRemove');

To remove all items from cache:
$cache->clean(Zend_Cache::CLEANING_MODE_ALL);

And finally, to debug cache, set debug_header to true in frontendOptions:
'debug_header' => true

  • No hay posts relacionados.

Aún no hay respuestas

Trackback URI | RSS de comentarios

Leave a Reply