edit-icon download-icon

Cache PHP session variables

Last Updated: Dec 20, 2017

Background

You may store some data in the global variable $_SESSION for convenient access when establishing a PHP website. The ini configuration file of PHP contains [Session] configurations for storing the data to a file or the Memcached server. The configuration item session.save_handler = memcached makes the decision. In most cases, the session data does not require persistence. The session information is cached into Memcached to improve the website performance.

Problem description

ApsaraDB for Memcache and self-built Memcached both comply with the standard Memcached protocol. Some users, in a hope to reduce server memory usage and cut down on investment in Memcached maintenance, want to migrate session information stored in the self-built Memcached to ApsaraDB for Memcache without modifying the code. This document guides you to tackle with the problems, which may arise during the migration process.

The most important difference between ApsaraDB for Memcache and self-built Memcached is the account and password authentication.

  • ApsaraDB for Memcache: ApsaraDB for Memcache is a distributed cluster offering external services in a uniform way. It achieves load balance without SPOF, and you can adjust the configuration flexibly without restarting the service. When external services are involved, the corresponding security mechanism is in place, such as the whitelist, throttling, and account and password authentication.

  • Self-built Memcached: Most of the self-built Memcached instances do not require an account or password, 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.

Solution

  1. The feature is not supported in older versions of PHP Memcached extensions. You must upgrade PHP Memcached extension to Version 2.2.0. The example code is as follows:

    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
    5. ./configure --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-sasl
    6. make
    7. make install
  2. Find the upgraded memcached.so, run the stat command to confirm whether it has been upgraded (pay attention to the last modified time).

  3. Modify the php.ini configuration.

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

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

      Modify the storage address, that is, the Memcache access address, to:

      1. session.save_path = "be6b6b8221cc11e4.m.cnhzalicm10pub001.ocs.aliyuncs.com:11211" (Attention: for an extension with a d, no preceding tcp:// is needed. For an extension without a d, the preceding tcp:// is required)

      Modify the key time for caching to Memcached:

      1. session.gc_maxlifetime = 1440 (Unit: second. A reasonable life time is strongly recommended to make sure that only hotspot data is cached in ApsaraDB for Memcache)
    2. Memcached section: In the global section of php.ini, create a separate [memcached] section, and add the following configuration in the blank space:

      1. [memcached]
      2. memcached.use_sasl = On
      3. memcached.sess_binary = On
      4. memcached.sess_sasl_username = "your_ocs_name"
      5. memcached.sess_sasl_password = "your_ocs_password"
      6. memcached.sess_locking = Off

The installation steps are completed. For other useful parameters in the preceding Memcached and Session sections, see the following links.

http://php.net/manual/en/memcached.configuration.php

http://php.net/manual/en/session.configuration.php

Testing

Write the following testing code as in session.php:

  1. <?php
  2. session_start();
  3. $sn = session_id();
  4. echo "session id:".$sn."\n";
  5. $_SESSION["ocs_key"]="session_value";
  6. echo "session:".$_SESSION["ocs_key"]."\n";
  7. ?>

The output is as follows:

  1. session id:ttrct9coa2q62r2sodlq4qf376
  2. session:session_value

Get the data written by session.php from Memcache through the testing code in get.php.

  1. <?php
  2. $memc = new Memcached();
  3. $memc->setOption(Memcached::OPT_COMPRESSION, false);
  4. $memc->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
  5. $memc->addServer("be6b6b8221cc11e4.m.cnhzalicm10pub001.ocs.aliyuncs.com", 11211);
  6. $memc->setSaslAuthData("your_ocs_name", "your_ocs_password");
  7. echo $memc->get("memc.sess.key.ttrct9coa2q62r2sodlq4qf376");
  8. /*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. */
  9. ?>

Output:

  1. ocs_key|s:13:"session_value";

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

Thank you! We've received your feedback.