Memcached And MySQL
Click here to load reader
-
Upload
chris-barber -
Category
Technology
-
view
4.311 -
download
0
description
Transcript of Memcached And MySQL
Memcached & MySQLSitting in a Tree
Chris Barberhttp://www.cb1inc.com
What is memcached?
A high performance, distributedmemory object caching system
The Server
● Big hash table● Slab allocator● Servers are isolated● No redundancy● No failover● No security● No namespaces● Cross-platform (Linux, FreeBSD, Solaris, OSX)
The Client● C/C++● PHP● Perl● Python● Ruby● Java● MySQL● PostgreSQL● Lua● C#● more...
● Simple protocol● Text based● Handles hashing
– Prepend namespace to key
● Serialize objects● Compress objects
How memcached Works
1. User requests web page2. Web server passes request to PHP3. PHP checks if object is in memcached4. If yes, build page with object5. If no, PHP gets object from DB & stores in memcached6. Send page to user
Integrating memcached
● Web app checks if object is cached● If object not cached
– Load from database, file, web service, etc.– Store in cache– Return object
memcached UDFs for MySQL
● Written in C● Very fast● Uses libmemcached
Prerequisites
● MySQL 5.0 or newer● Make sure MySQL is compiled with –enabled-
shared● memcached 1.2.5 or newer● libmemcached 0.2 or newer
– http://download.tangent.org/libmemcached-0.20.tar.gz
● memcached functions for MySQL 0.4– http://download.tangent.org/memcached_functions_mysql-0.4.tar.gz
● autoconf tools
The UDFs● memc_add● memc_add_by_key● memc_servers_set● memc_set● memc_set_by_key● memc_cas● memc_cas_by_key● memc_get● memc_get_by_key● memc_delete● memc_delete_by_key
● memc_append● memc_append_by_key● memc_prepend● memc_prepend_by_key● memc_increment● memc_decrement● memc_replace● memc_replace_by_key● memc_servers_behavior_set● memc_list_behaviors● memc_stats
Example
SELECT memc_servers_set('localhost:11211');
SELECT memc_set('name', 'Chris');
SELECT memc_get('name') AS name;
Some Ideas
● Use triggers to update cache– When the data is changed (insert, update, delete)
● Use scheduled events (MySQL 5.1)– Flush cache– Repopulate with fresh data– Get memcached statistics
● memc_stats('localhost:11211')
Demo Time!
Questions?
Thank you!