PHP Sessions and Data Persistence Storing Sessons in Memcached - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Sessions and Data Persistence Storing Sessons in Memcached - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Thursday, June 13, 2019

PHP Sessions and Data Persistence Storing Sessons in Memcached

PHP Sessions and Data Persistence


Storing Sessons in Memcached

Problem

You want to store session data somewhere that’s fast and can be accessed by multiple webservers.

Solution

Use the session handler built into the memcached extension to store your sessions in one or more Memcached servers. With the memcached extension installed, set the session.save_handler configuration directive to memcached and then set session.save_path to the host and port of your Memcached server. For example, if your Memcached server is running on port 11211 of host 10.5.7.12, set session.save_path to 10.5.7.12:11211. If you are using multiple Memcached servers, make session.save_path a comma-separated list of the host:port values.

Once you specify the appropriate session.save_handler and session.save_path that tells PHP to store your session info in Memcached, you don’t have to do anything to your $_SESSION-using code to make it work properly. Because the session persistence backend is so easily pluggable, you can just change the configuration and it works.

If you are using consistent hashing with multiple Memcached servers to distribute your values across servers, set the configuration directive memcached.sess_consistent_hash to on. This ensures that your session data is also spread across the multiple Memcached servers.

Note that apart from the memcached PHP extension, there is also a memcache (no d on the end) PHP extension. It also has a built-in session handler. To use that session handler, set session.save_handler to memcache. The session.save_path configuration directive is used to indicate your Memcached servers, but the syntax is slightly different than the memcached extension. You need to prefix the hostname with the appropriate protocol (e.g., tcp://) and you can add query string–style name=value pairs to set any option that Memcache::addServer() accepts. For example, you could set session.save_path to tcp://10.5.7.12:11211?weight=3,tcp://10.5.7.13:11211? weight=5 to specify two servers—10.5.7.12 and 10.5.7.12—each running on port 11211 but weighted differently.

Both extensions can capably store sessions in Memcache. The memcached extension supports some different compression schemes for storing large pieces of data in Memcache. The memcache extension has a few less features but does not depend on any external libraries

Note that a Memcached server itself does not persist the data it stores across restarts— it only holds it in memory while it’s running. That means that session data in Memcached could disappear if one of your Memcached servers crashes or is restarted.


No comments:

Post a Comment

Post Top Ad