2

I have implemented custom entites, extending on Entity API. This is essentially identical to what the Model module does with a few extra customisations (fields, solr/facet integration etc).

I want to cache my custom entities using the Entity Cache module to get fast loading of entities when using Views and Apachesolr along with automatic cache updating when my entities are updated.

What do I have to do to implement this?

4

You can achieve this quite easily with a few hooks and additions to your existing hooks.

Define the cache table in your hook_schema() implementation in your module's .install file:

// Optional cache table for entitycache support.
$schema['cache_entity_my_entity'] = drupal_get_schema_unprocessed('system', 'cache');
$schema['cache_entity_my_entity']['description'] = 'Cache table used to store my_entity entity records.';

This adds a db table that can be used to store your cached entities.

Add an update hook to create the table:

function my_entity_update_700X() {
  if (!db_table_exists('cache_entity_my_entity')) {
    $schema = drupal_get_schema_unprocessed('system', 'cache');
    $schema['description'] = 'Cache table used to store my_entity entity records.';
    db_create_table('cache_entity_my_entity', $schema);
  }
}

Enable entitycache in hook_entity_info(). Add this near the bottom before return $info;

if (module_exists('entitycache')) {
  $info['my_entity']['field cache'] = FALSE;
  $info['my_entity']['entity cache'] = TRUE;
}

Implement hook_flush_caches() in your .module file:

/**
 * Implements hook_flush_caches().
 */
function my_entity_flush_caches() {
  return array('cache_entity_my_entity');
}

And that's it! Entity API takes care of the interactions and things like load() and resetCache() (I found you have to implement those functions when adding entitycache functionality to non-entityapi entities)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.