SlideShare a Scribd company logo
1 of 29
Download to read offline
Zend_Cache: how to improve the
performance of PHP applications
by Enrico Zimuel (enrico.z@zend.com)
Senior Consultant ­ Zend Technologies




October 31th, PHP Barcelona Conference - 2009



                                        Copyright © 2007, Zend Technologies Inc.
About me

                     Enrico Zimuel
                     • I come from Turin (Italy). 
                     • Senior Consultant at Zend Technologies in Italy.
                     • Software Engineer since 1996. 
                     • Open source contributor: XCheck, Core XPath
                     • I was a Research Programmer at the Informatics 
                     Institute of the University of Amsterdam.
                     • ZCE, ZFCE also proficiency in C/C++, Java, Perl, 
                     Javascript. 
                     • B.Sc. in Computer Science and Economics


  My website: http://www.zimuel.it ­ Blog: http://www.zimuel.it/blog
Summary


 • Keys of performance of a PHP application
 • The caching mechanism
 • Zend Framework and Zend_Cache
 • Using the Zend_Cache to cache data and pages
 • Benchmarking different caching: Files, APC, 
   Memcache, Zend Server CE


                                  Name of this section           | | 3
                                                         Nov 2, 2009
Performance 


 • How to improve the performance of a PHP 
   application or more in general a piece of 
   software?

    Optimize the code (make the code faster)

    Using a caching mechanism (reduce the 
     amount of code to execute)



                                    Name of this section           | | 4
                                                           Nov 2, 2009
What's a cache?


• In computer science, a cache is a collection of data 
  duplicating original values, stored elsewhere or computed 
  earlier, where the original data is expensive to fetch 
  (owing to longer access time) or to compute, compared to 
  the cost of reading the cache
                               From Wikipedia, the free encyclopedia




                                              Name of this section           | | 5
                                                                     Nov 2, 2009
Caching: key concepts


• Unique identifier (a string), usually named key, used to 
  identify cache records.

• Lifetime, how long the cached resource is considered 
  updated.

• Conditional execution, that parts of your code can be 
  skipped entirely, boosting performance.




                                            Name of this section           | | 6
                                                                   Nov 2, 2009
Conditional execution

              Read from cache            The goal is to reduce the
                                         numbers of execution of
                                         this part of code
                                NO
                 Updated?



              YES                    Retrieve the data




                                     Update in cache



               Use the data



                                      Name of this section           | | 7
                                                             Nov 2, 2009
Caching in PHP


• In PHP we can use different kinds of cache systems:

      File, by hand using the fwrite, fread, readfile, etc
      Pear::Cache_Lite, using the files as cache storage
      APC, using the apc extension (apc_add, apc_fetch, etc)
      Xcache, from the lighttpd web server project
      Memcache, using the memcache extension (Memcache::add, 
       Memcache::get, etc)
    Zend Server/CE, using Zend Server API (zend_shm_cache_fetch, 
       zend_shm_cache_store, etc)




                                                      Name of this section           | | 8
                                                                             Nov 2, 2009
Zend_Cache


• Zend_Cache is a general class of the Zend Framework 
  to cache any kind of data: object, string, array, function, 
  class, page, etc
• Zend_Cache uses a Front class to access the data and a 
  Backend class to manage the data
    Same methods to access the cache for differents backend
    You can switch from a backend to another without modify your 
     code
• It can be used WITHOUT the MVC part of the Zend 
  Framework!


                                               Name of this section           | | 9
                                                                      Nov 2, 2009
Zend_Cache_Frontend


• The core of the module (Zend_Cache_Core) is generic, 
  flexible and configurable
• Yet, for your specific needs there are cache frontends 
  that extend Zend_Cache_Core for convenience:
    Output
    File
    Function
    Class
    Pages


                                       Name of this section           | | 10
                                                              Nov 2, 2009
Zend_Cache_Backend


• Zend_Cache has different backends to store the 
  caching data:
      Zend_Cache_Backend_File
      Zend_Cache_Backend_Sqlite
      Zend_Cache_Backend_Memcached
      Zend_Cache_Backend_Apc
      Zend_Cache_Backend_Xcache
      Zend_Cache_Backend_ZendPlatform
      Zend_Cache_Backend_ZendServer
      Zend_Cache_Backend_TwoLevels




                                         Name of this section           | | 11
                                                                Nov 2, 2009
