Magento2配置多级Redis缓存及Memcached存储Session机制

  • 31 Oct 2015
  • Posted by admin
  • 0 Comments

Magento2麦进斗配置多级Redis缓存及Memcached存储Session机制

首选可安装Remi,Remi repository 是包含最新版本 PHP 和 MySQL 包的 Linux 源,由 Remi 提供维护。有个这个源之后,使用 YUM 安装或更新 PHP、MySQL、phpMyAdmin 等服务器相关程序的时候就非常方便了。

1
yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm

安装最新版redis, memcached和php拓展:

1
yum --enablerepo=remi, remi-php70 -y install redis memcached php-pecl-redis php-pecl-memcache php-pecl-lzf

在 6379 和6380端口配置Redis缓存:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
systemctl disable redis
 
for REDISPORT in 6379 6380
do
mkdir -p /var/lib/redis-${REDISPORT}
chmod 755 /var/lib/redis-${REDISPORT}
chown redis /var/lib/redis-${REDISPORT}
\cp -rf /etc/redis.conf /etc/redis-${REDISPORT}.conf
\cp -rf /usr/lib/systemd/system/redis.service /usr/lib/systemd/system/redis-${REDISPORT}.service
 
sed -i "s/daemonize no/daemonize yes/"  /etc/redis-${REDISPORT}.conf
sed -i "s/^bind 127.0.0.1.*/bind 127.0.0.1/"  /etc/redis-${REDISPORT}.conf
sed -i "s/^dir.*/dir \/var\/lib\/redis-${REDISPORT}\//"  /etc/redis-${REDISPORT}.conf
sed -i "s/^logfile.*/logfile \/var\/log\/redis\/redis-${REDISPORT}.log/"  /etc/redis-${REDISPORT}.conf
sed -i "s/^pidfile.*/pidfile \/var\/run\/redis\/redis-${REDISPORT}.pid/"  /etc/redis-${REDISPORT}.conf
sed -i "s/^port.*/port ${REDISPORT}/" /etc/redis-${REDISPORT}.conf
sed -i "s/redis.conf/redis-${REDISPORT}.conf/" /usr/lib/systemd/system/redis-${REDISPORT}.service
done
 
mv /usr/lib/systemd/system/redis.service /usr/lib/systemd/system/redis.service.OFF
 
systemctl daemon-reload
systemctl enable redis-6379
systemctl enable redis-6380

配置Memcached使用localhost和内存:

1
2
3
4
5
6
7
cat > /etc/sysconfig/memcached <<END
PORT="11211"
USER="memcached"
MAXCONN="5024"
CACHESIZE="128"
OPTIONS="-l 127.0.0.1"
END

在app/etc/env.php文件中配置Magento2缓存和Session

打开 app/etc/env.php文件

找到以下代码:

1
2
3
4
'session' =>
    array (
    'save' => 'files',
),

修改为:

1
2
3
4
5
'session' =>
          array (
            'save' => 'memcache',
            'save_path' => 'tcp://127.0.0.1:11211?persistent=1&weight=2&timeout=10&retry_interval=10'
           ),
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'cache' =>
        array (
            'frontend' => array(
                'default' => array(
                    'backend' => 'Cm_Cache_Backend_Redis',
                    'backend_options' => array(
                        'server' => '127.0.0.1',
                        'port' => '6379',
                        'persistent' => '',
                        'database' => '0',
                        'password' => '',
                        'force_standalone' => '0',
                        'connect_retries' => '1',
                        'read_timeout' => '10',
                        'automatic_cleaning_factor' => '0',
                        'compress_data' => '1',
                        'compress_tags' => '1',
                        'compress_threshold' => '20480',
                        'compression_lib' => 'lzf',
                        'use_lua' => '0'
                    )
                ),
                'page_cache' => array(
                    'backend' => 'Cm_Cache_Backend_Redis',
                    'backend_options' => array(
                        'server' => '127.0.0.1',
                        'port' => '6380',
                        'persistent' => '',
                        'database' => '0',
                        'password' => '',
                        'force_standalone' => '0',
                        'connect_retries' => '1',
                        'lifetimelimit' => '57600',
                        'compress_data' => '0'
                    )
                )
            )
        ),

测试您的Redis连接:

1
for port in 6379, 6380; do echo info | redis-cli -p ${port} | tail -1; done