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:
Drupal Static cache
Drupal Database cache
Drupal Static Cache: It stores the data in php variable with static scope. Drupal 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.