edit-icon download-icon

PHP_ memcached

Last Updated: Mar 22, 2018

Download the client

Download address

About the client

Client versions

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.

  1. 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 command yum install gcc+ gcc-c++.

  2. Run the command rpm –qa | grep php to check whether the PHP environment is ready in the system. If no, run the command yum 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.

  3. 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.

  4. 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).

    1. wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
    2. tar zxvf libmemcached-1.0.18.tar.gz
    3. cd libmemcached-1.0.18
    4. ./configure --prefix=/usr/local/libmemcached --enable-sasl
    5. make
    6. make install
    7. cd ..
  5. 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.

      1. wget http://pecl.php.net/get/memcached-2.2.0.tgz
      2. tar zxvf memcached-2.2.0.tgz
      3. cd memcached-2.2.0
      4. 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).
      5. ./configure --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-sasl(Pay attention to this parameter)
      6. make
      7. make install
  6. 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.

  7. 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)

  1. Check whether the gcc-c++ or other components have been installed. If not, run the command yum install gcc+ gcc-c++.

  2. Run the command rpm –qa | grep php to check whether the PHP environment is ready in the system. If no, run the command yum 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.

  3. Run the command yum install cyrus-sasl-plain cyrus-sasl cyrus-sasl-devel cyrus-sasl-lib to install the SASL-related environments.

  4. 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).

    1. wget http://launchpad.net/libmemcached/1.0/1.0.2/+download/libmemcached-1.0.2.tar.gz
    2. tar -zxvf libmemcached-1.0.2.tar.gz
    3. cd libmemcached-1.0.2
    4. ./configure --prefix=/usr/local/libmemcached --enable-sasl
    5. make
    6. make install
    7. cd ..
  5. 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.

      1. wget http://pecl.php.net/get/memcached-2.0.0.tgz tar -zxvf memcached-2.0.0.tgz
      2. 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).
      3. ./configure --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-sasl(Pay attention to this parameter)
      4. make
      5. make install
  6. 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.

  7. Run the command php –m |grep ,memcached. If the displayed result includes memcache, it indicates that memcache is supported in the environment.

  8. 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

  1. Change the Ubuntu source.

    Solution 1: Run the command vim /etc/apt/source.list and add the following content at the beginning.

    1. deb http://mirrors.aliyun.com/ubuntu/ precise main restricted universe multiverse
    2. deb http://mirrors.aliyun.com/ubuntu/ precise-security main restricted universe multiverse
    3. deb http://mirrors.aliyun.com/ubuntu/ precise-updates main restricted universe multiverse
    4. deb http://mirrors.aliyun.com/ubuntu/ precise-proposed main restricted universe multiverse
    5. deb http://mirrors.aliyun.com/ubuntu/ precise-backports main restricted universe multiverse
    6. deb-src http://mirrors.aliyun.com/ubuntu/ precise main restricted universe multiverse
    7. deb-src http://mirrors.aliyun.com/ubuntu/ precise-security main restricted universe multiverse
    8. deb-src http://mirrors.aliyun.com/ubuntu/ precise-updates main restricted universe multiverse
    9. deb-src http://mirrors.aliyun.com/ubuntu/ precise-proposed main restricted universe multiverse
    10. deb-src http://mirrors.aliyun.com/ubuntu/ precise-backports main restricted universe multiverse
    11. 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 permission chmod 777 file name, and run the script to perform the automatic source change operation.

  2. 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.

  3. 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).

  4. 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.

    1. apt-get install libsasl2-dev cloog-ppl
    2. cd /usr/local/src
  5. 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.

    1. wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
    2. tar -zxvf libmemcached-1.0.18.tar.gz
    3. cd libmemcached-1.0.18
    4. ./configure --prefix=/usr/local/libmemcached
    5. make
    6. make install
    7. cd ..
  6. 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.

    1. wget
    2. http://pecl.php.net/get/memcached-2.2.0.tgz tar zxvf memcached-2.2.0.tgz
    3. cd memcached-2.2.0 phpize5
    4. ./configure --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-sasl
    5. make
    6. make install
  7. Configure PHP to support memcached and then test the configuration.

    1. echo "extension=memcached.so" >>/etc/php5/conf.d/pdo.ini echo "memcached.use_sasl = 1" >>/etc/php5/conf.d/pdo.ini
    2. 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

  1. <?php
  2. $connect = new Memcached; //Declare a new Memcached connection
  3. $connect->setOption(Memcached::OPT_COMPRESSION, false); //Disable the compression function
  4. $connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true); //Use the binary protocol
  5. $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.
  6. $connect->addServer('aaaaaaaaaa.m.yyyyyyyyyyy.ocs.aliyuncs.com', 11211); //Add the ApsaraDB for Memcache instance address and port number
  7. $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.
  8. $connect->set("hello", "world");
  9. echo 'hello: ',$connect->get("hello");
  10. $connect->quit();
  11. ?>

