Memcached is an open-source, high-performance, distributed memory object cache system that can be used in various cache scenarios. Its main purpose is to accelerate Web applications by reducing database access. It is a memory-based "key-Value Pair" storage that stores direct data from database calls, API calls, or page reference results, such as strings and objects.
You need to describe the difference between him and memcache. mencache is a module of the PHP server, used to communicate with memcached.
Data storage method: slab allocation
Divide the allocated memory into chunk blocks of various sizes and divide the chunk blocks into groups)
Data expiration method: lazy expiration + LRU
Lazy Expiration: memcached does not monitor whether the record has expired. Instead, it checks the timestamp of the record during get to check whether the record has expired. This technology is called lazy (inert) expiration. Therefore, memcached does not consume CPU time on expired monitoring.
LRU: memcached preferentially uses the space of records that have timed out, but even so, there will be insufficient space to append new records. In this case, you need to use the name least recently used (LRU) to allocate space. As the name suggests, this is a mechanism to delete records that are "least recently used. Therefore, when memcached has insufficient memory space (when the new space cannot be obtained from the slab class), it will be searched from the records that have not been used recently, and allocate the space to the new record. 1. Install libevent
1. memcached depends on libevent API, project home page: http://libevent.org/
1: # tar xf libevent-2.0.21-stable.tar.gz
2: # cd libevent-2.0.21
3: # ./configure --prefix=/usr/local/libevent
4: # make && make install
5:  
6: # echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf
7: # ldconfig
Ii. install and configure memcached
1: # tar xf memcached-1.4.15.tar.gz
2: # cd memcached-1.4.15
3: # ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
4: # make && make install
Sysv script
#!/bin/bash## Init file for memcached## chkconfig: - 86 14# description: Distributed memory caching daemon## processname: memcached# config: /etc/sysconfig/memcached. /etc/rc.d/init.d/functions## Default variablesPORT="11211"USER="nobody"MAXCONN="1024"CACHESIZE="64"
RETVAL=0prog="/usr/local/memcached/bin/memcached"desc="Distributed memory caching"lockfile="/var/lock/subsys/memcached"start() { echo -n $"Starting $desc (memcached): " daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE
RETVAL=$? [ $RETVAL -eq 0 ] && success && touch $lockfile || failure echo return $RETVAL}stop() { echo -n $"Shutting down $desc (memcached): " killproc $prog RETVAL=$? [ $RETVAL -eq 0 ] && success && rm -f $lockfile || failure echo return $RETVAL}restart() { stop start}reload() { echo -n $"Reloading $desc ($prog): " killproc $prog -HUP RETVAL=$? [ $RETVAL -eq 0 ] && success || failure echo return $RETVAL}case "$1" in start) start ;; stop) stop ;; restart) restart ;; condrestart) [ -e $lockfile ] && restart RETVAL=$? ;; reload) reload ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|restart|condrestart|status}" RETVAL=1esacexit $RETVAL
Grant execution permission
1: # chmod +x /etc/init.d/memcached
2: # chkconfig --add memcached
3: # service memcached start
Iii. Description of common memcached options
-L <ip_addr>: Specifies the address of the Process listener;
-D: Run in service mode;
-U <username>: runs the memcached process as a specified user;
-M <num>: maximum memory space used to cache data. The unit is mb. The default value is 64 MB;
-C <num>: Maximum number of concurrent connections supported. The default value is 1024;
-P <num>: Specifies the TCP port of the listener. The default value is 11211;
-U <num>: Specifies the UDP port of the listener. The default value is listen 11,0 indicates that the UDP port is disabled;
-T <threads>: Maximum number of threads used to process inbound requests. This parameter is valid only when the supported threads are enabled during memcached compilation. -- enable-threads
-F <num>: Set Slab allocator to define the growth factor used when pre-allocated memory space is fixed;
-M: The LRU algorithm is forbidden;
-N: specify the minimum slab chunk size, in bytes;
-S: enables SASL for user authentication;
Iv. Test
Memcached
Storage commands: Set, add, replace, append, prepend
Get Data commands: Get, delete, incr/decr
Statistics commands: stats, stats items, stats slabs, stats sizes
Cleanup command: flush_all
Access memcached: Telnet host name port number
View the total status: stats
View A status: stats curr_connections
& #160;
& #160;
PS: I have seen some introductions saying that "the maximum memory usage of a single memcached process is 2 GB. If you want to use more memory, you can use another port to enable it." I have not tested it, write it as a memo.
The above content references the memcached comprehensive analysis compiled by mixi.jp. If you are interested in memcached, please refer to the following link for a clear explanation.
Http://blog.charlee.li/memcached-pdf/
& #160;
5. Install the memcache module for PHP to communicate with memcached
1: # tar xf memcache-2.2.5.tgz
2: # cd memcache-2.2.5
3: /usr/local/php/bin/phpize
4: # ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
5: # make && make install
After compilation, the/path/memcache. So file is output, and then the extension =/path/memcache. So line is added to/etc/PHP. ini.
6. Test whether PHP and memcached work is successful
<?php$mem = new Memcache;$mem->connect("127.0.0.1", 11211) or die("Could not connect");#vim memcached.php$version = $mem->getVersion();echo "Server‘s version: ".$version."<br/>\n";$mem->set(‘hellokey‘, ‘Hello World‘, 0, 600) or die("Failed to save data at the memcached server");echo "Store data in the cache (data will expire in 600 seconds)<br/>\n";$get_result = $mem->get(‘hellokey‘);echo "$get_result is from memcached server."; ?>
If the output "Hello world is from memcached." is successful, you don't need to say it!
7. libmemcached tool:
Libmemcached is an open-source C/C ++ code developed based on C language to access the memcached library file, allowing memcached and C/C ++ to work together. It also provides several memcached management tools that can be used remotely.
Installation:
1: # tar xf libmemcached-1.0.2.tar.gz
2: # cd libmemcached-1.0.2
3: # ./configure
4: # make && make install
5: # ldconfig
Management tools:
1: # memcat --servers=127.0.0.1:11211 mykey
2: # memping
3: # memslap
4: # memstat
This article from the "quiet natural access" blog, please be sure to keep this source http://bluezombie.blog.51cto.com/1444792/1441071
How to install memcached