A first example: caching a SQL query


  require_once 'Zend/Cache.php';
  $frontendOptions = array(
     'lifetime' => 7200, // cache lifetime of 2 hours
     'automatic_serialization' => true
  );
  $backendOptions = array(
      'cache_dir' => '/tmp/cache' 
  );
  $cache = Zend_Cache::factory('Core',
                               'File',
                               $frontendOptions,
                               $backendOptions);
  ...


                                                        Name of this section           | | 12
                                                                               Nov 2, 2009
A first example (2): caching a SQL query


  ...
  // check if the 'myresult' key is present into the cache
  if (!$result = $cache­>load('myresult')) {
      // cache miss; connect to the database
      require_once 'Zend/Db.php';
      $db = Zend_Db::factory([...]);
      $result = $db­>fetchAll('SELECT * FROM table');
      // update the cache
      $cache­>save($result, 'myresult');
  }
  // use the $result
  print_r($result);



                                                     Name of this section           | | 13
                                                                            Nov 2, 2009
Caching the output with Zend_Cache


• Zend_Cache_Frontend_Output is an output­capturing 
    frontend. 
•   It utilizes output buffering in PHP to capture everything 
    between its start() and end()  methods. 
• We can use this cache to store single parts of a page
• Using an MVC architecture we use this caching into the 
    View




                                             Name of this section           | | 14
                                                                    Nov 2, 2009
Output caching: example


  require_once 'Zend/Cache.php';
  $frontendOptions = array(
     'lifetime' => 30, // cache lifetime of 30 seconds
     'automatic_serialization' => false
  );
  $backendOptions = array(
      'cache_dir' => '/tmp/cache' 
  );
  $cache = Zend_Cache::factory('Output',
                               'File',
                               $frontendOptions,
                               $backendOptions);
  ...


                                                         Name of this section           | | 15
                                                                                Nov 2, 2009
Output caching (2): example


  ...
  // we pass a unique identifier to the start() method
  if (!$cache­>start('mypage')) {
      // output as usual:
      echo '<h1>Hello world!</h1> ';
      echo '<h3>This is cached ('.date ('H:m:s').')</h3>';
      $cache­>end(); // the output is saved and sent to the browser
  }

  echo '<h3>This is never cached ('.date ('H:m:s').').</h3>';




                                                   Name of this section           | | 16
                                                                          Nov 2, 2009
Full page caching with Zend_Cache


• Zend_Cache_Frontend_Page is designed to cache a 
  complete page. 
• The key of the cache value is calculated automatically 
  with $_SERVER['REQUEST_URI'] and (depending on 
  options) $_GET, $_POST, $_SESSION, $_COOKIE, 
  $_FILES. 
• Moreover, you have only one method to call, start(), 
  because the end() call is fully automatic when the page 
  is ended. 




                                          Name of this section           | | 17
                                                                 Nov 2, 2009
Full page caching with Zend_Cache (2)


• Using an MVC architecture with a front end controller 
  you can build a centralized cache management in the 
  bootstrap file 

• The Zend_Cache_Frontend_Page comes with a 
  regexps param to cache only specific pages:
    an associative array to set options only for some 
     REQUEST_URI, keys are (PCRE) regexps, values 
     are associative arrays with specific options to set if 
     the regexp matchs on $_SERVER['REQUEST_URI']


                                           Name of this section           | | 18
                                                                  Nov 2, 2009
Tagging Records


  • On the main problem of the cache mechanism is the 
    invalidation of cache values

  • To update or delete a value in the cache you have to 
    know the unique id of the value.

  • Build a good key generation system is a challenge.

  • The Zend_Cache uses a tag system to group together 
    cache values. In this way you can invalidate a set of 
    cache values using tags.


                                          Name of this section           | | 19
                                                                 Nov 2, 2009
Tagging Records (2)


  • When you save a cache with the save() method, you 
    can set an array of tags to apply for this record:

    $cache­>save($data, 'myKey', array('tagA', 'tagB'));

  • Then you will be able to clean all cache records tagged 
    with a given tag (or tags).

  • At the moment the only backends that support the tag 
    system are: File, Sqlite, ZendPlatform. Anyway you 
    can always use tags with TwoLevels backend, we will 
    see it.
                                          Name of this section           | | 20
                                                                 Nov 2, 2009
