SlideShare a Scribd company logo
Caching Data For Performance Dave Ross West Suburban Chicago PHP Meetup February 7, 2008
Caching It's faster to get information from somewhere close...
Caching Than somewhere far away.
It's all about scalability. (seriously)
Where? Generally speaking... Local Database faster than Shared Database Local Disk faster than Database RAM faster than Disk
What? Anything you don't want to fetch or compute every time your code runs. Anything that isn't going to change very often.
Everybody's Doing It! WP-Cache (Wordpress) Drupal has it built-in memcached (LiveJournal, Slashdot, WikiPedia)
Even PHP Itself Is Getting Cached PHP opcode caches compile your scripts and run these pre-parsed versions instead. Zend Optimizer APC (Alternative PHP Cache) Xcache eAccelerator ionCube
A Few Things I Cached Recently Access Control List Page metadata Entire CMS pages
Caching On Disk Make a big array of the data you need. // At the start of your script $array = unserialize(file_get_contents($fileName)); // If the data isn't already there, put it there if(array_key_exists('key', $array)) { $value = $array['key']; } else { // Retrieve or compute $value $value = “Hello, World!”; $array['key'] = $value; } // At the end of your script file_put_contents($fileName, serialize($array));
Of Course, the OS Caches Files Modern operating systems use all your free RAM as a disk cache. If you read the same file over & over, it's probably being read out of RAM the second time on. So, it's usually ok to read a small file over & over.
memcached “ memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.”
memcached (It's a big array that doesn't go away when your PHP program ends.)
memcached <?php $memcache = new Memcache; $memcache->connect('localhost', 11211) or die (&quot;Could not connect&quot;); $version = $memcache->getVersion(); echo &quot;Server's version: &quot;.$version.&quot;<br/>\n&quot;; $tmp_object = new stdClass; $tmp_object->str_attr = 'test'; $tmp_object->int_attr = 123; $memcache->set('key', $tmp_object, false, 10) or die (&quot;Failed to save data at the server&quot;); echo &quot;Store data in the cache (data will expire in 10 seconds)<br/>\n&quot;; $get_result = $memcache->get('key'); echo &quot;Data from the cache:<br/>\n&quot;; var_dump($get_result); ?>
Caveats Be careful caching anything where security matters. Don't cache credit card info (PCI compliance) Keep cache up-to-date (when data changes)
For More Information Caching In General http://en.wikipedia.org/wiki/Cache PHP Accelerators/Opcode Caches http://en.wikipedia.org/wiki/PHP_accelerator http://www.zend.com/en/products/guard/optimizer memcached http://www.danga.com/memcached/

More Related Content

PDF
WordPress Performance & Scalability
PPT
Zend Con 2008 Slides
PDF
Scaling WordPress
PPT
Speeding Up The Snail
PDF
Caching basics in PHP
PDF
Drupal feature proposal: two new stream-wrappers
PDF
Memcached Presentation
PPTX
Memcached B box presentation
WordPress Performance & Scalability
Zend Con 2008 Slides
Scaling WordPress
Speeding Up The Snail
Caching basics in PHP
Drupal feature proposal: two new stream-wrappers
Memcached Presentation
Memcached B box presentation

What's hot (20)

PPTX
PHP Performance with APC + Memcached
PPT
High Performance Wordpress
PPTX
Varnish bof
PDF
From One to a Cluster
PDF
Caching for Cash: Caching
PDF
Introduction to performance tuning perl web applications
PPTX
Building Scalable Web Apps - LVL.UP KL
PDF
Phorum MySQL tricks
PPTX
WP-CLI Workshop at WordPress Meetup Cluj-Napoca
PDF
Cache all the things - A guide to caching Drupal
PPT
Memcache
PDF
Drupal7 MEMCACHE
PDF
Caching for Cash: Benchmarking and Profiling
PPTX
Mini-Training: To cache or not to cache
PPTX
Drupal, varnish, esi - Toulouse November 2
PDF
Scaling symfony apps
PPTX
Caching
PDF
Less and faster – Cache tips for WordPress developers
PPTX
Using memcache to improve php performance
PPT
High Availabiltity & Replica Sets with mongoDB
PHP Performance with APC + Memcached
High Performance Wordpress
Varnish bof
From One to a Cluster
Caching for Cash: Caching
Introduction to performance tuning perl web applications
Building Scalable Web Apps - LVL.UP KL
Phorum MySQL tricks
WP-CLI Workshop at WordPress Meetup Cluj-Napoca
Cache all the things - A guide to caching Drupal
Memcache
Drupal7 MEMCACHE
Caching for Cash: Benchmarking and Profiling
Mini-Training: To cache or not to cache
Drupal, varnish, esi - Toulouse November 2
Scaling symfony apps
Caching
Less and faster – Cache tips for WordPress developers
Using memcache to improve php performance
High Availabiltity & Replica Sets with mongoDB
Ad

