The memcached is an open-source, high-performance, distributed memory object caching system that can apply a variety of scenarios that require caching, primarily to speed up Web applications by reducing access to the database. It is a memory-based "key-value pair" store that stores direct data for database calls, API calls, or page reference results, such as strings, objects, and so on.
Memcached is a development tool that is neither a code accelerator nor a database middleware. The philosophy of design is mainly reflected in the following aspects:
1. Simple Key/value Storage: The server does not care about the meaning and structure of the data itself, as long as the data can be serialized. The stored items are composed of "key, expiration time, optional flag and data" four parts;
2. The implementation of the function is half dependent on the client, half based on the server side: the customer is responsible for sending the storage items to the server side, the data from the service side and unable to connect to the server when the corresponding action; The service side is responsible for receiving, storing data, and responsible for the data item timeout expires;
3. Each server ignores each other: does not synchronize the data between the servers;
4. O (1) Efficiency of implementation
5. Cleanup of Extended Data: By default, Memcached is an LRU cache, and it cleans up extended data as long as it is pre-booked, but in fact, memcached does not delete any cached data but is no longer visible to the customer after it expires; Memcached also does not really clean up the cache by age, but only when the get command arrives, checking its duration;
Memcached provides a handful of commands to accomplish server-side interactions, which are based on memcached protocol implementations.
Storage class Commands: Set, add, replace, append, prepend
Get Data Class command: Get, delete, INCR/DECR
Statistics class commands: Stats, stats items, stats slabs, stats sizes
Cleanup command: Flush_all
First, install the Libevent
Memcached relies on the libevent API, so it should be installed beforehand; Libevent Project home: http://libevent.org/
Libevent-2.0.16-stable.tar.gz. Installation process:
# Tar XF libevent-2.0.20-stable.tar.gz
# CD libevent-2.0.20
#./configure--prefix=/usr/local/libevent
# Make && make install
# echo "/usr/local/libevent/lib" >/etc/ld.so.conf.d/libevent.conf
# Ldconfig
Second, installation configuration memcached
1, installation memcached
# Tar XF memcached-1.4.15.tar.gz
# CD memcached-1.4.15
#./configure--prefix=/usr/local/memcached--with-libevent=/usr/local/libevent
# Make && make install
2. Establish/etc/init.d/memcached startup file for memcached:
# vi/etc/init.d/memcached
#!/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 Variables
Port= "11211"
User= "Nobody"
maxconn= "1024"
Cachesize= "64"
Options= ""
Retval=0
Prog= "/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-o "$OPTIONS"
Retval=$?
Echo
[$RETVAL-eq 0] && Touch $lockfile
Return $RETVAL
}
Stop () {
Echo-n $ "Shutting down $desc (memcached):"
Killproc $prog
Retval=$?
Echo
[$RETVAL-eq 0] && rm-f $lockfile
Return $RETVAL
}
Restart () {
Stop
Start
}
Reload () {
Echo-n $ "Reloading $desc ($prog):"
Killproc $prog-hup
Retval=$?
Echo
Return $RETVAL
}
Case "$" in
Start
Start
;;
Stop
Stop
;;
Restart
Restart
;;
Condrestart)
[-e $lockfile] && restart
Retval=$?
;;
Reload
Reload
;;
Status
Status $prog
Retval=$?
;;
*)
echo $ "Usage: $ {Start|stop|restart|condrestart|status}"
Retval=1
Esac
Exit $RETVAL
To configure memcached Add system services:
# chmod +x/etc/init.d/memcached
# chkconfig--add memcached
# service memcached Start
3. Use the Telnet command to test the use of memcached
Memcached provides a set of basic commands for invoking its services or viewing server status based on the command line.
# telnet 127.0.0.1 11211
Add Command:
Add KeyName Flag Timeout datasize
Such as:
Add MyKey 0 10 12
Hello world!
Get command:
Get KeyName
such as: Get MyKey
VALUE MyKey 0 12
Hello world!
END
4, memcached of common options description
-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 for cache data in MB, default of 64MB;
-c <num>: Maximum number of concurrent connections supported, default is 1024;
-P <num>: Specifies the listening TCP port, which defaults to 11211;
-U <num>: Specifies the UDP port to listen on, the default is 11211,0 to close the UDP port;
-T <threads>: The maximum number of threads used to process inbound requests, only the support line friend valid at memcached compile time;
-F <num>: Set slab allocator defines the growth factor used when pre-allocating blocks of fixed memory space;
-M: Returns an error message when the memory space is not available, rather than using the space according to the LRU algorithm;
-N: Specifies the minimum slab chunk size; the unit is byte;
-S: Enable SASL for user authentication;
Third, install the memcache PHP extension
① installing PHP's memcache extension
# Tar XF memcache-2.2.5.tgz
# CD memcache-2.2.5
/usr/local/php/bin/phpize
#./configure--with-php-config=/usr/local/php/bin/php-config--enable-memcache
# Make && make install
There will be a hint similar to the following when the installation is complete:
Installing Shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
② edit/usr/local/php/lib/php.ini, add the following line to load the memcache extension in the location associated with the dynamic module:
Extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
Then test the memcached function, build the test page test.php in the Site directory and add the following:
<?php
$mem = new Memcache;
$mem->connect ("127.0.0.1", 11211) or Die ("Could not Connect");
$version = $mem->getversion ();
echo "Server ' s version:". $version. " <br/>\n ";
$mem->set (' TestKey ', ' Hello World ', 0, +) or Die ("Failed-to-save data at the memcached server");
echo "Store data in the cache (data would expire in seconds) <br/>\n";
$get _result = $mem->get (' TestKey ');
echo "$get _result is from memcached server.";
?>
If there is an output of "Hello world was from memcached." And so on, it indicates that Memcache has been able to work properly.
Iv. client-side tools using libmemcached:
The traditional way to access memcached is to use the Perl-based cache::memcached module, which works well in most Perl code, but also has a well-known performance problem. Libmemcached is an open-source C/C + + code-based memcached library file that has been developed for use in the language, and it also provides several memcached management tools that can be used remotely, such as Memcat, Memping,memstat, Memslap and so on.
1) Compile and install libmemcached
# Tar XF libmemcached-1.0.2.tar.gz
# CD libmemcached-1.0.2
#./configure
# Make && make install
# Ldconfig
2) Client Tools
# Memcat--servers=127.0.0.1:11211 MyKey
# memping
# Memslap
# MemStat
Five, Nginx integration memcached:
server {
Listen 80;
server_name www.hk.com;
#charset Koi8-r;
#access_log Logs/host.access.log Main;
Location/{
Set $memcached _key $uri;
Memcached_pass 127.0.0.1:11211;
Default_type text/html;
Error_page 404 @fallback;
}
Location {
Proxy_pass http://172.16.0.1;
}
}
This article is from the "Logs" blog, make sure to keep this source http://51log.blog.51cto.com/6076767/1899572
Memcached Application Scenarios