Cleaning the cache


  • To remove or invalidate in particular cache id, you can 
    use the remove() method: $cache­>remove('myKey');

  • To remove or invalidate several cache ids in one 
    operation, you can use the clean() method. For 
    example to remove all cache records : 

  // clean all records
  $cache­>clean(Zend_Cache::CLEANING_MODE_ALL);

  // clean only outdated
  $cache­>clean(Zend_Cache::CLEANING_MODE_OLD);


                                          Name of this section           | | 21
                                                                 Nov 2, 2009
Cleaning the cache with tags


  • If you want to remove cache entries matching the tags 
    'tagA' and 'tagB': 

      $cache­>clean(
          Zend_Cache::CLEANING_MODE_MATCHING_TAG,
          array('tagA', 'tagB')
      );

  • You can clean using different boolean conditions:
    OR = Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG
    NOT = Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG


                                         Name of this section           | | 22
                                                                Nov 2, 2009
A special backend: TwoLevels


  • This backend is an hybrid one. It stores cache records 
    in two other backends: a fast one like APC and a 
    "slow" one like File.

  • This backend will use the priority parameter (given at 
    the frontend level when storing a record) and the 
    remaining space in the fast backend to optimize the 
    usage of these two backends. 




                                         Name of this section           | | 23
                                                                Nov 2, 2009
Benchmarking Zend_Cache backends


• I provided an experiment to benchmark the Zend_Cache 
  using differents backends: File, APC, Memcached, 
  ZendServerCE (disk/ram)
• The Experiment:
  I tested the execution times of writing, reading and deleting 100 
  values from the cache (each value was an array of 100 elements). I 
  run the experiment 10 times and I elaborate the averages of the 
  results.
• I run the experiment on my laptop:
  Gnu/Linux Ubuntu 9.04, Kernel 2.6.28, CPU Intel Centrino vPro 
  2.01Ghz, RAM 2 Gb, HD 250 Gb, Zend Server CE 4.0.5, Apache 
  2.2.12, PHP 5.3, ZendFramework 1.9.4, Memcached 1.2.2


                                                Name of this section           | | 24
                                                                       Nov 2, 2009
Benchmarking Zend_Cache: results


 Backends                             Writing        Reading         Removing

 File                                  84,850             8,964               3,562

 APC                                    8,471             4,846               1,548

 Memcached                             27,129           11,259                7,467

 ZendServerCE_Disk                     25,323             6,005               8,681

 ZendServerCE_ShMem                     9,996             4,785               2,532


 All the times are execution times in milliseconds


                                                     Name of this section           | | 25
                                                                            Nov 2, 2009
Benchmarking Zend_Cache: graphic

                                    Benchmarking Zend_Cache banckends
                        90


                        80


                        70


                        60
response times (msec)




                        50                                                                           Writing
                                                                                                     Reading

                        40                                                                           Removing



                        30


                        20


                        10


                         0
                             File    APC        Memcached   ZendServerCE_Disk   ZendServerCE_ShMem
                                               backends




                                                                       Name of this section           | | 26
                                                                                              Nov 2, 2009
Caching with Zend_Cache: best practices


 • To cache SQL result use the MD5 of the SQL string as 
   key for the cache value
 • Estimate the lifetime of the cache in a real environment 
   (it depends on traffic!)
 • Always use the tag system to cache data
 • Don't delete a single cache value. It's better to clean 
   using the tag system or clean all the cache!
 • On a single server the faster cache systems are APC 
   and ZendServer/CE using the memory (ShMem)



                                           Name of this section           | | 27
                                                                  Nov 2, 2009
Questions?




             Name of this section           | | 28
                                    Nov 2, 2009
Thank you!

For more info: http://www.zend.com
                  http://framework.zend.com

More Related Content

What's hot

Zend Framework 2 - presentation
Zend Framework 2 - presentationZend Framework 2 - presentation
Zend Framework 2 - presentation
yamcsha
 
[MathWorks] Versioning Infrastructure
[MathWorks] Versioning Infrastructure[MathWorks] Versioning Infrastructure
[MathWorks] Versioning Infrastructure
Perforce
 
