1 form.inc form_set_cache($form_build_id, $form, $form_state)

Stores a form in the cache.

The default cache_form expiration is 6 hours. On busy sites, the cache_form table can become very large. A shorter cache lifetime can help to keep the table's size under control.

Related topics


core/includes/form.inc, line 564
Functions for form and batch generation and processing.


function form_set_cache($form_build_id, $form, $form_state) {
  $site_config = config('system.core');

  // Try to read the 'form_cache_expiration' setting from settings.php. If not
  // set in settings.php, then default to the value set in 'system.core' config
  // (via system_update_1067 - setting not exposed in the admin UI currently).
  $expire = settings_get('form_cache_expiration', $site_config->get('form_cache_expiration'));

  // Ensure that the form build_id embedded in the form structure is the same as
  // the one passed in as a parameter. This is an additional safety measure to
  // prevent legacy code operating directly with form_get_cache and
  // form_set_cache from accidentally overwriting immutable form state.
  if ($form['#build_id'] != $form_build_id) {
    watchdog('form', 'Form build-id mismatch detected while attempting to store a form in the cache.', array(), WATCHDOG_ERROR);

  // Note that form caches must not be affected by cache clears, so we use the
  // tempstore to ensure persistent values between clears.
  if (isset($form)) {
    if ($GLOBALS['user']->uid) {
      $form['#cache_token'] = backdrop_get_token();
    tempstore_set('form', $form_build_id, $form, REQUEST_TIME + $expire);

  // Cache form state.
  if ($site_config->get('cache') && backdrop_page_is_cacheable()) {
    $form_state['build_info']['immutable'] = TRUE;
  if ($data = array_diff_key($form_state, array_flip(form_state_keys_no_cache()))) {
    tempstore_set('form_state', $form_build_id, $data, REQUEST_TIME + $expire);