Drupal entity cache
2013 04 Dec

Drupal Entity Cache

Drupal Entity Cache Architecture: In Drupal 7, concepts of entities and fields were introduced. For loading entities and fields from database, every access became a very heavy task. Thus caching becomes utmost important. 

Drupal 7 provides two main mechanisms for caching:

  1. Drupal Static cache

  2. Drupal Database cache

Drupal Static Cache: It stores the data in php variable with static scopeDrupal 7 uses drupal_static()function to do so. The function returns an empty value the first time we call it, but any changes to the variable later will be preserved. This would mean that any function can check if the variable is already populated, and return the value immediately without doing any further computation.

Note: Static variables persist only in single page request. So for subsequent requests, the static variable needs to be calculated the first time again. 

Drupal Database Cache: So for persisting data between page requests, Drupal use database cache. It stores the computed data [by complex logic like joining many DB table or performing time consuming calculation] into single table in binary format.

For this drupal 7 api provides cache_set(), cache_get(), cache_clear_all(). Menu cache, block cache, page cache, etc use these functions.

For basic understanding of caching read this blog.

Entities uses Drupal static cache and Field uses Drupal database cache.

Loading of Entities: Drupal 7 api provides entity_load function for this. Calling entity_load() for a particular entity ID requires complex database queries for the first time, but the resulting information is kept in a static variable for the remaining page load. That way, calling entity_load more than ten times in single page request, doesn't require ten full sets of queries to the database. It get data from static variable after the first time. 

Loading of Fields: Fields are attached with entity, so these are loaded with calls to entity_load, from various tables by performing sql-joins. When fields are loaded, they are also stored in  ‘field_cache’ table. So on next call of entity_load, it will fetch the fields from  drupal’s cache table ‘field_cache’ which reduces the load on database and improves response time.

But drupal 7 does not provide database cache mechanism for entities. 

To our delight here comes entitycache module.

Entity cache contrib module: This module replaces DrupalDefaultEntityController system with own EntityCacheDefaultEntityController system by altering entity info, disabling drupal field api cache and storing the fields in its own cache table.

function entitycache_entity_info_alter(&$entity_info) {

 foreach (entitycache_supported_core_entities(TRUE) as $type => $controller) {

     $entity_info[$type]['field cache'] = FALSE;

     $entity_info[$type]['entity cache'] = TRUE;

   $entity_info[$type]['controller class'] = $controller;

 }

}

This module uses Drupal database cache system and is thus persistent across different page requests for different users. It creates separate table for each core entity type with prefix entity_cache, like

  • Node => entity_cache_node
  • User => entity_cache_user

So cache data will persist until we don’t flush cache.

Now if we are using entity_load function in our custom code for some functionality, then Entities should come from cache instead of rebuilding them for e.g. 

  • In one page request we want to show list of entities

  • In other page request we want to alter, or filter these entities

For testing:

I have created 1000 dummy users. Timing of loading nodes: 

  • First time => 72 second
  • Then=> 2 second

with Entitycache module

  • first time => 141 second
  • Then => 595 milisecond

 

For more statistics click here.

But if we are retrieving data dusing views, then there is no need of this module, as views already has its own caching system.

Conclusion: In Drupal Default cache, entity_load() functions use Drupal static cache mechanism not database cache mechanism. So values persist in single page request.

But if we want entities to persist between multiple page requests, we can use entitycache module.

Latest Blogs

Drupal 9 Upgrade

The Roadmap to upgrade Drupal site to Drupal 9

Drupal is approaching with its next big evolution and the upcoming release that is none other than Drupal 9.

Read More

How to Choose Web Development Agencies

Points to check before choosing the right website development company

In today’s day and age, where the digital population is increasing exponentially, no credible business can or should refrain from having a strong and steady web presence.

Read More

Wordpress to Drupal

Should I plan to move my Wordpress Website into the Latest Drupal version?

If you're here, I assume you already have a website built on WordPress but you're not satisfied with it, or perhaps you're here to have a clear idea about why you should give a thought to migrating

Read More

Drupal 9 Easy Upgrade

Why is Drupal 9 going to be an easy upgrade compared to the previous major versions?

Lately, there has been a lot of buzz around the corner about Drupal 9. The Drupal community is launching Drupal 9, and as well as have plans for the EOL of Drupal 7 and Drupal 8 by 2021.

Read More