Tuning the Performance of Your ColdFusion Environment to Racecar Specs!
Tuning the Performance of Your ColdFusion Environment to Racecar Specs!Tuning the Performance of Your ColdFusion Environment to Racecar Specs!
Tuning the Performance of Your ColdFusion Environment to Racecar Specs!
Hostway|HOSTING
 
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Michael Pirnat
 
WE18_Performance_Up.ppt
WE18_Performance_Up.pptWE18_Performance_Up.ppt
WE18_Performance_Up.ppt
webhostingguy
 

What's hot (20)

Magento 2 Workflows
Magento 2 WorkflowsMagento 2 Workflows
Magento 2 Workflows
 
Improving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve InternetImproving Website Performance with Memecached Webinar | Achieve Internet
Improving Website Performance with Memecached Webinar | Achieve Internet
 
Как мы взломали распределенные системы конфигурационного управления
Как мы взломали распределенные системы конфигурационного управленияКак мы взломали распределенные системы конфигурационного управления
Как мы взломали распределенные системы конфигурационного управления
 
Performance all teh things
Performance all teh thingsPerformance all teh things
Performance all teh things
 
HBaseConEast2016: Practical Kerberos with Apache HBase
HBaseConEast2016: Practical Kerberos with Apache HBaseHBaseConEast2016: Practical Kerberos with Apache HBase
HBaseConEast2016: Practical Kerberos with Apache HBase
 
Java Enterprise Edition Concurrency Misconceptions
Java Enterprise Edition Concurrency Misconceptions Java Enterprise Edition Concurrency Misconceptions
Java Enterprise Edition Concurrency Misconceptions
 
Zend Framework 2 - presentation
Zend Framework 2 - presentationZend Framework 2 - presentation
Zend Framework 2 - presentation
 
PHP language presentation
PHP language presentationPHP language presentation
PHP language presentation
 
Apache Camel: The Swiss Army Knife of Open Source Integration
Apache Camel: The Swiss Army Knife of Open Source IntegrationApache Camel: The Swiss Army Knife of Open Source Integration
Apache Camel: The Swiss Army Knife of Open Source Integration
 
Apache Cookbook - TekX Chicago 2010
Apache Cookbook - TekX Chicago 2010Apache Cookbook - TekX Chicago 2010
Apache Cookbook - TekX Chicago 2010
 
Adobe AEM Maintenance - Customer Care Office Hours
Adobe AEM Maintenance - Customer Care Office HoursAdobe AEM Maintenance - Customer Care Office Hours
Adobe AEM Maintenance - Customer Care Office Hours
 
Drupal, varnish, esi - Toulouse November 2
Drupal, varnish, esi - Toulouse November 2Drupal, varnish, esi - Toulouse November 2
Drupal, varnish, esi - Toulouse November 2
 
DSpace UI Prototype Challenge: Spring Boot + Thymeleaf
DSpace UI Prototype Challenge: Spring Boot + ThymeleafDSpace UI Prototype Challenge: Spring Boot + Thymeleaf
DSpace UI Prototype Challenge: Spring Boot + Thymeleaf
 
Sizing your alfresco platform
Sizing your alfresco platformSizing your alfresco platform
Sizing your alfresco platform
 
are available here
are available hereare available here
are available here
 
[MathWorks] Versioning Infrastructure
[MathWorks] Versioning Infrastructure[MathWorks] Versioning Infrastructure
[MathWorks] Versioning Infrastructure
 
The new repository in AEM 6
The new repository in AEM 6The new repository in AEM 6
The new repository in AEM 6
 
Tuning the Performance of Your ColdFusion Environment to Racecar Specs!
Tuning the Performance of Your ColdFusion Environment to Racecar Specs!Tuning the Performance of Your ColdFusion Environment to Racecar Specs!
Tuning the Performance of Your ColdFusion Environment to Racecar Specs!
 
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
 
WE18_Performance_Up.ppt
WE18_Performance_Up.pptWE18_Performance_Up.ppt
WE18_Performance_Up.ppt
 

Similar to Zend_Cache: how to improve the performance of PHP applications

CHI-YAPC-2009
CHI-YAPC-2009CHI-YAPC-2009
CHI-YAPC-2009
jonswar
 
Workshop eZ Publish Caching Mechanisms
Workshop eZ Publish Caching MechanismsWorkshop eZ Publish Caching Mechanisms
Workshop eZ Publish Caching Mechanisms
Kaliop-slide
 
Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010
isnull
 
