I. Requirement Description 1: linodexenvps1GRAM, 40 sites, frequent I/O, openfilesulimit has increased the access traffic by 65535US, and nginx is occasionally 502. Solution: pagescache, but this vps memory is insufficient, so I installed memcached on several idle servers as a cache cluster, and then set the weight of memcache based on network latency.
I. Requirement Description 1: linode xen vps 1G RAM, 40 sites, frequent IO, openfilesulimit has increased by 65535 US access traffic, nginx is not scheduled 502 II. Solution to pages cache, however, this vps memory is insufficient. Therefore, memcached is installed on several idle servers for cache clusters, and memcache is weighted based on network latency.
I. Requirement Description
One linode xen vps 1g ram, 40 + sites, frequent IO, openfiles ulimit 65535
US time traffic is greatly increased, and nginx does not regularly reach 502
Ii. Solution
Pages cache, but this vps memory is not enough, so I installed memcached on several idle servers as a cache cluster.
Set weights based on network latency.
Memcached server: http://memcached.org/
Iii. Code
Access to memcached server requires php support. You can select the memcached database and memcache database. Note the differences between the two names.
Comparison: https://code.google.com/p/memcached/wiki/PHPClientComparison
I'm using php memcache: http://php.net/manual/en/book.memcache.php because memcache doesn't rely on libmemcached
|
1
2
3
4
5
6
7
8
9
10
11 |
// Cluster Array
$memserver= array(
array( 'host'=> 'fast1.xxx.com', 'port'=> 13579 , 'weight'=> 40 ),
array( 'host'=> 'fast2.xxx.com', 'port'=> 13579 , 'weight'=> 20 ),
array( 'host'=> 'fast3.xxx.com', 'port'=> 13579 , 'weight'=> 20 ),
array( 'host'=> 'slow1.xxx.com', 'port'=> 24680 , 'weight'=> 5 ),
array( 'host'=> 'slow2.xxx.com', 'port'=> 24680 , 'weight'=> 5 ),
array( 'host'=> 'local.xxx.com', 'port'=> 11211 , 'weight'=> 10 )
);
|
There is nothing to say about reading, writing, and deleting key values. Just write them by document sample.
Considering future transplantation problems, check whether the environment supports memcache: class_exists ('memcache ')
The following is my implementation. Due to specific requirements, several functions are written relatively independently, and memcache is not instantiated globally.
You can instantiate memcache globally and optimize operations as needed.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 |
//---get content from memcache---
functiongetCache( $key)
{
Global $memserver;
$ret= false;
$key= md5( $key);
$mc = newMemcache();
foreach( $memserveras $s )
$mc->addServer( $s['host'], $s['port'], FALSE, $s['weight'] );
$ret= $mc->get( $key);
$mc->close();
unset( $mc);
return$ret;
}
//---set content from memcache---
functionsetCache( $key, $val)
{
Global $memserver;
$ret= false;
$key= md5( $key);
$mc = newMemcache();
foreach( $memserveras $s )
$mc->addServer( $s['host'], $s['port'], FALSE, $s['weight'] );
if( isset($val['cached']) )
{
$val['cached'] = 1;
$ret= $mc->set( $key, $val);
}
$mc->close();
unset( $mc);
return$ret;
}
//---delete content from memcache---
functiondelCache( $key)
{
Global $memserver;
$ret= false;
$key= md5( $key);
$mc = newMemcache();
foreach( $memserveras $s )
$mc->addServer( $s['host'], $s['port'], FALSE, $s['weight'] );
$ret= $mc->delete( $key, 0);
$mc->close();
unset( $mc);
return$ret;
}
|