Memcache
Memcache the role of the online data are very good, said the simple point is to reduce the pressure to read the database, the principle is very simple:
The requested data will go to memcache first, if not go to the database to fetch, by the way to Memcache bring a copy.
The data in Memcache is updated each time the data is updated, and if not, the database is updated and Memcache is updated.
It is therefore important to note that this data is susceptible to loss of sexual storage.
Mode and port
Memcache is a structure based on C/s:
Service side: Using memcached software
Client: Using the Memcache plugin (this plugin is in combination with the backend language such as PHP python java)
Service port: 11211 (can be changed)
Software Checklist:
Libevent Dependent Library http://www.libevent.org/
Memcache plug-in http://pecl.php.net/package/memcache/
memcached Service http://www.memcached.org/
Lamp environment yum-y Install httpd PHP php-mysql mysql-server
Operating system CentOS-6.5 (x86_64)
1. Upload the relevant package, install the lamp environment
Yum-y Install httpd php php-mysqlmysql-server /etc/init.d/httpd start Echo "<?php phpinfo ()?>" >/var/www/html/index.php
Then use the browser to view the PHP information, in the information is not found Memcache
2. Installing the Libevent Plugin
Tar XF Libevent-2.0.22-stable.tar. GZ cd libevent-2.0.22-stable ./configure--prefix=/usr/local/libevent && Make & & Make Install
3. Installing the memcached server
Tar XF Memcached-1.4.36.tar. GZ cd memcached-1.4.36./configure--prefix=/usr/local/memcached--with-libevent=/usr/local/libevent/ && make Install
After installation, the memcached will be generated in the/usr/local/memcached/bin/directory.
4. Configure Environment variables
cd/etc/profile.d/Vim Mem.SH export PATH= "/usr/local/memcached/bin:$PATH"#Write profile file Boot auto Importmemcached-M 32-p 11211-d-C 8192-u Root#m split memory size p Port D Mixed mode C maximum number of connectionsnetstat-anptu | grep memcached#to see if it starts, run a multi-instance change port Free-M#you can see that the memory is getting smaller because it's being allocated.PS-aux | grep memcached#See how the process PID isKill-9 1234#Close memcached ServicePkill memcached#Ibid .
5.memcached use
Yum-y Install NC telnet
1) Connect memcache using NC command
printf "Set first 0 5\r\nmmmmm\r\n" | NC 127.0.0.1 11211 # Save data (fields are key, flag, validity, length, value)printf "Get first\r\n" | NC 127.0.0.1 11211 #
2) Connect memcache using telnet command
Telnet 127.0.0.1 11211 # then you can use the relevant memcached command.
6. The following is a memcached related operation command
Add Key1 0 3 #添加数据30为效期 (if write 0 means never expires) 3 is size
Set Key1 0 3 #更新数据, does not exist automatically created
Replace Key1 0 3 #更新数据, there will be an error
Delete Key1 #删除数据
Get Key1 #获取数据
Gets key 1 #获取更多信息
Stats setting #查看配置信息
Stats Slabs #查看slab
Stats Items #查看item
Stats size #查看大小
7. Installing the Memcache client PHP plugin
Install the phpize command to add a new module to PHP
If you don't know what package you can use Yum provides */phpize
Yum-y Install php-devel tar xf memcache-2.2.7. tgz cd memcache-2.2.7 phpize # play module, generate configure and other files which php-config # View the php-config path location ./configure--enable-memcache --with-php-config=/usr/bin/php- && make Install
The module will be installed after the installation number/usr/lib64/php/modules/memcache.so
cd/etc/php.d/mysql. ini memcache.ini #Vim is edited to set the value of extension to memcache.so
After restarting the service, you can see that PHP has supported the Memcache module.
8. The following can be combined with the PHP site test database related
Tar XF memcache_page.tar.gz-c/var/www/html/!$
The test page has mysql_connect.php edit
So you need to set up the MySQL user first.
/etc/init.d/mysqld
Or you're in the database.
Grant all on * * to ' root ' @ ' 127.0.0.1 ' identified by ' 123456 ' flush privileges
Then the browser accesses mysql_connect.php
Docking success
Here you can read read.php and write.php to learn about Memcache's reading and writing principles
read.php
<?PHP$memcachehost= ' 192.168.1.113 ';$memcacheport= 11211;$memcachelife= 60;#memcache default Validity period$memcache=NewMemcache;$memcache->connect ($memcachehost,$memcacheport) or die("Could not Connect");#Connecting Memcache servers$num=$_post["Num"];$db=db1;$TB=T1;$query= "SELECT * from"$TBwhere id=$num";#MySQL Query statement#$key =md5 ($query);$key=MD5($num);#By encrypting the parameters, you can see that the value stored by the Memcache is encrypted.if(!$memcache->get ($key))#Try to get the value from Memcache first, if not go to the database fetch, by the way to memcache a copy{ $conn=mysql_connect("127.0.0.1", "root", "123456"); mysql_select_db($db); $result=mysql_query($query);#echo "MySQL $num"; while($row=Mysql_fetch_assoc($result)) { $arr[]=$row; } $f= ' MySQL '; $memcache->add ($key,Serialize($arr), 0,30); $data=$arr ;}Else{ $f= ' Memcache '; $data _mem=$memcache->get ($key); $data=unserialize($data _mem);}Echo"$f $num";Echo"Key is$key";Echo"<br>";?>
write.php
<?PHP$memcachehost= ' 192.168.1.113 ';$memcacheport= 11211;$memcachelife= 60;$memcache=NewMemcache;$memcache->connect ($memcachehost,$memcacheport) or die("Could Not Connect");$num=$_post["Num"];$db=db1;$TB=T1;$query= "INSERT INTO$TBValues$num)";#$key =md5 ($query);$key=MD5($num);if(!$memcache->get ($key))//try updating Memcache First, and then update the database if it doesn't exist, and update the store to Memcachce{ $conn=mysql_connect("127.0.0.1", "root", "123456"); mysql_select_db($db); $result=mysql_query($query); while($row=Mysql_fetch_assoc($result)) { $arr[]=$row; } $f= ' MySQL '; $memcache->add ($key,Serialize($arr), 0,30);//after the MySQL insert is successful, insert memcached $data=$arr ;#}#else{ $f 1= ' Memcache '; $data _mem=$memcache->get ($key); $data=unserialize($data _mem);}Echo"$f $f 1 $num";Echo"<br>";?>
For simple usage of PHP memcache see http://www.cnblogs.com/demonxian3/p/6868361.html
In the above two PHP you can see the DB1 and table T1 called the database, so you need to create a
SEQ 1 999 >/tmp/sum # create 1-999 test data
Connect to database Import data
Create Database db1; Create T1 (id int) engine=innodb; ' /tmp/sum ' into table T1; # Import test Data
Popular Science: Use history to view historical commands, enter the!+ number can execute the number of the command
! 111
Use the browser to access the test page
Test read data, from the database to query the value of ID 5
Go back and pick it up again.
Test Write Data
Finally, we recommend a very useful memcache management tool: Memadmin PHP written
Memcache Service Building