Sample code for PHP load memcache module

Source: Internet
Author: User

Memcache

Defined

Memcache is a set of distributed cache systems
Currently used by many Web sites to improve the speed of website access, especially for some large

Work flow

1. First check whether the data accessed by the client is memcache, and if there is a direct return
2. If not in the memcache, go to the database, while caching a copy to Memcache, greatly improve the read speed.

Applications and Features

1. Used to make Web pages or database caches
2. Can be used to do session sharing
3. Suitable for small but many data changes (such as Weibo fans + 1)
4. Store in memory, cannot persist data

Cache optimization Rules: 28 principles

20%: Hot data, frequently accessed data. Used as a cache, stored in memory
80%: Basic unchanged data, stored on solid-state drives

PHP Load memcache Module

Detecting the current PHP environment

Vim ~/.bash_profilepath= $PATH: $HOME/bin:/usr/local/lnmp/mysql/bin/:/usr/local/lnmp/php/bin

. ~/.bash_profile or do a 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    preparing the pre-compilation environment

./configure make && make install

Ensure that the PHP execution path is the path of the source package

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

Inspection

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

Rpm-qa |grep PHP guarantees no RPM packet interference

Empty

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

Accessing the memcached Database

Yum install telnet-y telnet localhost 11211
Set name 0 0 6westosSTOREDget namevalue name 0 6westosENDdelete namedeletedget name END

Writing a monitoring page

CD memcache-2.2.5

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

Vim memcache.php

Define (' Admin_password ', ' Westos ');      Admin Password28 $MEMCACHE _servers[] = "; Add more as an array29 $MEMCACHE _servers[] = ' mymemcache-server2:11211 '; Add more as an arra    y

Writing a test page

Vim test.php

<?php$memcache = new Memcache; $memcache->connect (' 127.0.0.1 ', 11211) or Die ("Could not Connect"); $version = $MEMCA Che->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, Ten) or Die ("Failed-to-save data at Theserver"); echo "Store data in the" cache (data would expire in seconds) \ n "; $get _result = $memcache->get (' key '); echo" Data from the cache:\n "; Var_dump ($get _result);? >

Start Nginx

Nginx

Inspection

In Browser access:

1.172.25.88.11/memcache.php Monitoring Cache Hit rate

2.172.25.88.11/test.php

Constantly refreshed, can be seen on the monitoring page, the cache hit ratio (Hits) is getting bigger

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.