Submitting a form rebuilds (expensive) page upon submit I have a simple menu callback that draws a page with a form on it. The page callback does a very expensive operation (requests a bunch of data from an external API).

function test_menu() {
  $items = array();
  $items['test'] = array(
    'title' =&gt; 'Test',
    'page callback' =&gt; 'test_page_handler',
    'access callback' =&gt; TRUE,
  );
  return $items;
}

function test_page_handler() {
  $data = very_expensive_data_operation();
  $output = drupal_get_form('test_simple_form', $data);
  return $output;
}

function test_simple_form(&amp;$form_state, $data) {
  $form['create'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Submit'),
  );
  return $form;
}

function test_simple_form_submit(&amp;$form, &amp;$form_state) {
  // do something
}


The problem I am seeing is that when the form is submitted, Drupal will re-execute the page callback in the background, resulting in the very_expensive_data_operation() running again.

There are probably lots of great reasons for Drupal to do this, but how can I change my app so that the very_expensive_data_operation() doesn't have to run again when submitting the form (after all, that's why I'm passing $data to the form, rather than having the form run it directly.)

One workaround I can think of is to toss $data into a cache table, but it feels like I'm missing something that available to me in the Drupal+FAPI api.
You can also try using drupal_static


  Provides central static variable storage.
  
  All functions requiring a static variable to persist or cache data
  within a single page request are encouraged to use this function
  unless it is absolutely certain that the static variable will not need
  to be reset during the page request. By centralizing static variable
  storage through this function, other functions can rely on a
  consistent API for resetting any other function's static variables.


function test_menu() {
  $items = array();
  $items['test'] = array(
    'title' =&gt; 'Test',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('test_simple_form'),
    'access callback' =&gt; TRUE,
  );
  return $items;
}

function expensive_data() {
  $data = &amp;drupal_static(__FUNCTION__);
  if (!isset($data)) {
    $data = very_expensive_data_operation();
  }
  return $data;
}

function test_simple_form(&amp;$form_state) {
  $data = expensive_data();
  $form['create'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Submit'),
  );
  return $form;
}

function test_simple_form_submit(&amp;$form, &amp;$form_state) {
  // do something
}


You might notice that the static variable technique only stores data for the duration of a single page load. For even better performance try this

function expensive_data() {
  $data = &amp;drupal_static(__FUNCTION__);
  if (!isset($data)) {
    if ($cache = cache_get('my_module_data')) {
      $my_data = $cache-&gt;data;
    }
    else {
      $data = very_expensive_data_operation();
      cache_set('my_module_data', $my_data, 'cache');
    }

  }
  return $data;
}


If you want to refresh your cache from time to time you can try 

cache_set('my_module_data', $my_data, 'cache', time() + 360); 


OR you can manually rebuild your cache using

cache_clear_all('my_module', 'cache', TRUE);