All Products
Document Center

How to cache PHP global variable of session through ApsaraDB for Memcache

Last Updated: May 31, 2020

I. Scenario

Users may store some data in the global variable of $_SESSION when establishing a website with PHP for convenient accesses. In the ini configuration file of PHP, there are related configurations for [Session] for storing the information to a file or Memcached server. The configuration item of session.save_handler = memcached makes the decision. In most cases, the session data do not require persistence. The session information will be cached into Memcached to improve the website performance.

II. Problems

Here is the problem. We know that Alibaba Cloud ApsaraDB for Memcache complies with the standard Memcached protocol. Users, in the hope of reducing server memory usage and to cut down investment in Memcached maintenance, want to migrate session information stored in the self-built Memcached to ApsaraDB for Memcache without modifying the code. But problems occur during the switch, hence the text which may be helpful.

First, we need to understand the difference between ApsaraDB for Memcache and the self-built Memcached, both of which comply with the standard Memcached protocol. The difference is: ApsaraDB for Memcache is a distributed cluster offering external services in a uniform way. It achieves load balancing without SPOF, and users can adjust the configurations flexibly with no service restarts required.When external services are involved, the corresponding security mechanism will be in place, such as the white list, flow control, account and password authentication.The most important difference between ApsaraDB for Memcache and Memcached is the “account and password authentication”. Most of the self-built Memcached of users do not require account or password settings, so the SASL authentication is missing in self-built Memcached compared with ApsaraDB for Memcache.To migrate session information stored in self-built Memcached to ApsaraDB for Memcache, the account and password need to be configured in php.ini.

III. Solution

1. The feature is not supported in older versions of PHP Memcached extensions. Users need to upgrade PHP Memcached extension to Version 2.2.0. Re-compile the extension following the tutorial on Alibaba Cloud ApsaraDB for Memcache official website.



tar zxvf memcached-2.2.0.tgz

cd memcached-2.2.0


./configure —with-libmemcached-dir=/usr/local/libmemcached —enable-memcached-sasl


make install

2. Find the just upgraded, confirm whether to update it with the stat command and pay attention to the modify time.


3. Modify the php.ini configuration

3.1 Session section

Find the [Session] section and modify the storage engine to:

session.save_handler = memcached (Attention: it has a d)

Modify the storage address, i.e., the ApsaraDB for Memcache access address, to:

session.save_path = “” (Attention: for the extension with a d, no preceding tcp:// is needed. For the extension without a d, the preceding tcp:// is required)

Modify the key time of cache to Memcached

session.gc_maxlifetime = 1440 (Unit: second. A reasonable life time is strongly recommended to ensure that only the hotspot data are cached in ApsaraDB for Memcache)

3.2 Memcached section

In the global section of php.ini, create a separate segment of [memcached], and add the configurations below in the blank space:


memcached.use_sasl = On

memcached.sess_binary = On

memcached.sess_sasl_username = “your_ocs_name”

memcached.sess_sasl_password = “your_ocs_password”

memcached.sess_locking = Off

The install steps are complete. For the useful parameters in the above Memcached and Session sections, refer to the links below:

Next, test whether it works.

IV. Test

Write the testing codes as below in session.php



$sn = session_id();

echo “session id:”.$sn.”\n”;


echo “session:”.$_SESSION[“ocs_key”].”\n”;



session id:ttrct9coa2q62r2sodlq4qf376


Get the written data through session by session.php with the get.php from ApsaraDB for Memcache


$memc = new Memcached();

$memc->setOption(Memcached::OPT_COMPRESSION, false);

$memc->setOption(Memcached::OPT_BINARY_PROTOCOL, true);

$memc->addServer(“”, 11211);

$memc->setSaslAuthData(“your_ocs_name”, “your_ocs_password”);

echo $memc->get(“memc.sess.key.ttrct9coa2q62r2sodlq4qf376”);

/Attention: here the key has a prefix defined by the memcached.sess_prefix field in php.ini. The default value is “memc.sess.key.”The prefix is followed by the sessionid ttrct9coa2q62r2sodlq4qf376“ as shown above. /




It indicates the PHP SESSION has been written into ApsaraDB for Memcache successfully.