Viewers also liked (20)

PDF
Van Gogh-Borges
KEY
FRT Vol. 5 クラウド時代の企業アプリケーションとマーケティング
PPT
Trabalhando com o Moodle e a Comunidade
PDF
Global Knowledge Training Courses & Promotion 2015-Sep
PDF
STelligence Savvius Thai Datasheet
PPT
MoodleMoot Brasil 2011 - O Moodle na UFSC (Infraestrutura de TI)
PDF
ThaiCert Phishing and Malicious Code Infographic 2015
PDF
SQL Server 簡易診断サービス ご紹介資料
PDF
SQL Server 現状診断サービス ご紹介資料
PDF
OSSV [Open System SnapVault]
PDF
[INSIGHT OUT 2011] C12 50分で理解する SQL Serverでできることできないこと(uchiyama)
PPT
Driver development – memory management
PDF
Sql server 構築 運用 tips
PDF
Board support package_on_linux
PDF
45分で理解する SQL Serverでできることできないこと
PPTX
Sql server 運用 101
PDF
Linux for embedded_systems
PPT
Kernel module programming
PDF
HANAのハナシの基本のき
PPT
Top 10 Interview Questions
Van Gogh-Borges
FRT Vol. 5 クラウド時代の企業アプリケーションとマーケティング
Trabalhando com o Moodle e a Comunidade
Global Knowledge Training Courses & Promotion 2015-Sep
STelligence Savvius Thai Datasheet
MoodleMoot Brasil 2011 - O Moodle na UFSC (Infraestrutura de TI)
ThaiCert Phishing and Malicious Code Infographic 2015
SQL Server 簡易診断サービス ご紹介資料
SQL Server 現状診断サービス ご紹介資料
OSSV [Open System SnapVault]
[INSIGHT OUT 2011] C12 50分で理解する SQL Serverでできることできないこと(uchiyama)
Driver development – memory management
Sql server 構築 運用 tips
Board support package_on_linux
45分で理解する SQL Serverでできることできないこと
Sql server 運用 101
Linux for embedded_systems
Kernel module programming
HANAのハナシの基本のき
Top 10 Interview Questions
Ad

Similar to Caching Data For Performance (20)

PPT
Everyone loves PHP
ODP
Caching and tuning fun for high scalability @ phpBenelux 2011
ODP
Caching and tuning fun for high scalability @ FrOSCon 2011
KEY
Site Performance - From Pinto to Ferrari
ODP
WP Sandbox Presentation WordCamp Toronto 2011
ODP
Caching and tuning fun for high scalability
ODP
phptek13 - Caching and tuning fun tutorial
ODP
Caching and tuning fun for high scalability @ PHPTour
PPT
Lamp Stack Optimization
PPT
Performance and Scalability
ODP
Caching and tuning fun for high scalability @ FOSDEM 2012
PDF
Caching with Memcached and APC
PDF
Scaling PHP apps
PDF
Caching objects-in-memory
ODP
MNPHP Scalable Architecture 101 - Feb 3 2011
ODP
Caching and tuning fun for high scalability
PPT
Drupalcamp Estonia - High Performance Sites
PPT
Drupalcamp Estonia - High Performance Sites
PPTX
Jug Lugano - Scale over the limits
PPT
0628阙宏宇
Everyone loves PHP
Caching and tuning fun for high scalability @ phpBenelux 2011
Caching and tuning fun for high scalability @ FrOSCon 2011
Site Performance - From Pinto to Ferrari
WP Sandbox Presentation WordCamp Toronto 2011
Caching and tuning fun for high scalability
phptek13 - Caching and tuning fun tutorial
Caching and tuning fun for high scalability @ PHPTour
Lamp Stack Optimization
Performance and Scalability
Caching and tuning fun for high scalability @ FOSDEM 2012
Caching with Memcached and APC
Scaling PHP apps
Caching objects-in-memory
MNPHP Scalable Architecture 101 - Feb 3 2011
Caching and tuning fun for high scalability
Drupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance Sites
Jug Lugano - Scale over the limits
0628阙宏宇

More from Dave Ross (20)

