Storing Magento sessions in memcached via socket

本文详细介绍了如何在服务器上安装Memcached,并配置Magento使用Memcached进行会话存储。此外,还提供了通过UNIX套接字而非TCP/IP的方式进一步优化Memcached性能的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值