Jug Lugano - Scale over the limits
Jug Lugano - Scale over the limitsJug Lugano - Scale over the limits
Jug Lugano - Scale over the limits
Davide Carnevali
 

Similar to Zend_Cache: how to improve the performance of PHP applications (20)

Zend Server Data Caching
Zend Server Data CachingZend Server Data Caching
Zend Server Data Caching
 
Caching with Memcached and APC
Caching with Memcached and APCCaching with Memcached and APC
Caching with Memcached and APC
 
phptek13 - Caching and tuning fun tutorial
phptek13 - Caching and tuning fun tutorialphptek13 - Caching and tuning fun tutorial
phptek13 - Caching and tuning fun tutorial
 
CHI-YAPC-2009
CHI-YAPC-2009CHI-YAPC-2009
CHI-YAPC-2009
 
Caching objects-in-memory
Caching objects-in-memoryCaching objects-in-memory
Caching objects-in-memory
 
Workshop eZ Publish Caching Mechanisms
Workshop eZ Publish Caching MechanismsWorkshop eZ Publish Caching Mechanisms
Workshop eZ Publish Caching Mechanisms
 
Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010
 
Drupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hoodDrupal 8 Cache: Under the hood
Drupal 8 Cache: Under the hood
 
eZ Publish cluster unleashed revisited
eZ Publish cluster unleashed revisitedeZ Publish cluster unleashed revisited
eZ Publish cluster unleashed revisited
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
Give Your Site a Boost with Memcache
Give Your Site a Boost with MemcacheGive Your Site a Boost with Memcache
Give Your Site a Boost with Memcache
 
Pecl Picks
Pecl PicksPecl Picks
Pecl Picks
 
A quick start on Zend Framework 2
A quick start on Zend Framework 2A quick start on Zend Framework 2
A quick start on Zend Framework 2
 
Jug Lugano - Scale over the limits
Jug Lugano - Scale over the limitsJug Lugano - Scale over the limits
Jug Lugano - Scale over the limits
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
 
eZ Publish Caching Mechanisms
eZ Publish Caching MechanismseZ Publish Caching Mechanisms
eZ Publish Caching Mechanisms
 
Accelerate your ColdFusion Applications using Caching
Accelerate your ColdFusion Applications using CachingAccelerate your ColdFusion Applications using Caching
Accelerate your ColdFusion Applications using Caching
 
Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ phpBenelux 2011Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ phpBenelux 2011
 
Asp.net caching
Asp.net cachingAsp.net caching
Asp.net caching
 
Scaling in Mind (Case study of Drupal Core)
Scaling in Mind (Case study of Drupal Core)Scaling in Mind (Case study of Drupal Core)
Scaling in Mind (Case study of Drupal Core)
 

More from Enrico Zimuel

More from Enrico Zimuel (20)

Password (in)security
Password (in)securityPassword (in)security
Password (in)security
 
Integrare Zend Framework in Wordpress
Integrare Zend Framework in WordpressIntegrare Zend Framework in Wordpress
Integrare Zend Framework in Wordpress
 
Quick start on Zend Framework 2
Quick start on Zend Framework 2Quick start on Zend Framework 2
Quick start on Zend Framework 2
 
Cryptography with Zend Framework
Cryptography with Zend FrameworkCryptography with Zend Framework
Cryptography with Zend Framework
 
Introduzione alla Posta Elettronica Certificata (PEC): le regole tecniche
Introduzione alla Posta Elettronica Certificata (PEC): le regole tecnicheIntroduzione alla Posta Elettronica Certificata (PEC): le regole tecniche
Introduzione alla Posta Elettronica Certificata (PEC): le regole tecniche
 
Zend Framework 2 quick start
Zend Framework 2 quick startZend Framework 2 quick start
Zend Framework 2 quick start
 
PHP goes mobile
PHP goes mobilePHP goes mobile
PHP goes mobile
 
Zend Framework 2
Zend Framework 2Zend Framework 2
Zend Framework 2
 
Cryptography in PHP: use cases
Cryptography in PHP: use casesCryptography in PHP: use cases
Cryptography in PHP: use cases
 
Manage cloud infrastructures in PHP using Zend Framework 2 (and 1)
Manage cloud infrastructures in PHP using Zend Framework 2 (and 1)Manage cloud infrastructures in PHP using Zend Framework 2 (and 1)
Manage cloud infrastructures in PHP using Zend Framework 2 (and 1)
 