KEY
Stylesheets of the future with Sass and Compass
KEY
HTML5 History & Features
PPT
A geek's guide to getting hired
KEY
NoSQL & MongoDB
PDF
Date and Time programming in PHP & Javascript
KEY
Simulated Eye Tracking with Attention Wizard
KEY
What's new in HTML5?
KEY
The Canvas Tag
KEY
Wordpress
PPT
The FPDF Library
PPT
FirePHP
PPT
Bayesian Inference using b8
PPT
SQL Injection in PHP
KEY
Web App Security: XSS and CSRF
KEY
The Mobile Web: A developer's perspective
KEY
Balsamiq Mockups
KEY
LAMP Optimization
KEY
Lint - PHP & Javascript Code Checking
KEY
Cufon - Javascript Font Replacement
KEY
PHP Output Buffering
Stylesheets of the future with Sass and Compass
HTML5 History & Features
A geek's guide to getting hired
NoSQL & MongoDB
Date and Time programming in PHP & Javascript
Simulated Eye Tracking with Attention Wizard
What's new in HTML5?
The Canvas Tag
Wordpress
The FPDF Library
FirePHP
Bayesian Inference using b8
SQL Injection in PHP
Web App Security: XSS and CSRF
The Mobile Web: A developer's perspective
Balsamiq Mockups
LAMP Optimization
Lint - PHP & Javascript Code Checking
Cufon - Javascript Font Replacement
PHP Output Buffering

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation theory and applications.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
Teaching material agriculture food technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Network Security Unit 5.pdf for BCA BBA.
NewMind AI Weekly Chronicles - August'25 Week I
Advanced methodologies resolving dimensionality complications for autism neur...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Big Data Technologies - Introduction.pptx
Empathic Computing: Creating Shared Understanding
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Teaching material agriculture food technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectral efficient network and resource selection model in 5G networks
Understanding_Digital_Forensics_Presentation.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Network Security Unit 5.pdf for BCA BBA.

Caching Data For Performance

  • 1. Caching Data For Performance Dave Ross West Suburban Chicago PHP Meetup February 7, 2008
  • 2. Caching It's faster to get information from somewhere close...
  • 4. It's all about scalability. (seriously)
  • 5. Where? Generally speaking... Local Database faster than Shared Database Local Disk faster than Database RAM faster than Disk
  • 6. What? Anything you don't want to fetch or compute every time your code runs. Anything that isn't going to change very often.
  • 7. Everybody's Doing It! WP-Cache (Wordpress) Drupal has it built-in memcached (LiveJournal, Slashdot, WikiPedia)
  • 8. Even PHP Itself Is Getting Cached PHP opcode caches compile your scripts and run these pre-parsed versions instead. Zend Optimizer APC (Alternative PHP Cache) Xcache eAccelerator ionCube
  • 9. A Few Things I Cached Recently Access Control List Page metadata Entire CMS pages
  • 10. Caching On Disk Make a big array of the data you need. // At the start of your script $array = unserialize(file_get_contents($fileName)); // If the data isn't already there, put it there if(array_key_exists('key', $array)) { $value = $array['key']; } else { // Retrieve or compute $value $value = “Hello, World!”; $array['key'] = $value; } // At the end of your script file_put_contents($fileName, serialize($array));
  • 11. Of Course, the OS Caches Files Modern operating systems use all your free RAM as a disk cache. If you read the same file over & over, it's probably being read out of RAM the second time on. So, it's usually ok to read a small file over & over.
  • 12. memcached “ memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.”
  • 13. memcached (It's a big array that doesn't go away when your PHP program ends.)
  • 14. memcached <?php $memcache = new Memcache; $memcache->connect('localhost', 11211) or die (&quot;Could not connect&quot;); $version = $memcache->getVersion(); echo &quot;Server's version: &quot;.$version.&quot;<br/>\n&quot;; $tmp_object = new stdClass; $tmp_object->str_attr = 'test'; $tmp_object->int_attr = 123; $memcache->set('key', $tmp_object, false, 10) or die (&quot;Failed to save data at the server&quot;); echo &quot;Store data in the cache (data will expire in 10 seconds)<br/>\n&quot;; $get_result = $memcache->get('key'); echo &quot;Data from the cache:<br/>\n&quot;; var_dump($get_result); ?>
  • 15. Caveats Be careful caching anything where security matters. Don't cache credit card info (PCI compliance) Keep cache up-to-date (when data changes)
  • 16. For More Information Caching In General http://en.wikipedia.org/wiki/Cache PHP Accelerators/Opcode Caches http://en.wikipedia.org/wiki/PHP_accelerator http://www.zend.com/en/products/guard/optimizer memcached http://www.danga.com/memcached/