1 cache_example.module cache_example_form_cache_clearing($form, &$form_state)

Submit handler to demonstrate the various methods for clearing cache.

Related topics

File

modules/examples/cache_example/cache_example.module, line 227
Outlines how a module can use the Cache API.

Code

function cache_example_form_cache_clearing($form, &$form_state) {
  switch ($form_state['values']['cache_clear_type']) {
    case 'expire':
      // Here we'll remove all cache keys in the 'cache' bin that have expired.
      // An alternative syntax is to use the shortcut: cache_clear_all(NULL, 'cache', NULL);
      cache('cache')->garbageCollection();
      backdrop_set_message(t('Expired cache items were removed.'));
      break;

    case 'remove_all':
      // This removes all keys in a bin. This has nothing to do with expiration.
      // It's just brute-force removal.
      // An alternative syntax is to use the shortcut: cache_flush('cache');
      cache('cache')->flush();
      backdrop_set_message(t('ALL entries in the "cache" bin were removed.'));
      break;

    case 'remove_wildcard':
      // We can also explicitly remove all cache items whose cid begins with
      // 'cache_example' by using a wildcard. This again is brute-force
      // removal, not expiration.
      // An alternative syntax is to use the shortcut: cache_clear_all('cache_example', 'cache', TRUE);
      cache('cache')->deletePrefix('cache_example');
      backdrop_set_message(t('Cache entries whose cid began with "cache_example" in the "cache" bin were removed.'));
      break;
  }
}