The previous article introduced the installation of memcached and the simple commands. Here we introduce how PHP combines memcached.
The configuration is actually very simple just a few steps.
1. First install php memcache extension, I believe you will not be unfamiliar with the expansion module installation of PHP.
First, you can go to pecl.php.net official website to download the installation package, I downloaded here 2.2.5 version
# tar Zxfmemcache-2.2.5.tgz
# CD memcache-2.2.5
#/usr/local/php/bin/phpize
#./configure--with-php-config=/usr/local/php/bin/php-config
# makes && make installl after installation there will be a hint like this:
Installing Shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20130626/
Then modify the php.ini and change the Extension_dir = "./" revision to
Extension_dir = "/usr/local/php/extensions" and adds a row
extension= "Memcache.so"
Then create the directory mkdir /usr/local/php/extensions
move memcache.so under the no-debug-non-zts-20130626/directory to the /usr/local/php/extensions/directory
/usr/local/php/bin/php-m |grepmemcache can verify that the extension was added successfully.
2. PHP Script test
First, write a test php script
Vim test.php content is as follows:
<?php
$mem = new Memcache;
$mem->connect ("localhost", 11211);
Save data
$mem->set (' key1 ', ' This is first value ', 0, 60);
$val = $mem->get (' Key1 ');
echo "Get Key1 value:". $val. " <br> ";
Replace data
$mem->replace (' key1 ', ' This is replace value ', 0, 60);
$val = $mem->get (' Key1 ');
echo "Get Key1 value:". $val. "<br>";
Saving array data
$arr = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');
$mem->set (' Key2 ', $arr, 0, 60);
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "<br>";
Delete data
$mem->delete (' Key1 ');
$val = $mem->get (' Key1 ');
echo "Get Key1 value:". $val. "<br>";
Clear All data
$mem->flush ();
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "<br>";
Close connection
$mem->close ();
?>
Then execute the command:/usr/local/php/bin/php test.php
See if you can get the following:
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>
If you get this content the description is combined successfully .
This article is from the Linux OPS blog, so be sure to keep this source http://zhumy.blog.51cto.com/11647651/1828866
PHP combined memcached