The memcached of accelerating and caching technology

Source: Internet
Author: User
Tags install php php memcached zts

Memcached

The memcached is an open-source, high-performance, distributed memory object caching system that can be used in a variety of scenarios where caching is required, with the main purpose of reducing

Database access to speed up the Web application. 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 software developed by Brad Fitzpatric, a Danga Interactive company in LiveJournal. It has become an important factor in improving Web application extensibility in many services such as Mixi, Hatena, Facebook, Vox, and LiveJournal .

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 to install it beforehand, the project home page: http://libevnet.org/, readers can choose the version they need to download. This article uses the version 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, memcached sysv the startup script code as shown below, set it as a/etc/init.d/memcahced file:

!/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=" 1024x768 "cachesize=" "options=" "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  -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: $0 {start| Stop|restart|condrestart|status} "        RETVAL=1esacexit  $RETVAL

Configure memcached to become a system service using the following command:

#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

1) Install PHP memcached 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/

2) Edit the/usr/local/php/lib/php.ini and 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, 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 (' 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.xxx.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  @fallback  {                 proxy_pass http://172.16.0.1;         }    }


The memcached of accelerating and caching technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.