Drupal entity cache
2013 04 Dec

Drupal Entity Cache

By Kuldeep Singh Pal

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 ownEntityCacheDefaultEntityController 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.

Featured blog

web-personalization

Personalized Content is a Proven Entity !!

Irrespective of how big a business icon or brand you are, increasing the relevance of your website will always be critical to your success.

Read More

Git Hooks

Git hooks for better codes

We are programmers and we are always on the lookout for ways to improve our code. A good and structured way of coding defines the completeness of a programmer.

Read More

Drupal ,varnish cache

Hard time with Drupal, Varnish Cache and Cookies

Using a reverse proxy server in front of a web server is usually needed for every big site and it is a very good thing to do so as reverse proxy server will handle all the anonymous traff

Read More

Say no to captcha

Say no to captcha - Various Spam Protection Methods

Maintaining high traffic websites have their own merits and demerits, the most annoying thing about them is SPAM.

Read More