Download the client
System requirements and environment configuration
Note: If you have a PHP Memcache environment, pay attention to the prompts during the tutorial to avoid overwriting the production environment, which may render the business unavailable. We recommend that you back up the data before upgrading or compiling the environment.
Windows series
If the environment cannot be established successfully using the standard PHP Memcached extensions, you can consider changing to manual packet splicing to access ApsaraDB for Memcache. For connection methods, you can see the following link. The example code is simple and its difference with the PHP Memcached lies in that it supports only mainstream interfaces and you must supplement some specific interfaces on your own. For installation and usage methods, see here.
CentOS and Alibaba Cloud Linux 6 series
Note: Memcached 2.2.0 extensions must use Libmemcached 1.0.x libraries, and libraries lower than version 1.0 won’t be compiled successfully. GCC must be of version 4.2 or later for compiling Libmemcached.
Check whether the gcc-c++ or other components have been installed (use the
gcc –v
command to see whether the version is 4.2 or later). If not, run the commandyum install gcc+ gcc-c++
.Run the command
rpm –qa | grep php
to check whether the PHP environment is ready in the system. If no, run the commandyum install php-devel,php-common,php-cli
to install PHP with source code compiling.We recommend that you use PHP 5.3 or later. Part of PHP 5.2 versions may contain the zend_parse_parameters_none function in the source code, and the function may have errors. If you must use the function, see the official PHP documentation. For source code compilation, follow the official PHP compiling and upgrading methods.
Check whether the SASL-related environmental packets have been installed. If no, run the command
yum install cyrus-sasl-plain cyrus-sasl cyrus-sasl-devel cyrus-sasl-lib
to install the SASL-related environments.Check whether the Libmemcached source packages have been installed. If no, run the following command to install Libmemcached source package (Recommended version: Libmemcached-1.0.18).
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure --prefix=/usr/local/libmemcached --enable-sasl
make
make install
cd ..
Run the command
yum install zlib-devel
to install the Memcached source packages (Recommended version: Memcached-2.2.0).Note:
Check whether there is a zlib-devel package to be executed first before installing the Memcached.
Check whether the Memcached client package has been installed first (including the source package). If yes, you must re-compile it to add the -enable-memcached-sasl extension.
wget http://pecl.php.net/get/memcached-2.2.0.tgz
tar zxvf memcached-2.2.0.tgz
cd memcached-2.2.0
Phpize (If there are two sets of PHP environments in the system, you must call the command on the absolute path /usr/bin/phpize. The path is the PHP environment path for using Alibaba Cloud ApsaraDB for Memcache).
./configure --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-sasl(Pay attention to this parameter)
make
make install
Modify the php.ini file (Locate this file. If there are two sets of PHP environments in the system, you must find the PHP environment path for using Alibaba Cloud ApsaraDB for Memcache and modify it accordingly) and add
extension=memcached.so memcached.use_sasl = 1
.Use the test code at the bottom of this page to test whether the environment has been deployed successfully. If not, modify the corresponding address, port, user name and password in the code.
CentOS and Alibaba Cloud Linux 5 series (64-bit)
Check whether the gcc-c++ or other components have been installed. If not, run the command
yum install gcc+ gcc-c++
.Run the command
rpm –qa | grep php
to check whether the PHP environment is ready in the system. If no, run the commandyum install php53 php53-devel
to install the PHP with source code compiling. If the PHP environment is already prepared, no installation is required. We recommend that you use PHP 5.3 or later.Part of PHP 5.2 versions may contain the zend_parse_parameters_none function in the source code, and the function may have errors. If you must use the function, see the official PHP documentation.
Run the command
yum install cyrus-sasl-plain cyrus-sasl cyrus-sasl-devel cyrus-sasl-lib
to install the SASL-related environments.Check whether the Libmemcached (including source packages) has been installed. If no, run the following command to install Libmemcached (Recommended version: Libmemcached 1.0.2).
wget http://launchpad.net/libmemcached/1.0/1.0.2/+download/libmemcached-1.0.2.tar.gz
tar -zxvf libmemcached-1.0.2.tar.gz
cd libmemcached-1.0.2
./configure --prefix=/usr/local/libmemcached --enable-sasl
make
make install
cd ..
Run the command
yum install zlib-devel
to install the Memcached source package (Recommended version: Memcached 2.0).Note:
Check whether there is a zlib-devel package to be executed first before installing the Memcached.
Check whether the Memcached client package has been installed first (including the source package). If yes, you must re-compile it to add the -enable-memcached-sasl extension.
wget http://pecl.php.net/get/memcached-2.0.0.tgz tar -zxvf memcached-2.0.0.tgz
cd memcached-2.0.0 phpize (If there are two sets of PHP environments in the system, you must call the command on the absolute path /usr/bin/phpize. The path is the PHP environment path for using Alibaba Cloud ApsaraDB for Memcache. Run phpize in the memcached source code directory).
./configure --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-sasl(Pay attention to this parameter)
make
make install
Modify the php.ini file (Locate the file. The yum is usually installed in /etc/php.ini. If there are two sets of PHP environments in the system, you must find the PHP environment path for using Alibaba Cloud ApsaraDB for Memcache and modify it accordingly) and add
extension=memcached.so memcached.use_sasl = 1
.Run the command
php –m |grep ,memcached
. If the displayed result includes memcache, it indicates that memcache is supported in the environment.Use the test code at the bottom of this page to test whether the environment has been deployed successfully. If not, modify the corresponding address, port, user name and password in the code.
Ubuntu Debian series
Change the Ubuntu source.
Solution 1: Run the command
vim /etc/apt/source.list
and add the following content at the beginning.deb http://mirrors.aliyun.com/ubuntu/ precise main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ precise-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ precise-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ precise-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ precise main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ precise-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ precise-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ precise-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ precise-backports main restricted universe multiverse
apt-get update //Update the list
Solution 2: Download the update_source package at
wget http://oss.aliyuncs.com/aliyunecs/update_source.zip
, extract the package and grant it the execution permissionchmod 777
file name, and run the script to perform the automatic source change operation.Configure GCC, G++ with
ape-get
.First, you must run the command ‘dpkg –s installation package name’, such as ‘dpkg –s gcc’ to check whether gcc-c++ or other components have been installed. If no, run the command
apt-get build-dep gcc apt-get install build-essential
.Install PHP 5 and PHP5-dev.
First, you must run the command ‘dpkg –s installation package name’, such as ‘dpkg –s php’ to check whether php or other components have been installed. If no, run the command
apt-get install php5 php5-dev
(and php5-cli and php5-common will be installed at the same time).Install and configure SASL support.
First, you must run the command ‘dpkg –s installation package name’, such as ‘dpkg –s libsasl2’ to check whether libsasl2 cloog-ppl or other components have been installed. If no, run the following command.
apt-get install libsasl2-dev cloog-ppl
cd /usr/local/src
Run the following command to install Libmemcache of a specific version.
Note: Check whether these packages have been installed first (including the source package). If yes, no installation is required.
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar -zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure --prefix=/usr/local/libmemcached
make
make install
cd ..
Run the following command to install a specific version of Memcached.
Note: Check whether the Memcached client package has been installed first (including the source package). If yes, no installation is required, but you must recompile it to add the -enable-memcached-sasl extension.
wget
http://pecl.php.net/get/memcached-2.2.0.tgz tar zxvf memcached-2.2.0.tgz
cd memcached-2.2.0 phpize5
./configure --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-sasl
make
make install
Configure PHP to support memcached and then test the configuration.
echo "extension=memcached.so" >>/etc/php5/conf.d/pdo.ini echo "memcached.use_sasl = 1" >>/etc/php5/conf.d/pdo.ini
php -m |grep mem memcached
If this component is displayed, it indicates the component has been installed and configuration is completed.
PHP code example
Example 1: Basic connections to Alibaba Cloud ApsaraDB for Memcache and set/get operations
<?php
$connect = new Memcached; //Declare a new Memcached connection
$connect->setOption(Memcached::OPT_COMPRESSION, false); //Disable the compression function
$connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true); //Use the binary protocol
$connect->setOption(Memcached::OPT_TCP_NODELAY, true); //Important: The php memcached has a bug that when the get value does not exist, there will be a fixed 40 ms of latency. Enabling this parameter can avoid this bug.
$connect->addServer('aaaaaaaaaa.m.yyyyyyyyyyy.ocs.aliyuncs.com', 11211); //Add the ApsaraDB for Memcache instance address and port number
$connect->setSaslAuthData('aaaaaaaaaa', 'password'); //Set the ApsaraDB for Memcache account and password for authentication. If the password-free function has been enabled, this step can be skipped. For the new version of ApsaraDB for Memcache, the username is the instance ID.
$connect->set("hello", "world");
echo 'hello: ',$connect->get("hello");
$connect->quit();
?>
Example 2: Cache an array in Alibaba Cloud ApsaraDB for Memcache
<?php
$connect= new Memcached; //Declare a new Memcached connection
$connect->setOption(Memcached::OPT_COMPRESSION, false); //Disable the compression function
$connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true);//Use the binary protocol
$connect->setOption(Memcached::OPT_TCP_NODELAY, true); //Important: The php memcached has a bug that when the get value does not exist, there will be a fixed 40 ms of latency. Enabling this parameter can avoid this bug.
$connect->addServer('xxxxxxxx.m.yyyyyyyy.ocs.aliyuncs.com', 11211);//Add the ApsaraDB for Memcache instance address and port number
$connect->setSaslAuthData('xxxxxxxx', 'bbbbbbbb');//Set the ApsaraDB for Memcache account and password for authentication. If the password-free function has been enabled, this step can be skipped.
$user = array(
"name" => "ocs",
"age" => 1,
"sex" => "male"
); //Declare an array.
$expire = 60; //Set an expiration time.
test($connect->set('your_name',$user,$expire), true, 'Set cache failed');
if($connect->get('your_name')){
$result =$connect->get('your_name');
}else{
echo "Return code:", $connect->getResultCode();
echo "Retucn Message:", $connect->getResultMessage (); //If an error is prompted, parse the return code
$result=" ";
}
print_r($result);
$connect->quit();
function test($val, $expect, $msg)
{
if($val!= $expect) throw new Exception($msg);
}
?>
Example 3: Combined use of Alibaba Cloud ApsaraDB for Memcache and MySQL database
<?php
$connect = new Memcached; //Declare a new Memcached connection
$connect->setOption(Memcached::OPT_COMPRESSION, false);//Disable the compression function
$connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true);//Use the binary protocol
$connect->setOption(Memcached::OPT_TCP_NODELAY, true); //Important: The php memcached has a bug that when the get value does not exist, there will be a fixed 40 ms of latency. Enabling this parameter can avoid this bug.
$connect->addServer('xxxxxx.m.yyyyyyyy.ocs.aliyuncs.com', 11211);//Add the instance address and port number.
$connect->setSaslAuthData('xxxxxx', 'my_passwd');//Set the ApsaraDB for Memcache account and password for authentication. If the password-free function has been enabled, this step can be skipped.
$user = array(
"name" => "ocs",
"age" => 1,
"sex" => "male"
); //Define an array.
if($connect->get('your_name'))
{
$result =$connect->get('your_name');
print_r($result);
echo "Found in OCS, get data from OCS"; //If data is obtained, print the source of the dataOCS
exit;
}
else
{
echo "Return code:", $connect->getResultCode();
echo "Retucn Message:", $connect->getResultMessage ();//Throw the return code
$db_host='zzzzzz.mysql.rds.aliyuncs.com'; //Database address
$db_name='my_db'; //database name
$db_username='db_user'; //User name of the database
$db_password='db_passwd';//Password of the database
$connection=mysql_connect($db_host,$db_username,$db_password);
if (!mysql_select_db($db_name, $connection))
{
echo 'Could not select database'; //If the database connection fails, an error will be thrown out
exit;
}
$sql = "SELECT name,age,sex FROM test1 WHERE name = 'ocs'";
$result = mysql_query($sql, $connection);
while ($row = mysql_fetch_assoc($result))
{
$user = array(
"name" => $row["name"],
"age" => $row["age"],
"sex" => $row["sex"],
);
$expire = 5; //Set a data expiration time in the cache.
test($connect->set('your_name',$user,$expire), true, 'Set cache failed'); //Write the ApsaraDB for Memcache cache
}
mysql_free_result($result);
mysql_close($connection);
}
print_r($connect->get('your_name')); //Print the data obtained
echo "Not Found in OCS,get data from MySQL"; //Confirm the data obtained from the database
$connect->quit();
function test($val, $expect, $msg)
{
if($val!= $expect) throw new Exception($msg);
}
?>