понедельник, 28 октября 2019 г.

Linux Ubuntu 18.04 Use redis for cache pages and PHP sessions storage

apt update -y && apt install redis-server php-redis

/etc/php/7.2/fpm/pool.d/user.conf
php_admin_value[session.save_handler] = redis
php_admin_value[session.save_path] = "tcp://127.0.0.1:6379?persistent=1&weight=1&database=2"

phpinfo should display redis info on the page.
There is link https://github.com/phpredis/phpredis#php-session-handler for save_path parameters

The checking script is 

<?php

  $redisinstance = new Redis();
  $redisinstance->connect("127.0.0.1", 6379);
  $result = $redisinstance->get("test");

  if ($result) {
      echo $result;
  } else {
      echo "No matching key found. Refresh the browser to add it!";
      $redisinstance->set("test", "Successfully retrieved the data!") or die("Couldn't save anything to redis...");
  }
?>
After refreshing page

redis-cli
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> keys "*"
1) "test"
127.0.0.1:6379> get "test"
"Successfully retrieved the data!"

How to enable redis sessions and cache in Magento

Page caching

bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=127.0.0.1 --cache-backend-redis-db=0
We saved default values for these options: amqp-host, amqp-port, amqp-user, amqp-password, amqp-virtualhost, amqp-ssl, amqp-ssl-options.

bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=127.0.0.1 --page-cache-redis-db=1
We saved default values for these options: amqp-host, amqp-port, amqp-user, amqp-password, amqp-virtualhost, amqp-ssl, amqp-ssl-options.

Session caching

bin/magento setup:config:set --session-save=redis --session-save-redis-host=127.0.0.1 --session-save-redis-log-level=3 --session-save-redis-db=2
Overwrite the existing configuration for session-save?[Y/n]y
We saved default values for these options: amqp-host, amqp-port, amqp-user, amqp-password, amqp-virtualhost, amqp-ssl, amqp-ssl-options.

Checking

redis-cli monitor

After refreshing shop page
1572252738.470944 [0 127.0.0.1:59236] "select" "1"
1572252738.476874 [0 127.0.0.1:59238] "hget" "zc:k:2d4_GLOBAL__DICONFIG" "d"
1572252738.483920 [0 127.0.0.1:59238] "hget" "zc:k:2d4_DICONFIG0A7134A0C61B6CBD27B7713EBF5327E4" "d"
1572252738.489531 [0 127.0.0.1:59238] "hget" "zc:k:2d4_INTERCEPTION" "d"
1572252738.502277 [0 127.0.0.1:59240] "select" "1"
1572252738.502735 [0 127.0.0.1:59242] "hget" "zc:k:2d4_GLOBAL_PRIMARY_PLUGIN_LIST" "d"
1572252738.509947 [0 127.0.0.1:59242] "hget" "zc:k:2d4_SYSTEM_DEFAULT" "d"
1572252738.513050 [0 127.0.0.1:59242] "hget" "zc:k:2d4_RESOLVED_STORES_D1BEFA03C79CA0B84ECC488DEA96BC68" "d"
1572252738.536886 [0 127.0.0.1:59242] "hget" "zc:k:2d4_SYSTEM_STORES_DEFAULT" "d"
1572252738.538672 [0 127.0.0.1:59242] "hget" "zc:k:2d4_FRONTEND__DICONFIG" "d"
1572252738.539455 [0 127.0.0.1:59238] "hget" "zc:k:2d4_DICONFIGBA0EC425EDF04611B2A61C4617CE2671" "d"
1572252738.545265 [0 127.0.0.1:59242] "hget" "zc:k:2d4_GLOBAL_PRIMARY_FRONTEND_PLUGIN_LIST" "d"
1572252738.563835 [0 127.0.0.1:59242] "hget" "zc:k:2d4_EXTENSION_ATTRIBUTES_CONFIG" "d"
1572252738.566118 [0 127.0.0.1:59242] "hget" "zc:k:2d4_GLOBAL__EVENT_CONFIG_CACHE" "d"
1572252738.566679 [0 127.0.0.1:59242] "hget" "zc:k:2d4_FRONTEND__EVENT_CONFIG_CACHE" "d"
1572252738.573593 [0 127.0.0.1:59242] "hget" "zc:k:2d4_FRONTEND__RESOURCESCACHE" "d"
1572252738.576852 [0 127.0.0.1:59242] "hget" "zc:k:2d4_EAV_ENTITY_TYPES" "d"
1572252738.580051 [1 127.0.0.1:59240] "hget" "zc:k:2d4_BD5AA36C557EF082E117F1BA654401B5204FAB97" "d"

1572263520.460285 [2 127.0.0.1:59428] "SELECT" "2"
1572263520.460439 [2 127.0.0.1:59428] "GET" "PHPREDIS_SESSION:rd2hma5hltcmlq8gokto58nutm"
1572263520.490096 [2 127.0.0.1:59428] "SELECT" "2"
1572263520.490245 [2 127.0.0.1:59428] "EXPIRE" "PHPREDIS_SESSION:rd2hma5hltcmlq8gokto58nutm" "86400"







where 0, 1, 2 are redis databses in [0 127.0.0.1:59236], 1 127.0.0.1:59240], [2 127.0.0.1:59428] rows.

How to flush redis databases

redis-cli FLUSHALL

flush all databases

redis-cli -n <database number> FLUSHDB

flush specific database

How to show keys and flush current database

redis-cli
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> keys "*"
1) "PHPREDIS_SESSION:rd2hma5hltcmlq8gokto58nutm"
127.0.0.1:6379[2]> flushdb
OK
127.0.0.1:6379[2]> keys "*"
(empty list or set)


Комментариев нет:

Отправить комментарий