Example 2: Cache an array in Alibaba Cloud ApsaraDB for Memcache

  1. <?php
  2. $connect= new Memcached; //Declare a new Memcached connection
  3. $connect->setOption(Memcached::OPT_COMPRESSION, false); //Disable the compression function
  4. $connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true);//Use the binary protocol
  5. $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.
  6. $connect->addServer('xxxxxxxx.m.yyyyyyyy.ocs.aliyuncs.com', 11211);//Add the ApsaraDB for Memcache instance address and port number
  7. $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.
  8. $user = array(
  9. "name" => "ocs",
  10. "age" => 1,
  11. "sex" => "male"
  12. ); //Declare an array.
  13. $expire = 60; //Set an expiration time.
  14. test($connect->set('your_name',$user,$expire), true, 'Set cache failed');
  15. if($connect->get('your_name')){
  16. $result =$connect->get('your_name');
  17. }else{
  18. echo "Return code:", $connect->getResultCode();
  19. echo "Retucn Message:", $connect->getResultMessage (); //If an error is prompted, parse the return code
  20. $result=" ";
  21. }
  22. print_r($result);
  23. $connect->quit();
  24. function test($val, $expect, $msg)
  25. {
  26. if($val!= $expect) throw new Exception($msg);
  27. }
  28. ?>

Example 3: Combined use of Alibaba Cloud ApsaraDB for Memcache and MySQL database

  1. <?php
  2. $connect = new Memcached; //Declare a new Memcached connection
  3. $connect->setOption(Memcached::OPT_COMPRESSION, false);//Disable the compression function
  4. $connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true);//Use the binary protocol
  5. $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.
  6. $connect->addServer('xxxxxx.m.yyyyyyyy.ocs.aliyuncs.com', 11211);//Add the instance address and port number.
  7. $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.
  8. $user = array(
  9. "name" => "ocs",
  10. "age" => 1,
  11. "sex" => "male"
  12. ); //Define an array.
  13. if($connect->get('your_name'))
  14. {
  15. $result =$connect->get('your_name');
  16. print_r($result);
  17. echo "Found in OCS, get data from OCS"; //If data is obtained, print the source of the dataOCS
  18. exit;
  19. }
  20. else
  21. {
  22. echo "Return code:", $connect->getResultCode();
  23. echo "Retucn Message:", $connect->getResultMessage ();//Throw the return code
  24. $db_host='zzzzzz.mysql.rds.aliyuncs.com'; //Database address
  25. $db_name='my_db'; //database name
  26. $db_username='db_user'; //User name of the database
  27. $db_password='db_passwd';//Password of the database
  28. $connection=mysql_connect($db_host,$db_username,$db_password);
  29. if (!mysql_select_db($db_name, $connection))
  30. {
  31. echo 'Could not select database'; //If the database connection fails, an error will be thrown out
  32. exit;
  33. }
  34. $sql = "SELECT name,age,sex FROM test1 WHERE name = 'ocs'";
  35. $result = mysql_query($sql, $connection);
  36. while ($row = mysql_fetch_assoc($result))
  37. {
  38. $user = array(
  39. "name" => $row["name"],
  40. "age" => $row["age"],
  41. "sex" => $row["sex"],
  42. );
  43. $expire = 5; //Set a data expiration time in the cache.
  44. test($connect->set('your_name',$user,$expire), true, 'Set cache failed'); //Write the ApsaraDB for Memcache cache
  45. }
  46. mysql_free_result($result);
  47. mysql_close($connection);
  48. }
  49. print_r($connect->get('your_name')); //Print the data obtained
  50. echo "Not Found in OCS,get data from MySQL"; //Confirm the data obtained from the database
  51. $connect->quit();
  52. function test($val, $expect, $msg)
  53. {
  54. if($val!= $expect) throw new Exception($msg);
  55. }
  56. ?>
Thank you! We've received your feedback.