Storing Magento sessions in memcached via socket

来源:互联网 发布:excel防止数据粘贴 编辑:程序博客网 时间:2024/05/19 21:41

Installing memcached on your server

To use memcached in Magento, you will first need to install it on your server. As root you can use your package manager to install memcached. With CentOS Linux this will look like this:

yum install memcached php-pecl-memcached

After installing the packages, the service needs to be started up. We also use chkconfig to start the service automatically after reboots:

service memcached start
chkconfig memcached on

Now the memcached service is ready for use.

Configuring Magento for storing sessions in memcache

Next, the Magento application needs to be told to save sessions in the memcached database. To do this, we open up the Magento file app/etc/local.xml and locate the line mentioning <session_save>. When sessions are currently save in the database, it might look like this:

<session_save><![CDATA[database]]></session_save>

Replace this line with the following lines:

<session_save><![CDATA[memcache]]></session_save>
<session_save_path>
<![CDATA[tcp://127.0.0.1:11211?persistent=1&weight=2&timeout=10&retry_interval=10]]>
</session_save_path>

Afterwards, flush the Magento cache. Now, sessions should be saved into the memcached database. If you want to, you can check out the memcached-tool to see statistics on how the memcached database is behaving:

memcached-tool 127.0.0.1:11211 display
memcached-tool 127.0.0.1:11211 stats

Reconfiguring memcached to run over a UNIX socket

Now, memcached by default is setup for networking and is listening to the TCP-port 11211. This is useful when you have multiple servers, where for instance a Magento webserver is connecting to a different server for the database. The memcached database can then be accessed remotely via the network. But if you have only a single server, running Magento, MySQL and everything, then using memcached through TCP/IP is actually a (very small) waste of resources. Instead of using TCP/IP, you can also use UNIX sockets to skip networking and still use memcached.

To do this, memcached needs to reconfigured to listen to a UNIX-socket instead. Open up the file /etc/sysconfig/memcached and locate the following line:

OPTIONS=""

Replace it with this line:

OPTIONS="-s /var/tmp/memcache.socket -a 0777"

This instructs memcached to bind itself not to a TCP/IP socket but a file socket /var/tmp/memcache.socket instead. Because the memcached service is being ran as UNIX-user memcached, the socket will also be owned by this user. To allow other UNIX-users like the webserver-user to read and write to memcached, we set the permissions to 777. If your servers is setup as shared hosting server, you will definitely need to look into the security aspects of this.

Restart the memcached service, so the changes take effect:

service memcached restart

You can verify now whether memcached is being used by using the memcached-tool command as follows:

memcached-tool /var/tmp/memcache.socket display

Reconfiguring Magento to talk to memcached via the UNIX socket

Next, we go back to the Magento app/etc/local.xml to reconfigure it so that Magento will access memcached via UNIX-socket and not TCP/IP. Locate the previous lines again and replace them with the following:

<session_save><![CDATA[memcache]]></session_save>
<session_save_path>
<![CDATA[unix:///var/tmp/memcache.socket?persistent=1&weight=2&timeout=10&retry_interval=10]]>
</session_save_path>

Again flush the Magento cache. If all goes well, memcached should still be usable and now accessible through the UNIX-socket instead.

0 0
原创粉丝点击