Hi, Im Ben.
benramsey.com @ramsey joind.in/1599
What is a cache?
A cache is a collection of data duplicating original values stored elsewhere or computed earlier, where the original data is expensive to fetch (owing to longer access time) or to compute, compared to the cost of reading the cache. Wikipedia
A cache is a temporary storage area where frequently accessed data can be stored for rapid access.
To reduce the number of requests made To reduce the time spent computing data To reduce filesystem access
Types of caches
File system Database Shared memory RAM disk Object cache (memcached and APC) Opcode cache (APC)
Memcached
What is memcached?
Distributed memory object caching Acts as a simple key/value dictionary Runs as a daemon Has a simple protocol for client access
over TCP and UDP
Memcached principles
Fast asynchronous network I/O Not a persistent data store It does not provide redundancy Data is not replicated across the cluster It doesnt handle failover
Memcached principles
Daemons are not aware of each other It does not provide authentication Works great on a small and local-area
network
memcached
www www
memcached
memcached
www
memcached
www www
memcached
memcached
www
Storage commands:
Retrieval command: get, gets Deletion command: delete Increment/decrement: incr, decr Other commands:
$> telnet localhost 11211 Trying ::1... Connected to localhost. Escape character is '^]'. set foobar 0 0 15 This is a test. STORED get foobar VALUE foobar 0 15 This is a test. END quit Connection closed by foreign host. $>
$memcache = new Memcached(); $memcache->addServers(array( array('10.35.24.1', '11211'), array('10.35.24.2', '11211'), array('10.35.24.3', '11211'), ));
$book = $memcache->get('0764596349'); if ($book === false) { if ($memcache->getResultCode() == Memcached::RES_NOTFOUND) { $book = Book::getByIsbn('0764596349'); $memcache->set($book->getCacheKey(), $book); } }
Memcached limits
Key size has a 250 byte limit Value can not be larger than 1 MB Memory limits for 32bit/64bit systems Replication not built-in; dependent on the
client
pecl/memcached
pecl/memcached basics
Andrei Zmievski authored the extension Now at a stable version 1.0.2 http://php.net/memcached
Memcached methods
getResultMessage() getServerList() getStats() increment() prepend() replace() set() setMulti() ... and more!
apc
www www
apc
apc
www
What is APC?
Gives information about file upload Stores to local, shared memory Not distributed http://php.net/apc
For opcode caching, just install the For memory allocation, change
apc.stat (set to 0)
To speed things up even more, turn off Set and forget but it also does object
storage
could potentially fill up with stale entries while newer ones wont be cached; if greater than zero, APC will attempt to remove expired entries
example.com/
index.php
APC
public/index.php library/Zend/Application.php library/Zend/Loader/Autoloader.php library/Zend/Loader.php library/Zend/Config/Ini.php library/Zend/Config.php application/Bootstrap.php library/Zend/Application/Bootstrap/Bootstrap.php library/Zend/Application/Bootstrap/BootstrapAbstract.php library/Zend/Application/Bootstrap/Bootstrapper.php library/Zend/Application/Bootstrap/ ResourceBootstrapper.php library/Zend/Application/Module/Autoloader.php library/Zend/Loader/Autoloader/Resource.php library/Zend/Loader/Autoloader/Interface.php library/Zend/Loader/PluginLoader.php ... 48 more files ...
example.com/
index.php
APC
public/index.php library/Zend/Application.php library/Zend/Loader/Autoloader.php library/Zend/Loader.php library/Zend/Config/Ini.php library/Zend/Config.php application/Bootstrap.php library/Zend/Application/Bootstrap/Bootstrap.php library/Zend/Application/Bootstrap/BootstrapAbstract.php library/Zend/Application/Bootstrap/Bootstrapper.php library/Zend/Application/Bootstrap/ ResourceBootstrapper.php library/Zend/Application/Module/Autoloader.php library/Zend/Loader/Autoloader/Resource.php library/Zend/Loader/Autoloader/Interface.php library/Zend/Loader/PluginLoader.php ... 48 more files ...
/welcome
Magic!
language en fr key HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO ... ... ... translation Hello Bonjour Hola Hallo Hallo Hei Dia duit Ol ... ... ...
en.php
background process
data base
APC functions
Advanced APC
Application settings Configuration Data that is the same for each user Requests are guaranteed to go to the
same machine (i.e. sticky sessions) sticky sessions)
Questions?
Thank you!
Ben Ramsey
benramsey.com @ramsey joind.in/1599