php session in Memcached
2014-01-26 11:12
204 查看
http://www.ducea.com/2009/06/02/php-sessions-in-memcached/
http://allurcode.com/2010/03/16/php-session-handling-with-memcache/
The moment a PHP application grows to run on more servers, normally people will see problems caused byPHP sessions. If the application is not persistent you are lucky and don’t care about this, but if not you will quickly see this
regardless of how good the load balancer you use is handling stickiness (sending the users to the same real server), this will slowly become a major issue. There are various solutions that can be used to store PHP sessions in ashared location,
but I want to present today one solution that is very simple to implement, yet very efficient and on the long term better suited than using a database backend for this: usingmemcache
to store the sessions.
The
pecl memcache php extension has supported for a long time the memcache session.save_handler, but with the release3.0.x (still in beta at this time) this brings in a set of interestingfeatures
for us:- UDP support- Binary protocol support- Non-blocking IO using select()- Key and session redundancy (values are written to N mirrors)- Improved error reporting and failover handling
Installing the php memcache module is very simple and can be done either by using distribution repositories (the version we want to use 3.0.x will probably not be available) or by using pecl or manual compilation:
Using pecl:
or manually:
Finally, we need to activate the module in php.ini. I normally prefer to create a new file for thismemcache.ini inside the include directory of the php build (for ex. in debian this is under/etc/php5/conf.d/memcache.ini)
like this:
To use memcached to store the php sessions we will have to edit php.ini and replace the default file handler settings with something like:
or if we don’t want to use this serverwide, we can just define it inside a php block like this:
Note: as you can see I used in the above example tcp and udp also. Please be sure that your memcached server has udp support enabled if you want to use that. Also ensure that the web server can connect to the memcache server/port (use proper firewall rules
to allow this) in order for this to work.
After restarting apache, it will start using memcache to store the php sessions. If redundancy is needed (why not?) we will probably want to use the internal php memcache support to save the sessions to more servers, or if you prefer you can use an external
solution to replicate the memcached server data -
repcached.
http://allurcode.com/2010/03/16/php-session-handling-with-memcache/
The moment a PHP application grows to run on more servers, normally people will see problems caused byPHP sessions. If the application is not persistent you are lucky and don’t care about this, but if not you will quickly see this
regardless of how good the load balancer you use is handling stickiness (sending the users to the same real server), this will slowly become a major issue. There are various solutions that can be used to store PHP sessions in ashared location,
but I want to present today one solution that is very simple to implement, yet very efficient and on the long term better suited than using a database backend for this: usingmemcache
to store the sessions.
The
pecl memcache php extension has supported for a long time the memcache session.save_handler, but with the release3.0.x (still in beta at this time) this brings in a set of interestingfeatures
for us:- UDP support- Binary protocol support- Non-blocking IO using select()- Key and session redundancy (values are written to N mirrors)- Improved error reporting and failover handling
Installing the php memcache module is very simple and can be done either by using distribution repositories (the version we want to use 3.0.x will probably not be available) or by using pecl or manual compilation:
Using pecl:
1 | pecl install memcache-3.0.4 |
12 | wget http://pecl.php.net/get/memcache-3.0.4.tgz tar xvfz memcache-3.0.4.tgz cd memcache-3.0.4 phpize ./configure make make install |
like this:
12 | extension=memcache.so |
12 | ; Use memcache as a session handler session.save_handler = memcache ; Use a comma separated list of server urls to use for storage: session.save_path="udp://<memcache_server>:11211?persistent=1&weight=1&timeout=1&retry_interval=15" |
12 | $session_save_path = "tcp://<memcache_server1>:11211?persistent=1&weight=1&timeout=1&retry_interval=15, tcp://<memcache_server2>:11211"; ini_set('session.save_handler', 'memcache'); ini_set('session.save_path', $session_save_path); |
to allow this) in order for this to work.
After restarting apache, it will start using memcache to store the php sessions. If redundancy is needed (why not?) we will probably want to use the internal php memcache support to save the sessions to more servers, or if you prefer you can use an external
solution to replicate the memcached server data -
repcached.
相关文章推荐
- memcache和memcached的区别
- centos系统为php安装memcached扩展步骤
- (转)如何最佳地使用memcached?
- win7 安装 memcached
- memcached的应用场景
- memcached简介
- 【NOSQL 】 memcache 安装及配置分布式集群 双向复制
- 对比下HBase, Memcached, MongoDB, Redis和Solr
- 一致性Hash算法在Memcached中的应用
- memcache和redis本质区别在哪里?
- GDB分析PHP连接Memcached 导致coredump问题
- 谈谈Memcached与Redis
- 通过telnet连接查看memcache服务器
- Memcached 集群架构问题归纳
- Installing Memcached for PHP 5.3 on Windows 7
- memcached服务器的安装
- MySQL触发器自动更新memcache[转]
- Nginx + Tomcat + MSM + Memcached 非粘性Session共享测试
- memcached + php 扩展 for ubuntu
- GDB分析PHP连接Memcached 导致coredump问题