SHARE
TWEET

Drush Script to cache entities with EntityCache module

btmash Jul 11th, 2011 245 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * @file entitycache.drush.inc
  5.  *   drush integration for entitycache.
  6.  *   Simple idea - load each of the entities.
  7.  *   @TODO: Figure out how to do it in batches.
  8.  *   @TODO: Figure out how to only load entities not yet in cache.
  9.  *   @TODO: Figure out a way to have it done via a scheduler.
  10.  */
  11.  
  12. /**
  13.  * Implementation of hook_drush_help().
  14.  */
  15. function entitycache_drush_help($section) {
  16.   switch ($section) {
  17.     case 'drush:entity-load-cache':
  18.       return dt('Used without parameters, this command loads all the various objects in the entities into cache using entity_load()');
  19.   }
  20. }
  21.  
  22. /**
  23.  * Implementation of hook_drush_command().
  24.  */
  25. function entitycache_drush_command() {
  26.   $items = array();
  27.   $items['entitycache-load-cache'] = array(
  28.     'callback' => 'drush_entitycache_load_cache',
  29.     'description' => t('Load the cache with the various entities configured to use the cache'),
  30.     'arguments' => array(
  31.       'type' => dt('Optional. Only load the particular entity type objects into cache'),
  32.     ),
  33.     'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  34.     'aliases' => array('elc'),
  35.   );
  36.  
  37.   return $items;
  38. }
  39.  
  40. /**
  41.  * Load the cache bin with content from the various entities.
  42.  * @param $type Optional. The specific type of entity that should
  43.  *   get it's content cached.
  44.  */
  45. function drush_entitycache_load_cache($type = '') {
  46.   $types = entitycache_supported_core_entities(TRUE);
  47.   $start = time();
  48.   if (!empty($type)) {
  49.     if (!isset($types[$type])) {
  50.       drush_die("Type $type is not supported");
  51.     }
  52.     else {
  53.       _drush_entitycache_load_cache($type);
  54.     }
  55.   }
  56.   else {
  57.     foreach ($types as $entity_type => $entity_controller) {
  58.       _drush_entitycache_load_cache($entity_type);
  59.     }
  60.   }
  61.   drush_print("Caching complete!");
  62.   drush_print("Total processing time: ". (time() - $start) ." seconds.");
  63. }
  64.  
  65. /**
  66.  * Load the cache bin with content from a specific type of entity.
  67.  */
  68. function _drush_entitycache_load_cache($type) {
  69.   $max_limit = 50;
  70.   drush_print("Begin caching $type");
  71.   $query = new EntityFieldQuery;
  72.   $result = $query
  73.     ->entityCondition('entity_type', $type)
  74.     ->execute();
  75.    
  76.   $keys = array();
  77.   $limit = 0;
  78.   foreach ($result[$type] as $entity_key => $entity_info) {
  79.     $keys[] = $entity_key;
  80.     $limit++;
  81.     if ($limit >= $max_limit) {
  82.       entity_load($type, $keys, array(), TRUE);
  83.       $keys = array();
  84.       $limit = 0;
  85.     }
  86.   }
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top