Your SlideShare is downloading. ×
0
CACHING FOR CASH -
    CACHING
     Scott MacVicar
     php|works 2008
WHY CACHE?


Reduce memory usage

Reduce processing time

Reduce network traffic

Reduce disk access
CACHING OPTIONS

Flat file

RAM disk

Database

Memcache

APC

Script level
WHAT TO CACHE?


External resources

Large result sets

Static data

Sessions
FLAT FILE CACHE


Write data out to a local file

Config files from Database or XML

Remote resources store locally

Output b...
CACHING FILES



PEAR::Cache_Lite

Zend_Cache in Zend Framework

var_export in PHP
RAM DISK



Mount some memory as disk space

Used when IO actions can’t be avoided

Non resizable on the fly
LIGHTTPD + PHP



Lua is a scripting language in lighttpd

Use PHP to write out file to disk

Have Lua look at the modified ...
MEMCACHE


Origins within Livejournal

Used by Facebook, Youtube, Wikipedia and digg

Caching daemon, no persistence

Stor...
FACEBOOK EXAMPLE



200 dedicated memcache servers

Each 16GB quad-core amd64

3TB memcache data
MEMCACHE


Slab Allocator

Libevent based (non-blocking)

Simple Protocol

Server is just a hash table

No authentication ...
MEMCACHE CLIENT



Clients provide key and value

Responsible for serialising any value

Compress data
SERVER PROTOCOL


set/replace/add

get

append/prepend

increment/decrement

compare and swap
HASHING


Key is hashed into a value

Modulous then applied which is the number of the
servers

Server is then picked and ...
MEMCACHE LIMITS


Key Size is 250 bytes

Data Size is 1 megabyte

32bit/64bit memory limit

No Replication across cluster
MEMCACHE CLEANING UP



Uses Least Recently Used Algorithm

Looks for the oldest item and removes

TTL also applies to key...
SIMPLE USAGE



Fetch from memcache

If there return

Else calculate, store in cache and return
WHERE TO RUN?



Any server works

No need for matching size

Low CPU usage
MEMCACHE + PHP


PECL extension using custom connection

pecl install memcache

Provides MemcachePool and Memcache

INI se...
MEMCACHE SAMPLE
MEMCACHE ACTIONS



Add - only if the key doesn’t exist

Set - will add or update key

Replace - only if the key already e...
MEMCACHE TASK


Open Terminal in VMWare Image and run
sudo service memcached start

Try using memcache to set, get and add...
MEMCACHEDB



Persistent key/value storage system

BDB used for persitence

Compatible with Memcache protocol
MEMCACHEDB CAVEATS



No expiration

Isn’t a replacement for memcache
APC


Provides OPCode caching in PHP

Shared memory for storage

APC is only on the local server

No network overhead from...
APC
  Input


Tokenizer


 Parser                     APC


Compiler
            Store Opcodes



Executor
APC SAMPLE
CACHE LAYERING

                                         Disk /
   APC            Memcache
                               ...
APC USE CASES



Should always use it to cache opcode of files

Small but frequently accessed things

You only have one ser...
APC TASK



Try storing classes or arrays in APC

Use the Time To Live

APC supports an array for keys in apc_get, fetch
m...
SQLITE



Local node storage of filesystem

Cache result sets from remote database

In memory database
DATABASE QUERY CACHING



Rely on MySQL to do the query caching

Doesn’t quite work though :-(

Invalidation of cache happ...
ALTERNATIVE DATABASE
        CACHING


Store the result set from the database using another
caching software

Hash query a...
CACHING TIPS


Pre-heat the cache

Use multiple levels of cache

Cache even dynamic data for short times

serialize() is s...
QUESTIONS?
Upcoming SlideShare
Loading in...5
×

Caching for Cash: Caching

3,112

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,112
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
62
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Caching for Cash: Caching"

  1. 1. CACHING FOR CASH - CACHING Scott MacVicar php|works 2008
  2. 2. WHY CACHE? Reduce memory usage Reduce processing time Reduce network traffic Reduce disk access
  3. 3. CACHING OPTIONS Flat file RAM disk Database Memcache APC Script level
  4. 4. WHAT TO CACHE? External resources Large result sets Static data Sessions
  5. 5. FLAT FILE CACHE Write data out to a local file Config files from Database or XML Remote resources store locally Output buffering to local file
  6. 6. CACHING FILES PEAR::Cache_Lite Zend_Cache in Zend Framework var_export in PHP
  7. 7. RAM DISK Mount some memory as disk space Used when IO actions can’t be avoided Non resizable on the fly
  8. 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
  9. 9. MEMCACHE Origins within Livejournal Used by Facebook, Youtube, Wikipedia and digg Caching daemon, no persistence Stores key / value pairs
  10. 10. FACEBOOK EXAMPLE 200 dedicated memcache servers Each 16GB quad-core amd64 3TB memcache data
  11. 11. MEMCACHE Slab Allocator Libevent based (non-blocking) Simple Protocol Server is just a hash table No authentication or self awareness
  12. 12. MEMCACHE CLIENT Clients provide key and value Responsible for serialising any value Compress data
  13. 13. SERVER PROTOCOL set/replace/add get append/prepend increment/decrement compare and swap
  14. 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. 15. MEMCACHE LIMITS Key Size is 250 bytes Data Size is 1 megabyte 32bit/64bit memory limit No Replication across cluster
  16. 16. MEMCACHE CLEANING UP Uses Least Recently Used Algorithm Looks for the oldest item and removes TTL also applies to key / value pairs
  17. 17. SIMPLE USAGE Fetch from memcache If there return Else calculate, store in cache and return
  18. 18. WHERE TO RUN? Any server works No need for matching size Low CPU usage
  19. 19. MEMCACHE + PHP PECL extension using custom connection pecl install memcache Provides MemcachePool and Memcache INI settings to change hash strategy, function and protocol.
  20. 20. MEMCACHE SAMPLE
  21. 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. 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 :-)
  23. 23. MEMCACHEDB Persistent key/value storage system BDB used for persitence Compatible with Memcache protocol
  24. 24. MEMCACHEDB CAVEATS No expiration Isn’t a replacement for memcache
  25. 25. APC Provides OPCode caching in PHP Shared memory for storage APC is only on the local server No network overhead from TCP/IP
  26. 26. APC Input Tokenizer Parser APC Compiler Store Opcodes Executor
  27. 27. APC SAMPLE
  28. 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. 29. APC USE CASES Should always use it to cache opcode of files Small but frequently accessed things You only have one server
  30. 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
  31. 31. SQLITE Local node storage of filesystem Cache result sets from remote database In memory database
  32. 32. DATABASE QUERY CACHING Rely on MySQL to do the query caching Doesn’t quite work though :-( Invalidation of cache happens easily
  33. 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. 34. CACHING TIPS Pre-heat the cache Use multiple levels of cache Cache even dynamic data for short times serialize() is slow
  35. 35. QUESTIONS?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×