Manage cloud infrastructures using Zend Framework 2 (and ZF1)
Manage cloud infrastructures using Zend Framework 2 (and ZF1)Manage cloud infrastructures using Zend Framework 2 (and ZF1)
Manage cloud infrastructures using Zend Framework 2 (and ZF1)
 
Framework software e Zend Framework
Framework software e Zend FrameworkFramework software e Zend Framework
Framework software e Zend Framework
 
Strong cryptography in PHP
Strong cryptography in PHPStrong cryptography in PHP
Strong cryptography in PHP
 
Velocizzare Joomla! con Zend Server Community Edition
Velocizzare Joomla! con Zend Server Community EditionVelocizzare Joomla! con Zend Server Community Edition
Velocizzare Joomla! con Zend Server Community Edition
 
XCheck a benchmark checker for XML query processors
XCheck a benchmark checker for XML query processorsXCheck a benchmark checker for XML query processors
XCheck a benchmark checker for XML query processors
 
Introduzione alle tabelle hash
Introduzione alle tabelle hashIntroduzione alle tabelle hash
Introduzione alle tabelle hash
 
Crittografia quantistica: fantascienza o realtà?
Crittografia quantistica: fantascienza o realtà?Crittografia quantistica: fantascienza o realtà?
Crittografia quantistica: fantascienza o realtà?
 
Introduzione alla crittografia
Introduzione alla crittografiaIntroduzione alla crittografia
Introduzione alla crittografia
 
Crittografia è sinonimo di sicurezza?
Crittografia è sinonimo di sicurezza?Crittografia è sinonimo di sicurezza?
Crittografia è sinonimo di sicurezza?
 
