Memcache distributed Cache system deployment and debugging
Working mechanism: By creating an area in memory to maintain a large hash table to speed up the page access speed, and the database is independent, is mainly used to cache the database data, stored in the memory of the data through the LRU algorithm to eliminate the memory; You can also eliminate the data stored in memory by deleting and setting the expiration time.
One memcache installation
1 memcached Server Installation
1.1 Memcached Official Website http://memcached.org/
Current stable version memcached-1.4.10.tar.gz
1.2 memcached relies on the Libevent library to check if the Libevent library is installed before memcached is installed, and if not, you need to install Libevent first
Libevent official website http://libevent.org/
Current stable version libevent-2.0.16-stable
Libevent is an event-triggered network library for Windows, Linux, BSD and other platforms, internal use of Select, Epoll, Kqueue and other system invoke management event mechanism
1.3 Installing Libevent and memcached
- # tar ZXVF libevent-2.0.16-stable.tar.gz
- # CD Libevent-2.0.16-stable
- #./configure --prefix=/usr/local/libevent
- # Make && make install
- # tar ZXVF memcached-1.4.10.tar.gz
- # CD memcached-1.4.10
- #./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
- # Make && make install
1.4 Simple test
- #/usr/local/memcached/bin/memcached-u root-d-M 200-l 127.0.0.1-p 11211-p/var/run/memcached.pid
- # PS aux | grep memcache | Grep-v ' grep '
- Root 8979 0.0 0.2 64732 1072? SSL 12:03 0:00/usr/local/memcached/bin/memcached-u root-d-M 200-l 127.0.0.1-p 11211-p/var/run/memcached.pid
- # Lsof-i: 11211
- COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
- memcached 8999 root 26u IPv4 12244 TCP one.t.com:11211 (LISTEN)
- memcached 8999 root 27u IPv4 12245 UDP one.t.com:11211
2 Memcache Client Installation
2.1 PHP Memcache Client
Http://pecl.php.net/package/memcache
Current stable version memcache-2.2.6.tgz
- # tar ZXVF memcache-2.2.6.tgz
- # CD memcache-2.2.6
- #/usr/local/php/bin/phpize
- #./configure--enable-memcache --with-php-config=/usr/local/php/bin/php-config
- # Make && make install
- Installing Shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
- # Vim/etc/php.ini
- Then modify the php.ini
- Extension_dir = "./"
- Revision changed to
- Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"
- and add a row
- extension=memcache.so
- #/etc/init.d/php-fpm Restart
#memcache. PHP Test Files
- <?
- $memcache = new Memcache;//Create a Memcache object
- $memcache->connect (' localhost ', 11211) or Die ("Could not Connect");//Connect memcached server
- $memcache->set (' key ', ' test ');//set a variable into memory, the name is the key value is test
- $get_value = $memcache->get (' key ');//Remove the value of key from memory
- echo $get _value;
- ?>
- Output test, indicating the success of the building!
#php调用memcache
2.2. NET Memcache Client
http://blog.csdn.net/xiaoyong322/article/details/6544027
Http://code.google.com/p/memcached/wiki/Clients
Two memcache installing shell scripts and installation packages
1 memcached Installation Script
2 memcached Service Script
3 Memcached installation Package
Three memcached command parameter description
- /usr/local/memcached/bin/memcached -d -u root -m 6144 -l 10.10.10.1 -p 11211 -c 100000 -p /var/run/memcached.pid
- -p The port of the listener,
- -l the IP address of the connection, default all IP addresses
- -d start start the memcached service
- -u run (valid only when running as root)
- -m Maximum memory usage, MB; default 64mb
- -p listening TCP port number; default 11211
- -c The maximum number of simultaneous connections, the default is 1024
- -p memcache pid file, need to use -d parameter
- -f block size growth factor, default is 1.25
- -n minimum allocation space, key+value+flags default is 48
- -h Display Help
Quad memcached Server Debug
Telnet to the Memcache server, such as Telnet 10.0.1.252 11211
Stats View basic information
Stats items can see a lot of items in the line
Stats cachedump 1 0 (3 is the number following the items, 0 means all are displayed)
Get Key (key is the string after item)
- #memcached server 1
- $ telnet 10.10.10.1 11211
- Trying 10.10.10.1 ...
- Connected to 10.10.10.1.
- Escape character is ' ^] '.
- Stats
- STAT PID 1750
- STAT Uptime 106479
- STAT Time 1325943604
- STAT version 1.4.10
- STAT libevent 2.0.16-stable
- STAT Pointer_size 64
- STAT Rusage_user 42.595524
- STAT Rusage_system 140.332666
- STAT Curr_connections 1531
- STAT total_connections 1057087
- STAT Connection_structures 1874
- STAT Reserved_fds 20
- STAT Cmd_get 33790491
- STAT Cmd_set 1557865
- STAT Cmd_flush 0
- STAT Cmd_touch 0
- STAT get_hits 30863261
- STAT get_misses 2927230
- STAT delete_misses 157
- STAT Delete_hits 186
- STAT incr_misses 0
- STAT Incr_hits 0
- STAT decr_misses 0
- STAT Decr_hits 0
- STAT cas_misses 0
- STAT Cas_hits 0
- STAT Cas_badval 0
- STAT Touch_hits 0
- STAT touch_misses 0
- STAT Auth_cmds 0
- STAT auth_errors 0
- STAT Bytes_read 8488077314
- STAT Bytes_written 173942242454
- STAT limit_maxbytes 6442450944
- STAT Accepting_conns 1
- STAT Listen_disabled_num 0
- STAT Threads 4
- STAT Conn_yields 9
- STAT Hash_power_level 19
- STAT hash_bytes 4194304
- STAT hash_is_expanding 0
- STAT expired_unfetched 31545
- STAT evicted_unfetched 0
- STAT bytes 1309377867
- STAT Curr_items 404976
- STAT Total_items 1557865
- STAT Evictions 0
- STAT reclaimed 47812
- #memcached server 2
- # telnet 10.0.1.252 11211
- Trying 10.0.1.252 ...
- Connected to 10.0.1.252 (10.0.1.252).
- Escape character is ' ^] '.
- Stats items
- STAT Items:1:number 1
- STAT Items:1:age 10975
- STAT items:1:evicted 0
- STAT Items:1:evicted_nonzero 0
- STAT Items:1:evicted_time 0
- STAT Items:1:outofmemory 0
- STAT Items:1:tailrepairs 0
- STAT items:1:reclaimed 0
- STAT items:1:expired_unfetched 0
- STAT items:1:evicted_unfetched 0
- END
- Stats Cachedump 1 0
- ITEM key [4 B; 1325927086 S]
- END
- Get key
- VALUE Key 0 4
- Test
- END
Extended
Memcache Agreement Chinese version
End
Memcached saves disk reads and writes, database connections, SQL parsing resources, and it uses memory to manage data, so it can provide better performance than a direct read database. In large systems, access to the same data is frequent, and memcached can significantly reduce database stress.
Memcached is also often used as a storage medium for data sharing between servers, such as in a Web cluster where the session can be saved in memcached and shared by multiple applications.
Memcached is to put data in memory, not persistent storage, and once the server restarts, the data will be lost.
Memcached distributed Cache system deployment and debugging