memcached installation Configuration

Source: Internet
Author: User
Tags memcached zts aliyun

I. Introduction of Memcached

    • Overseas Community website Live Journal team developed to improve dynamic Web site performance by caching database query results and reducing database access times. Official site http://memcached.org

    • Simple protocol based on C/s architecture

    • Libevent-based event handling

    • Autonomic memory storage processing (Slab allowcation)

    • Data expiration methods: Lazy Expiration and LRU


Second, memcached flowchart

Reduce the number of reads to MySQL

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m01/84/91/wkiom1evy0xs10weaagocw6myr0001.png-wh_500x0-wm_3-wmp_4-s_3880255528.png "title = "2.png" alt= "Wkiom1evy0xs10weaagocw6myr0001.png-wh_50"/>


Slab allocation

    • The principle of Slab allocation-dividing allocated memory into blocks of various sizes (chunk) and dividing the same size blocks into groups (chunk collections), each chunk collection is called Slab.

    • The memory allocation for memcached is in page, and the page default value is 1M, which can be specified at startup by the-l parameter.

    • The slab is made up of multiple page, and the page is cut into multiple chunk by the specified size

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s5.51cto.com/wyfs02/m00/84/91/wkiol1evy1xwpdcraajrag0pkug087.png-wh_500x0-wm_3-wmp_4-s_2765678042.png "title = "3.png" alt= "Wkiol1evy1xwpdcraajrag0pkug087.png-wh_50"/>


Growth factor

    • Memcached can specify the growth factor factor at startup by using the-f option. This value controls the difference in chunk size. The default value is 1.25

    • Viewing the different slab states of a specified memcached instance by Memcached-tool, you can see that the difference between the size of a ltem (chunk size) is 1.25

    • Command: #memcached-tool 127.0.0.1:11211 Display


memcached How data expires

    • Lazy Expiration

Memcached internally does not monitor whether the record is out of date, but instead looks at the timestamp of the record at get and checks whether the record is out of date. This technique is called lazy (lazy) expiration. As a result, memcached does not consume CPU time on outdated monitoring.

    • Lru

Memcached takes precedence over the space of a record that has timed out, but even so, there is a situation where there is a shortage of append new records, which is the time to allocate space using the least recently used (LRU) mechanism. As the name implies, it is the mechanism to delete records that have been "seldom used recently". Therefore, when there is insufficient memory space (when a new space cannot be obtained from the Slab Class), it is searched from records that have not been used recently, and its space is allocated to the new record. From the point of view of cache usage, the model is ideal.


Third, memcached installation

1. Install the extension source

Wget-o/etc/yum.repos.d/centos-base.repo Http://mirrors.aliyun.com/repo/Centos-6.repowget-P/etc/yum.repos.d/http ://mirrors.aliyun.com/repo/epel-6.repo Yum Clean Allyum makecache

#安装阿里yum源及扩展源


2. Install the memcached package and the dependent package

Yum install-y libevent memcached libmemcached NC


3. Start the memcached service

/etc/init.d/memcached startps aux | grep memcached498         1029  0.0  0.0 330844   988 ?         Ssl  10:43   0:00 memcached -d -p  11211 -u memcached -m 64 -c 1024 -p /var/run/memcached/ Memcached.pidnetstat -tnlup| grep memcachedtcp        0       0 0.0.0.0:11211                0.0.0.0:*                    listen      1029/memcached       tcp        0       0 :::11211                     :::*                          LISTEN      1029/memcached       udp        0       0 0.0.0.0:11211                0.0.0.0:*                                1029/ memcached      udp        0       0 :::11211                     :::* 


4.memcached Related Configurations

Vim/etc/sysconfig/memcached

Port= "11211" #端口

User= "memcached" #用户

maxconn= "1024"

Cachesize= "#内存"

Options= ""


5.memcached Startup Parameters

-D Start a daemon

-m specifies the amount of memory allocated to memcached, in megabytes

-u specifies memcached to run the user, if current is root, this parameter needs to be used

-l Specifies the server IP address to listen on

-p Specifies the port on which memcached listens, default is 11211

-c Specifies the maximum number of concurrent connections to run, default is 1024

-P setting memcached PID file


6. View Memcached Running Status

Memcached-tool 127.0.0.1:11211 Stats

Echo Stats | NC 127.0.0.1 11211

MemStat--servers=127.0.0.1:11211


7.memcached Basic Operation

Telnet 127.0.0.1 11211 #连接memcached


Set Key1 0 300 2

12

STORED

Get Key1

VALUE Key1 0 2

12

END


Memcached-tool 127.0.0.1:11211 Display #查看chunk


Set: Indicates that the data is stored in accordance with the corresponding <key>, there is no time to increase, some overwrite

Add: Indicates that the data is added according to the appropriate <key>, but the operation fails if the <key> already exists

Replace: Indicates that the data is replaced by the appropriate <key>, but the operation fails if the <key> does not exist

<flags> is a 16-bit unsigned integer

<exptime> expiry time


8.PHP Connection memcached

#找一台LNMP或LAMP, the Pecl installer that comes with PHP

/usr/local/php/bin/pecl Install Memcache


can also be installed from the source code

wget Http://www.lishiming.net/data/attachment/forum/memcache-2.2.3.tgztar zxf memcache-2.2.3.tgzcd memcache-2.2.3/ Usr/local/php/bin/phpize. /configure--with-php-config=/usr/local/php/bin/php-configmake && make install


After installation, there will be a hint like this:

Installing '/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/memcache.so '

remember this, and then Vim/usr/local/php/etc/php.ini . , put

Extension_dir = "./"

Revision changed to

Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/"

and add a row

extension= "Memcache.so"


Inspection

#/usr/local/php/bin/php-m

Test PHP Scripts

wget www.apelearn.com/study_v2/.memcache.txt


mv. Memcache.txt test.php

/usr/local/php/bin/php test.php #不要忘记安装memcached


Get Key1 Value:this is first value<br>get key1 value:this is replace Value<br>get key2 Value:array

(

[0] = AAA

[1] = BBB

[2] = = CCC

[3] = DDD

)

<br>get key1 Value: <br>get key2 value: <br>

Ok


9.memcached Implementing session Sharing

Vim/usr/local/php/etc/php.ini

#搜索The session keyword, add the following two lines

Session.save_handler = "Memcache"

Session.save_path = "tcp://127.0.0.1:11211"


or lamp.

Add the corresponding virtual hosts in the httpd.conf:

Php_value Session.save_handler "Memcache"

Php_value Session.save_path "tcp://127.0.0.1:11211"


or LNMP.

Php-fpm.conf the corresponding pool is added:

Php_value[session.save_handler] = Memcache

Php_value[session.save_path] = "tcp://127.0.0.1:11211"



wget http://study.lishiming.net/.mem_se.txt

mv. mem_se.txt/usr/local/nginx/html/session.php

Curl localhost/session.php



This article is from the "Fuqin Wine" blog, please make sure to keep this source http://szk5043.blog.51cto.com/8456440/1829522

memcached installation Configuration

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.