Memcached Distributed Cache
We use PHP to connect multiple memcached servers and do distributed caching, which is implemented as follows:
$memcache New Memcache; $memcache->addserver (' 192.168.252.134 ', 11211); $memcache->addserver (' 192.168.252.134 ', 11212); $memcache->addserver (' 192.168.252.134 ', 11213); for ($i$i$i+ +) {$memcache->set ($i$i , 0, +);}
This implementation is the most straightforward, simple, PHP application directly connected to the memcached server. But there is also a problem, is the availability of the problem, if, there is a memcached server down, then the data on the server is lost, it may be believed that memcached is to do the cache, the loss of the cache is lost Bai, no fuss. In fact, the cache hit rate is also affecting the system stability and high availability of an indicator well, every time we lose the cache, we are so Xinkuan "go to the database to find the data is good", so the cache has meaning.
Magent--memcached Cache Proxy Server
For the above problem, you can use the Magent cache proxy Server to manage the memcached server, and the PHP application is transformed from direct and multiple memcached interactions into and magent interactions. So, what did magent do for us?
Let's take a look at the picture first
As can be seen, magent as a memcached cluster agent, you can also do backup (192.168.252.134:11213 as a backup server), the primary cache server (192.168.252.134:11211, 192.168.252.134:11212), applications can still read from the backup server to the cache after the outage.
Build magent Cache Proxy Server
Magent relies on Libevent library
Magent relies on Libevent library
Magent relies on Libevent library
Important Words three times!!
1) Compile and install Magent
CD/homemkdir /./-C https://memagent.googlecode.com/files/ magent-0.5.tar.gz //Google is wall up, if necessary can comment to stay in the mailbox tar xzvf./magent-0.5.tar. gz/sbin/-i "s#libs =-levent#libs =-levent-lm#g"./magent/usr/bin/magent
The compilation process will encounter a lot of problems, refer to this site to solve their own
2) The construction of memcached is skipped
3) Start memcached and Magent
Magent-u root-l 192.168.252.134-p 11210-s 192.168.252.134:11211-s 192.168.252.134:11212-b 192.168.252.134:11213
4) Run the test script
<?PHP$host= ' 192.168.252.134 ';$port= ' 11210 ';$mem= Memcache_connect ($host,$port);$key 1= ' Mctest1 ';$value 1= ' 1 ';$mem->add ($key 1,$value 1);$key 2= ' Mctest2 ';$value 2= ' 2 ';$mem->add ($key 2,$value 2);$key 3= ' Mctest3 ';$value 3= ' 3 ';$mem->add ($key 3,$value 3);$key 4= ' Mctest4 ';$value 4= ' 4 ';$mem->add ($key 4,$value 4);$key 5= ' Mctest5 ';$value 5= ' 5 ';$mem->add ($key 5,$value 5);$key 6= ' Mctest6 ';$value 6= ' 6 ';$mem->add ($key 6,$value 6);
Server 192.168.252.134:11211
Server 192.168.252.134:11212
Server 192.168.252.134:11213
The data on the primary server is backed up to the backup server, and the application can still fetch data from the backup server if the primary server goes down.
More deployment diagrams
This article link: http://www.hcoding.com/?p=316
Original articles, reproduced please specify: jc&hcoding.com
Magent--memcached Cache Proxy Server