Integration of Redis cache with Drupal 8
Installing of PHPRedis Library
- First you need to install PHPRedis Library for using redis cache with Drupal instance.
- In case of Fedora OS, use the following yum command for installing PHPRedis Library
- sudo yum install php-pecl-redis
- In case of Ubuntu OS, use the following apt-get command
- sudo apt-get install php-redis
Installing Redis tools:
- Download the Stable version of Redis package from Redis.io and untar it as below.
- tar -xvfz redis-stable.tar.gz
- Switch to redis folder
- cd redis-stable
- Check whether GCC/CC compiler is installed in your system,if not, install it
- yum -y install gcc
- Create a make file for the redis dependencies i.e,. for the following folders hiredis,jemalloc,linenoise and lua.
- You can find these directories under redis-stable/deps/ directory.
- make hiredis lua jemalloc linenoise
- Then, switch to redis-stable directory and create a make file for the redis
- make
- Make execution will ends as below
- For confirming, the proper installation of Redis, type
- make test
- Make test will ends as below
- After installing you will find, set of execution files in src/ directory namely redis-cli,redis-server,etc.,
- Standard way of using redis-cli and redis-server is copying those two files to /usr/local/bin, so that it can be accessed from any location.
- sudo cp -r redis-cli redis-server /usr/local/bin/
Working with Redis:
- First start the Redis server by executing below command
- redis-server
- Open a new tab,type the below command for interacting with Redis
- redis-cli
- You can test Redis by setting and getting key-value pair.
- Set Key-Value:
- redis127.0.0.1:6379>set <key> <value>
- For example: redis127.0.0.1:6379>set dolby 5.1
- Get Value:
- redis127.0.0.1:6379>get <key>
- For example: redis127.0.0.1:6379>get dolby 5.1
- Listing all the keys
- redis127.0.0.1:6379>keys *
Integrating Redis cache with Drupal Instance:
- Download and Install Redis Module from http://drupal.org.
- Include the following lines code in settings.php of your instance.
- $settings['redis.connection']['interface'] = 'PhpRedis'; // Can be "Predis".
- $settings['redis.connection']['host'] = 'localhost'; // Your Redis instance hostname.
- $settings['cache']['default'] = 'cache.backend.redis';
Checking the Redis cache integration with your Drupal Instance:
- Start the redis server.
- Use redis command line interface $redis-cli
- If redis cache integrated successfully your instance,then on executing keys *, you will be able to see list of key-value pair as below.