You are here

function cache_example_form_create_expiring_item in Examples for Developers 7

Submit handler to create a new cache item with specified expiration.

Related topics

1 string reference to 'cache_example_form_create_expiring_item'
cache_example_page_form in cache_example/cache_example.module
Main page for cache_example.

File

cache_example/cache_example.module, line 200
Outlines how a module can use the Cache API.

Code

function cache_example_form_create_expiring_item($form, &$form_state) {
  $interval = $form_state['values']['expiration'];
  if ($interval == 'never_remove') {
    $expiration = CACHE_PERMANENT;
    $expiration_friendly = t('Never expires');
  }
  else {
    $expiration = time() + $interval;
    $expiration_friendly = format_date($expiration);
  }

  // Set the expiration to the actual Unix timestamp of the end of the required
  // interval.
  cache_set('cache_example_expiring_item', $expiration_friendly, 'cache', $expiration);
  drupal_set_message(t('cache_example_expiring_item was set to expire at %time', array(
    '%time' => $expiration_friendly,
  )));
}