Php sample code for loading the memcache module (Figure)

Source: Internet
Author: User
Tags php sample code
Memcache is a distributed high-speed cache system that is currently used by many websites to speed up website access, especially for some large websites that require frequent access to databases, the access speed has improved significantly [1]

Memcache

Definition

Memcache is a distributed high-speed cache system.
It is currently used by many websites to speed up website access, especially for some large

Workflow

1. check whether the data accessed by the client lies in memcache.
2. if it is not in memcache, check the database and cache one copy to memcache, which greatly improves the reading speed.

Applications and features

1. cache for web pages or databases
2. it can be used for session sharing.
3. suitable for small but large data changes (such as Weibo fans + 1)
4. data persistence is not allowed because the data is stored in the memory.

Cache optimization rules: 28 principles

20%: Hot data, frequently accessed data. Used as cache and stored in memory
80%: basically unchanged data stored in Solid State Disks

Php load memcache module

Check the current php environment

vim ~/.bash_profilePATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin/:/usr/local/lnmp/php/bin

.~ /. Bash_profile or soft link

[root@server11 bin]# ln -s /usr/local/lnmp/php/bin /usr/local/bin/

Compile

Tar zxf memcache-2.2.5.tgz cd memcache-2.2.5phpize preparation precompiled environment

./Configure make & make install

Ensure that the php execution path is the source code package path.

[Root @ server11 memcache-2.2.5] # which php/usr/local/lnmp/php/bin/phpcd/usr/local/lnmp/php/etc/vim php. ini remember. 863 extension = memcache. so/etc/init. d/php-fpm start

Inspection

[root@server11 etc]# php -m |grep memcache

Rpm-qa | grep php ensures no rpm package interference

Null

Background installation configuration

yum install memcached -y/etc/init.d/memcached start

Listening port netstat-antlpue

udp        0      0 0.0.0.0:11211               0.0.0.0:*                               498        10940      3706/memcached

Access the memcached database

yum install telnet -y telnet localhost 11211
set name 0 0 6westosSTOREDget nameVALUE name 0 6westosENDdelete nameDELETEDget name END

Write monitoring page

Cd memcache-2.2.5

Cp memcache. php/usr/local/nginx/html/

Vim memcache. php

23 define('ADMIN_PASSWORD','westos');      // Admin Password28 $MEMCACHE_SERVERS[] = ''; // add more as an array29 $MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an arra    y

Compile the test page

Vim test. php

 connect('127.0.0.1', 11211) or die ("Could not connect");$version = $memcache->getVersion();echo "Server's version: ".$version."\n";$tmp_object = new stdClass;$tmp_object->str_attr = 'test';$tmp_object->int_attr = 123;$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at theserver");echo "Store data in the cache (data will expire in 10 seconds)\n";$get_result = $memcache->get('key');echo "Data from the cache:\n";var_dump($get_result);?>

Start nginx

Nginx

Inspection

Access in a browser:

1. 172.25.88.11/memcache. php metric cache hit rate

2. 172.25.88.11/test. php

Refresh constantly. we can see on the monitoring page that the cache hit rate (Hits) is getting bigger and bigger.

The above is the details of the sample code for php to load the memcache module (figure). For more information, see other related articles in the first PHP community!

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.