Sviluppo di applicazioni sicure
Sviluppo di applicazioni sicureSviluppo di applicazioni sicure
Sviluppo di applicazioni sicure
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Zend_Cache: how to improve the performance of PHP applications

  • 1. Zend_Cache: how to improve the performance of PHP applications by Enrico Zimuel (enrico.z@zend.com) Senior Consultant ­ Zend Technologies October 31th, PHP Barcelona Conference - 2009 Copyright © 2007, Zend Technologies Inc.
  • 2. About me Enrico Zimuel • I come from Turin (Italy).  • Senior Consultant at Zend Technologies in Italy. • Software Engineer since 1996.  • Open source contributor: XCheck, Core XPath • I was a Research Programmer at the Informatics  Institute of the University of Amsterdam. • ZCE, ZFCE also proficiency in C/C++, Java, Perl,  Javascript.  • B.Sc. in Computer Science and Economics My website: http://www.zimuel.it ­ Blog: http://www.zimuel.it/blog
  • 3. Summary • Keys of performance of a PHP application • The caching mechanism • Zend Framework and Zend_Cache • Using the Zend_Cache to cache data and pages • Benchmarking different caching: Files, APC,  Memcache, Zend Server CE Name of this section | | 3 Nov 2, 2009
  • 4. Performance  • How to improve the performance of a PHP  application or more in general a piece of  software?  Optimize the code (make the code faster)  Using a caching mechanism (reduce the  amount of code to execute) Name of this section | | 4 Nov 2, 2009
  • 5. What's a cache? • In computer science, a cache is a collection of data  duplicating original values, stored elsewhere or computed  earlier, where the original data is expensive to fetch  (owing to longer access time) or to compute, compared to  the cost of reading the cache From Wikipedia, the free encyclopedia Name of this section | | 5 Nov 2, 2009
  • 6. Caching: key concepts • Unique identifier (a string), usually named key, used to  identify cache records. • Lifetime, how long the cached resource is considered  updated. • Conditional execution, that parts of your code can be  skipped entirely, boosting performance. Name of this section | | 6 Nov 2, 2009
  • 7. Conditional execution Read from cache The goal is to reduce the numbers of execution of this part of code NO Updated? YES Retrieve the data Update in cache Use the data Name of this section | | 7 Nov 2, 2009
  • 8. Caching in PHP • In PHP we can use different kinds of cache systems:  File, by hand using the fwrite, fread, readfile, etc  Pear::Cache_Lite, using the files as cache storage  APC, using the apc extension (apc_add, apc_fetch, etc)  Xcache, from the lighttpd web server project  Memcache, using the memcache extension (Memcache::add,  Memcache::get, etc)  Zend Server/CE, using Zend Server API (zend_shm_cache_fetch,  zend_shm_cache_store, etc) Name of this section | | 8 Nov 2, 2009
  • 9. Zend_Cache • Zend_Cache is a general class of the Zend Framework  to cache any kind of data: object, string, array, function,  class, page, etc • Zend_Cache uses a Front class to access the data and a  Backend class to manage the data  Same methods to access the cache for differents backend  You can switch from a backend to another without modify your  code • It can be used WITHOUT the MVC part of the Zend  Framework! Name of this section | | 9 Nov 2, 2009
  • 10. Zend_Cache_Frontend • The core of the module (Zend_Cache_Core) is generic,  flexible and configurable • Yet, for your specific needs there are cache frontends  that extend Zend_Cache_Core for convenience:  Output  File  Function  Class  Pages Name of this section | | 10 Nov 2, 2009
  • 11. Zend_Cache_Backend • Zend_Cache has different backends to store the  caching data:  Zend_Cache_Backend_File  Zend_Cache_Backend_Sqlite  Zend_Cache_Backend_Memcached  Zend_Cache_Backend_Apc  Zend_Cache_Backend_Xcache  Zend_Cache_Backend_ZendPlatform  Zend_Cache_Backend_ZendServer  Zend_Cache_Backend_TwoLevels Name of this section | | 11 Nov 2, 2009
  • 12. A first example: caching a SQL query require_once 'Zend/Cache.php'; $frontendOptions = array(    'lifetime' => 7200, // cache lifetime of 2 hours    'automatic_serialization' => true ); $backendOptions = array(     'cache_dir' => '/tmp/cache'  ); $cache = Zend_Cache::factory('Core',                              'File',                              $frontendOptions,                              $backendOptions); ... Name of this section | | 12 Nov 2, 2009
  • 13. A first example (2): caching a SQL query ... // check if the 'myresult' key is present into the cache if (!$result = $cache­>load('myresult')) {     // cache miss; connect to the database     require_once 'Zend/Db.php';     $db = Zend_Db::factory([...]);     $result = $db­>fetchAll('SELECT * FROM table');     // update the cache     $cache­>save($result, 'myresult'); } // use the $result print_r($result); Name of this section | | 13 Nov 2, 2009
  • 14. Caching the output with Zend_Cache • Zend_Cache_Frontend_Output is an output­capturing  frontend.  • It utilizes output buffering in PHP to capture everything  between its start() and end()  methods.  • We can use this cache to store single parts of a page • Using an MVC architecture we use this caching into the  View Name of this section | | 14 Nov 2, 2009
  • 15. Output caching: example require_once 'Zend/Cache.php'; $frontendOptions = array(    'lifetime' => 30, // cache lifetime of 30 seconds    'automatic_serialization' => false ); $backendOptions = array(     'cache_dir' => '/tmp/cache'  ); $cache = Zend_Cache::factory('Output',                              'File',                              $frontendOptions,                              $backendOptions); ... Name of this section | | 15 Nov 2, 2009
  • 16. Output caching (2): example ... // we pass a unique identifier to the start() method if (!$cache­>start('mypage')) {     // output as usual:     echo '<h1>Hello world!</h1> ';     echo '<h3>This is cached ('.date ('H:m:s').')</h3>';     $cache­>end(); // the output is saved and sent to the browser } echo '<h3>This is never cached ('.date ('H:m:s').').</h3>'; Name of this section | | 16 Nov 2, 2009
  • 17. Full page caching with Zend_Cache • Zend_Cache_Frontend_Page is designed to cache a  complete page.  • The key of the cache value is calculated automatically  with $_SERVER['REQUEST_URI'] and (depending on  options) $_GET, $_POST, $_SESSION, $_COOKIE,  $_FILES.  • Moreover, you have only one method to call, start(),  because the end() call is fully automatic when the page  is ended.  Name of this section | | 17 Nov 2, 2009
  • 18. Full page caching with Zend_Cache (2) • Using an MVC architecture with a front end controller  you can build a centralized cache management in the  bootstrap file  • The Zend_Cache_Frontend_Page comes with a  regexps param to cache only specific pages:  an associative array to set options only for some  REQUEST_URI, keys are (PCRE) regexps, values  are associative arrays with specific options to set if  the regexp matchs on $_SERVER['REQUEST_URI'] Name of this section | | 18 Nov 2, 2009
  • 19. Tagging Records • On the main problem of the cache mechanism is the  invalidation of cache values • To update or delete a value in the cache you have to  know the unique id of the value. • Build a good key generation system is a challenge. • The Zend_Cache uses a tag system to group together  cache values. In this way you can invalidate a set of  cache values using tags. Name of this section | | 19 Nov 2, 2009
  • 20. Tagging Records (2) • When you save a cache with the save() method, you  can set an array of tags to apply for this record: $cache­>save($data, 'myKey', array('tagA', 'tagB')); • Then you will be able to clean all cache records tagged  with a given tag (or tags). • At the moment the only backends that support the tag  system are: File, Sqlite, ZendPlatform. Anyway you  can always use tags with TwoLevels backend, we will  see it. Name of this section | | 20 Nov 2, 2009
  • 21. Cleaning the cache • To remove or invalidate in particular cache id, you can  use the remove() method: $cache­>remove('myKey'); • To remove or invalidate several cache ids in one  operation, you can use the clean() method. For  example to remove all cache records :  // clean all records $cache­>clean(Zend_Cache::CLEANING_MODE_ALL); // clean only outdated $cache­>clean(Zend_Cache::CLEANING_MODE_OLD); Name of this section | | 21 Nov 2, 2009
  • 22. Cleaning the cache with tags • If you want to remove cache entries matching the tags  'tagA' and 'tagB':      $cache­>clean(         Zend_Cache::CLEANING_MODE_MATCHING_TAG,         array('tagA', 'tagB')     ); • You can clean using different boolean conditions: OR = Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG NOT = Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG Name of this section | | 22 Nov 2, 2009
  • 23. A special backend: TwoLevels • This backend is an hybrid one. It stores cache records  in two other backends: a fast one like APC and a  "slow" one like File. • This backend will use the priority parameter (given at  the frontend level when storing a record) and the  remaining space in the fast backend to optimize the  usage of these two backends.  Name of this section | | 23 Nov 2, 2009
  • 24. Benchmarking Zend_Cache backends • I provided an experiment to benchmark the Zend_Cache  using differents backends: File, APC, Memcached,  ZendServerCE (disk/ram) • The Experiment: I tested the execution times of writing, reading and deleting 100  values from the cache (each value was an array of 100 elements). I  run the experiment 10 times and I elaborate the averages of the  results. • I run the experiment on my laptop: Gnu/Linux Ubuntu 9.04, Kernel 2.6.28, CPU Intel Centrino vPro  2.01Ghz, RAM 2 Gb, HD 250 Gb, Zend Server CE 4.0.5, Apache  2.2.12, PHP 5.3, ZendFramework 1.9.4, Memcached 1.2.2 Name of this section | | 24 Nov 2, 2009
  • 25. Benchmarking Zend_Cache: results Backends Writing Reading Removing File 84,850 8,964 3,562 APC 8,471 4,846 1,548 Memcached 27,129 11,259 7,467 ZendServerCE_Disk 25,323 6,005 8,681 ZendServerCE_ShMem 9,996 4,785 2,532 All the times are execution times in milliseconds Name of this section | | 25 Nov 2, 2009
  • 26. Benchmarking Zend_Cache: graphic Benchmarking Zend_Cache banckends 90 80 70 60 response times (msec) 50 Writing Reading 40 Removing 30 20 10 0 File APC Memcached ZendServerCE_Disk ZendServerCE_ShMem backends Name of this section | | 26 Nov 2, 2009
  • 27. Caching with Zend_Cache: best practices • To cache SQL result use the MD5 of the SQL string as  key for the cache value • Estimate the lifetime of the cache in a real environment  (it depends on traffic!) • Always use the tag system to cache data • Don't delete a single cache value. It's better to clean  using the tag system or clean all the cache! • On a single server the faster cache systems are APC  and ZendServer/CE using the memory (ShMem) Name of this section | | 27 Nov 2, 2009
  • 28. Questions? Name of this section | | 28 Nov 2, 2009
  • 29. Thank you! For more info: http://www.zend.com        http://framework.zend.com