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)