Tuesday 19 March 2019

Redis Integration with Drupal 8

Redis

  • Redis is a in-memory, key-value data store. Redis is the most popular key-value data store. 
  • Redis is used by all big IT brands in this world. Amazon Elastic Cache supports Redis which makes redis a very powerful and must know key-value data store.
  • Key-Value store is a storage system where data is stored in the form of key and value pairs.
  • In-memory Key-Value store means Key-Value pairs are stored in primary memory (RAM).
  • Redis stores data in the form of Key-Value in RAM.
  • Key: This has to be string
  • Value: This can be string, list, set or hash
  • Example: 
           name="raj"
           skills=["Drupal8", "Drupal7"]
  • In Database Management system, everything got stored in secondary storage due to which read and write operations are very slow But Redis store everything in primary memory and very fast to read/write of data.
  • Redis can only store small textual information which has to access, modify or insert with fast speed because It stores in primary memory which is very expensive and lesser size.
  • Redis Architecture having 2 processes:

              1. Redis clientRedis server is responsible for storing data in memory.
              2. Redis server: Redis client can be redis console client which is responsible for sending and receiving data.

Integrating Redis with Drupal 8

Requirement: 
  • PHP >= 7.0.28
  • Install Phpredi module/extension for PHP to communicate: PHPRedis

Follow below steps:
1. Run command:
       sudo apt-get update
   sudo apt-get upgrade
   sudo apt-get install software-properties-common
   sudo add-get-repository ppa:chris-lea/redis-server
   sudo apt-get update
   sudo apt-get upgrade
2. git clone https://github.com/phpredis/phpredis.git
3. cd phpredis
4. phpize
5. ./configure
6. make
7. make install
8. make test
9. cd ..
10. rm –f phpredis
11. echo “extension=redis.so” > /etc/php/7.0/mods-available/redis.ini
12. ln –sf /etc/php/7.0/mods-available/redis.ini  /etc/php/7.0/fpm/conf.d/20-redis.ini
13. ln –sf /etc/php/7.0/mods-available/redis.ini  /etc/php/7.0/cli/conf.d/20-redis.ini
14. service php7.0-fpm restart
15. put extension=redis.so in /etc/php/7.0/apache2/php.ini
16. Restart apache2
17. Now download Drupal module with drush: drush dl redis. OR go to page: https://www.drupal.org/project/redis
18. Now configure redis in Drupal. Now update settings.php file with following code.

/**
* Redis settings
*/ 
$settings['redis.connection']['interface'] = 'PhpRedis';       // Can be "Predis" in the future
$settings['redis.connection']['host'] = '127.0.0.1';         // Your Redis instance hostname
$settings['cache_prefix'] = 'cms_';         // Optional prefix for cache entries

$settings['cache']['default'] = 'cache.backend.redis';  // The default cache engine for the site
// Always set the fast backend for bootstrap, discover and config, otherwise this gets lost when redis is enabled.
$settings['cache']['bins']['bootstrap']    = 'cache.backend.chainedfast';
$settings['cache']['bins']['discovery']    = 'cache.backend.chainedfast';
$settings['cache']['bins']['config']       = 'cache.backend.chainedfast';
$settings['cache']['bins']['container']       = 'cache.backend.redis';
$settings['cache']['bins']['data']       = 'cache.backend.redis';
$settings['cache']['bins']['default']       = 'cache.backend.redis';
$settings['cache']['bins']['dynamic_page_cache']       = 'cache.backend.redis';
$settings['cache']['bins']['entity']       = 'cache.backend.redis';
$settings['cache']['bins']['menu']       = 'cache.backend.redis';
$settings['cache']['bins']['page']       = 'cache.backend.redis';
$settings['cache']['bins']['render']       = 'cache.backend.redis';
$settings['cache']['bins']['rest']       = 'cache.backend.redis';
$settings['cache']['bins']['signal']       = 'cache.backend.redis';
$settings['cache']['bins']['toolbar']       = 'cache.backend.redis';
$settings['cache']['bins']['ultimate_cron_logger']       = 'cache.backend.redis';

$settings['container_yamls'][] = 'modules/redis/example.services.yml';
$settings['container_yamls'][] = 'modules/redis/redis.services.yml';

//Register our namespace
$class_loader->addPsr4('Drupal\\redis\\', 'modules/redis/src');

19. Start redis-server, run command: redis-server
20. Now in CLI windows: redis-cli monitor. this command will print OK means it is working fine. 

21. Check if redis is configured with Drupal. Go to admin > Reports > Status Report. Click on Status report and see the status report page.





22. Now access any Drupal website page and now you will be able to see the logs as below with the following command: redis-cli. Now this command will take to the redis client terminal.



Now here type below command to see redis cache storage.

Command: keys ‘cms*’ as in below screen shot.


If you want to see specific logs, then run above command as below: keys ‘cms_:entity*’

No comments:

Post a Comment