caching basics in php

21
Caching Basics in PHP Presented at The beauty of Web phpXperts seminar 2010

Upload: anis-ahmad

Post on 10-May-2015

30.172 views

Category:

Technology


0 download

DESCRIPTION

Very basics things about caching in PHP. Just what it is, how to do, where to cache etc. A little details about APC.

TRANSCRIPT

Page 1: Caching basics in PHP

Caching Basics in PHP

Presented at

The beauty of WebphpXperts seminar 2010

Page 2: Caching basics in PHP

What is Caching?

“A cache is a temporary storage area where frequently accessed data can be stored for rapid access.”

Page 3: Caching basics in PHP

Why do we need it?

● To reduce the number or retrieval queries made to a database

● To reduce the number of requests made to external services

● To reduce the time spent computing data● To reduce filesystem access

Page 4: Caching basics in PHP

A basic example

● A Post

● 23 comments

● Sidebar Contents

● Navigations

● Categories

● Much more...

We have to load -

Page 5: Caching basics in PHP

1. Check if already cached if(the resource is cached) {

Just display it.}

// TOP of your script$cachefile = 'cache/'.basename($_SERVER['SCRIPT_URI']);

// Serve from the cache if existif (file_exists($cachefile)) { include($cachefile); exit;}

Page 6: Caching basics in PHP

2. Generate content and cacheelse{

Get the resource, Display it and Cache.}

ob_start(); // start the output buffer

// – Your normal PHP script and HTML content here –

// BOTTOM of your script

// save the contents of output buffer to the file$fp = fopen($cachefile, 'w');fwrite($fp, ob_get_contents()); fclose($fp);

ob_end_flush(); // Send the output to the browser

Page 7: Caching basics in PHP

3. Expire the cache

● Event/Trigger

● Setting a time

if(new comment || post edited) {Expire cache for this post

}

$cachetime = 120 * 60; // 2 hours

// Serve from the cache if it is younger than $cachetimeif (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) { include($cachefile); Exit; }

Page 8: Caching basics in PHP

So, It was simple. right?

Request

Output

CacheCompute

Miss

Hit

Store

1

2

3

4

Request

Page 9: Caching basics in PHP

Your question is ...

My pages always have an ever changing content and cannot be cached in their entirety

Page 10: Caching basics in PHP

Different Caching Approaches

● Full page caching● Granular page caching● Content pre­generation● Opcode caching● SQL query caching● Browser caching● Object/Variable caching 

Page 11: Caching basics in PHP

Everybody is doing it!

● Zend Framework● CakePHP● Kohana● Symfony● ...

● Drupal (modules)

● Wordpress (wp­cache)

● Joomla (modules)

● PhpBB● Magento● ...

Frameworks                CMSs

Page 12: Caching basics in PHP

What to cache?

Anything that...● you don't want to fetch or compute every time your code runs.● isn't going to change very often

To find them, monitor ­● Queries● Page loads● Web Analytics

Page 13: Caching basics in PHP

Where to Cache?

● Local Database faster than Remote Database *● Local Disk faster than Database*● RAM faster than Disk*

* Some conditions apply

Page 14: Caching basics in PHP

APC

● APC: Alternative PHP Cache● Free, Open Source Opcode Cache● Content / Object caching● Simple installation● Stores to local, shared memory● Going to be released as part of PHP6

Page 15: Caching basics in PHP

Installation

Basic PECL (http://pecl.php.net)

pecl install apc

Ubuntu/LinuxMintapt­get install php5­apc

Windows(http://pecl4win.php.net )

extension=php_apc.dll

Page 16: Caching basics in PHP

A Quick BenchmarkPHP Native PHP w/APC

Concurrency Level 10

Time taken for tests 60 seconds

Complete requests 298 914

Total transferred 643,149 bytes 1,962,675 bytes

HTML transferred 516,971 bytes 1,582,035 bytes

Requests per second 4.91 [#/sec] 15.21 [#/sec]

Time per request 2035.405 [ms] 657.623 [ms]

Time per request(mean, across all concurrent requests)

203.541 [ms] 65.762 [ms]

Transfer rate 10.35 [KB/s] received 31.88 [KB/s] received

Page 17: Caching basics in PHP

How? Opcode caching

Scanning Lexing Parsing Compilation

Page 18: Caching basics in PHP

Object caching

● apc_add() / apc_store() ­ Cache a variable● apc_fetch() ­ Fetch a stored variable from the cache ● apc_delete() ­ Removes a stored variable from the cache

Page 19: Caching basics in PHP

Monitor apc.php

Page 20: Caching basics in PHP

I am...http://ajaxray.com | http://www.facebook.com/ajaxray | http://twitter.com/ajaxray

Anis uddin AhmadCo­Founder, WNeeds

http://ajaxray.com | [email protected] 

Page 21: Caching basics in PHP

Thank you!

?APC Developers

● George Schlossnagle

● Daniel Cowgill

● Rasmus Lerdorf

● Gopal Vijayaraghavan

● Edin Kadribasic

● Ilia Alshanetsky

● Marcus Börger

● Sara Golemon