This document discusses various caching options for websites to improve performance including flat file caching, RAM disk caching, Memcache caching, and APC caching. Memcache caching stores key-value pairs in memory for fast retrieval and is commonly used by large sites like Facebook and Wikipedia to cache dynamic content, database queries, and other computationally expensive processes. APC caching stores precompiled PHP opcode in shared memory for faster execution. Caching improves performance by reducing memory usage, processing time, disk access, and network traffic.
7. RAM DISK
Mount some memory as disk space
Used when IO actions can’t be avoided
Non resizable on the fly
8. LIGHTTPD + PHP
Lua is a scripting language in lighttpd
Use PHP to write out file to disk
Have Lua look at the modified time of the file, if
within the limit serve else execute PHP
14. HASHING
Key is hashed into a value
Modulous then applied which is the number of the
servers
Server is then picked and stored
If one server drops out you only lose a fraction of the
keys
15. MEMCACHE LIMITS
Key Size is 250 bytes
Data Size is 1 megabyte
32bit/64bit memory limit
No Replication across cluster
16. MEMCACHE CLEANING UP
Uses Least Recently Used Algorithm
Looks for the oldest item and removes
TTL also applies to key / value pairs
18. WHERE TO RUN?
Any server works
No need for matching size
Low CPU usage
19. MEMCACHE + PHP
PECL extension using custom connection
pecl install memcache
Provides MemcachePool and Memcache
INI settings to change hash strategy, function and
protocol.
21. MEMCACHE ACTIONS
Add - only if the key doesn’t exist
Set - will add or update key
Replace - only if the key already exists
22. MEMCACHE TASK
Open Terminal in VMWare Image and run
sudo service memcached start
Try using memcache to set, get and add values
Try setting a value using the Time To Live parameter
Ask questions :-)
28. CACHE LAYERING
Disk /
APC Memcache
Database
Fetch from APC locally
Fetch from Memcache, add to APC
Finally fetch from the original source and store back
in Memcache
29. APC USE CASES
Should always use it to cache opcode of files
Small but frequently accessed things
You only have one server
30. APC TASK
Try storing classes or arrays in APC
Use the Time To Live
APC supports an array for keys in apc_get, fetch
multiple values
32. DATABASE QUERY CACHING
Rely on MySQL to do the query caching
Doesn’t quite work though :-(
Invalidation of cache happens easily
33. ALTERNATIVE DATABASE
CACHING
Store the result set from the database using another
caching software
Hash query as a key value
Extend your DB layer to add this transparently?
34. CACHING TIPS
Pre-heat the cache
Use multiple levels of cache
Cache even dynamic data for short times
serialize() is slow