Skip to main content
Image
blog-banner-opensenselabs%20%282%29.jpg

HowTo: Store Cache in Redis bins in Drupal 8?

article publisher

Bikash

Drupal

Redis, an acronym for Remote Dictionary Server is an open-source in-memory database project implementing a distributed, in-memory key-value store with optional durability.

An in-memory database also called a main memory database system or a memory resident database is a system that basically uses the main memory or Random Access Memory (RAM) for data storage which makes it faster than disk-optimized databases because of faster access speeds.

A key-value store system uses a dictionary or hash tables which contains a collection of objects with many different fields which stores data as a single collection meaning it can have different fields for each record. This system uses far less memory than conventional RDBs. 

table with 5 rows and 2 columns with key values

Some of the common use cases of Redis are:

  • Caching
  • Publish and Subscribe
  • Queues
  • Counters

In this article, we will have a look at how we can use Redis bin to store cache in Drupal 8.

  • Caching

Caching is a means to decrease the time required to retrieve data such that data requests are processed efficiently. It does so by storing an earlier result of the same request in the cache memory.

Caching in Redis

Redis stores the whole dataset in memory where a snapshot of the data is transferred asynchronously from memory to disk at least every 2 seconds. This is done because Redis uses RAM for caching and since RAM is a volatile memory, data might get lost if there is a power failure.

Requirements

To use Redis bin instead of the default database, we would need to do as follows.

  • The Redis Module

To use Redis for caching in Drupal, we need to install the Redis module

  • PHP Extension

For communicating with the server, a Redis client library is required which provides an API for communicating with the Redis key-value store. Clients are available for various languages which are listed in https://redis.io/clients.

  • Installing and Configuring the Redis Server

Run the following commands to install the Redis server.

sudo apt-get update && sudo apt-get upgrade

sudo apt-get install software-properties-common

sudo add-apt-repository ppa:chris-lea/redis-server

sudo apt-get update && sudo apt-get upgrade

sudo apt-get install redis-server

Well, now that we have the required pieces, we need to configure the server such that Drupal uses Redis instead of the inbuilt cache database tables and in order to do that, we would need to modify the settings.php file by adding the following lines of code in the said file.

/**
*Configuration.
*/
$conf[‘chq_redis_cache_enabled’] = TRUE;
if(isset($conf[‘chq_redis_cache_enabled’]) &&
$conf[‘chq_redis_cache_enabled’] {
$settings[‘redis connection’][‘interface’] = ‘PhpRedis’;
$settings[‘cache’][‘default’] = ‘cache.backend.redis’;
$conf[‘cache_class_cache’] = ‘Redis_Cache’;
}

Optional Installation

  • phpRedisAdmin

A user can also use the web interface for controlling the Redis server instead of using the CLI. 

To do so, navigate to http://github.com/ErikDubbelboer/phpRedisAdmin. Download the files, move them to the root folder and configure the configure.inc.php file.

So, this is how, we can configure Drupal to use Redis bins for caching and Redis being faster than the default caching technique, is a very viable option.

For any further queries, feel free to leave your queries in the comments section below.

Subscribe

Ready to start your digital transformation journey with us?

Related Blogs

SDC: Integrating Storybook & Single Directory Component

Integrating SDC With Storybook OpenSense Labs

Today, we will talk about about Drupal Single Directory Components or SDC and Storybook. Single Directory Components in Drupal allows you…

RFP: How To Create An RFP For Open Source Solutions?

RFP OpenSense Labs

A good Request for Proposals (RFP) or Call for Proposals (CFP) clearly states the goals and expectations of your project and sets the…

Drupal 7 End Of Life: Top Reasons You Should Migrate To Drupal 10

Drupal 7 End Of Life Cover Image OpenSense Labs

Drupal 10 was released in December 2022 and ever since, the community has been pushing its users to do Drupal 7 to 10 migration. As per…