All Products
Search
Document Center

ApsaraDB for Memcache:Cache PHP session variables

最終更新日:Aug 08, 2023

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:

     wget http://pecl.php.net/get/memcached-2.2.0.tgz
     tar  zxvf  memcached-2.2.0.tgz
    
     cd memcached-2.2.0
    
     phpize
    
     ./configure --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-sasl
    
     make
    
     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:

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

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

      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:

      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:

      [memcached]
      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 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:

<?php
session_start();
$sn = session_id();
echo "session id:".$sn."\n";
$_SESSION["ocs_key"]="session_value";
echo "session:".$_SESSION["ocs_key"]."\n";
?>

The output is as follows:

session id:ttrct9coa2q62r2sodlq4qf376

session:session_value

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

<?php
$memc = new Memcached();
$memc->setOption(Memcached::OPT_COMPRESSION, false);
$memc->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$memc->addServer("be6b6b8221cc11e4.m.cnhzalicm10pub001.ocs.aliyuncs.com", 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. */
?>

Output:

ocs_key|s:13:"session_value";

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