2. Cache ? Why ?
- Easiest optimisation you could do
- Maximum return on work invested
- Hopefully transparent
- Probably doesn't need a complete redesign
- especially of business logic
- Tradeoff between speed and accuracy
- 80+% of Web content is not mission critical
- scale is more important
4. Sandwich Theory
- Pages are dynamic
- Like a sandwich made to order.
- The whole page is built to order, but bits of
content require varying “freshness”
- A jar of pre-cut olives are the next best thing after
Sliced Bread
9. Active Data Caching
- We're actually in PHP land now
- Two main cache tools
- APC
- Memcache
- APC is php specific
- Memcached is more generic and distributed
11. Red or Blue ?
- APC - Memcached
- Single server - Distributed
- Stores arrays as-is - needs serialization
- small data - large data safe
- data churn causes - handles data churn
issues better
- Does opcodes too - Only data
12. APC: Opcode Cache
- Drop-in & use
- Prevents disk access & syscalls
- Is really old Hat, now
- Tuning & tweaking
- nostat mode
- include_once_override
- locking modes
13. Tuning APC – check list
- You need to tweak APC if you've got
- a large number of files (> 1024)
- a large cache footprint (> 64 M)
- a mess of include_onces
- a highly OOP design
- very static php files
- rsync and inode re-use
15. APC monitoring
- Monitor APC using the apc.php in the pkg
- Provides basic information about
- memory usage
- fragmentation
- cache request/insert rates
- user and file
- expunge/cache-full count
16. Tune your Code - #I
- Reduce include/include_once calls
- Template & stitch
- R3 (http://rthree.sf.net/)
- Use full include paths
- include(“./z.php); instead of include(“z.php”);
- Use constant includes
- don't wrap include with your functions
- Avoid eval() at all costs
19. APC: var_export gotchas
- PHP opcodes are cached in APC
- So, putting a constant array on a file seems OK
- But here's the “constant” array in PHP
line # op ext operands
-------------------------------------------------------
2 0 INIT_ARRAY ~0, 'z', 'x'
3 1 ADD_ARRAY_ELEMENT ~0, 'b', 'a'
4 2 ADD_ARRAY_ELEMENT ~0, 'c', 'b'
5 3 ADD_ARRAY_ELEMENT ~0, 'd', 'c'
4 ASSIGN !0, ~0
7 5 RETURN 1
6 ZEND_HANDLE_EXCEPTION
20. APC: Dynamic Style
- apc_fetch()/apc_store()
- The interface is simple enough
if(!($data = apc_fetch('data'))) {
$data = array( .... );
apc_store('data', $data);
}
- But with a Time To Live (TTL) setting, data expires
22. APC vs Files
Writes Reads
file + includes 17809 1856348.5
file + serialized arrays 13804 517380.2
apc 98586 359790.3
Read/Write performance * Less time means better
Includes
Read
Write
Serialized
APC
0.00 500000.00 1000000.00 1500000.00 2000000.00
http://pooteeweet.org/blog/721
23. APC vs Files
- Serialized arrays are the fastest to write to
- APC is the slowest to write to, but the fastest to
read from
- Disk files with arrays lose out on both fronts
- But then why would you ever use files ?
- Because they expire gracefully, that's why
- So, what happens when a cache entry expires ?
24. Tune your Code - II
- Use APC user cache
- WPcache2, smarty_cache_apc
- Re-order your nested lookups
- NO: $a = apc_fetch(“foo”); echo $a[“baz”];
- YES: $a = apc_fetch(“foo.baz”); echo $a;
- Avoid data race conditions
- shouldn't I fix the race conditions ?
26. What works (or has worked)
- Give it more (virtual) memory to burn
- Use APC user cache for infrequently updated
data
- Update your APC cache out of band
- Cron & Curl are APC's friends
- Always check your APC info for
- cache full count
- fragmentation
27. Thank you for listening
- Resources
- http://pecl.php.net/package/APC
- http://pecl.php.net/package/memcache
- Slides
- http://t3.dotgnu.info/slides/oscon07.pdf
- My Blog
- http://t3.dotgnu.info/blog/php/