cache_example.module 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * @file
  4. * Outlines how a module can use the Cache API.
  5. *
  6. * @todo: Demonstrate cache expiration.
  7. */
  8. /**
  9. * @defgroup cache_example Example: Cache API
  10. * @ingroup examples
  11. * @{
  12. * Outlines how a module can use the Cache API.
  13. *
  14. * Cache API allows us to cache data that is heavy to calculate. As this can
  15. * significantly speed up the Drupal site, it is recommended to use cache
  16. * mechanism when it is appropriate.
  17. *
  18. * Cache in Drupal is very easy to use. This example will search entire Drupal
  19. * folder and display all files. Since this operation includes filesystem it can
  20. * take a while. This list will not change much on production
  21. * websites, so we decide to cache it.
  22. *
  23. * @see cache_get()
  24. * @see cache_set()
  25. * @see cache_clear_all()
  26. */
  27. /**
  28. * Implements hook_menu().
  29. */
  30. function cache_example_menu() {
  31. $items = array();
  32. $items['examples/cache_example'] = array(
  33. 'title' => 'Cache example',
  34. 'description' => 'Example of Drupal Cache API',
  35. 'page callback' => 'drupal_get_form',
  36. 'page arguments' => array('cache_example_page_form'),
  37. 'access callback' => TRUE,
  38. );
  39. return $items;
  40. }
  41. /**
  42. * Main page for cache_example.
  43. *
  44. * Displays a page/form which outlines how Drupal's cache works.
  45. */
  46. function cache_example_page_form($form, &$form_state) {
  47. // Log execution time.
  48. $start_time = microtime(TRUE);
  49. // Try to load the files count from cache. This function will accept two
  50. // arguments:
  51. // - cache object name (cid)
  52. // - cache bin, the (optional) cache bin (most often a database table) where
  53. // the object is to be saved.
  54. //
  55. // cache_get() returns the cached object or FALSE if object does not exist.
  56. if ($cache = cache_get('cache_example_files_count')) {
  57. /*
  58. * Get cached data. Complex data types will be unserialized automatically.
  59. */
  60. $files_count = $cache->data;
  61. }
  62. else {
  63. // If there was no cached data available we have to search filesystem.
  64. // Recursively get all files from Drupal's folder.
  65. $files_count = count(file_scan_directory('.', '/.*/'));
  66. // Since we have recalculated, we now need to store the new data into cache.
  67. // Complex data types will be automatically serialized before being saved
  68. // into cache.
  69. // Here we use the default setting and create an unexpiring cache item.
  70. // See below for an example that creates an expiring cache item.
  71. cache_set('cache_example_files_count', $files_count);
  72. }
  73. $end_time = microtime(TRUE);
  74. $duration = $end_time - $start_time;
  75. // Format intro message.
  76. $intro_message = '<p>' . t('This example will search the entire drupal folder and display a count of the files in it.') . ' ';
  77. $intro_message .= t('This can take a while, since there are a lot of files to be searched.') . ' ';
  78. $intro_message .= t('We will search filesystem just once and save output to the cache. We will use cached data for later requests.') . '</p>';
  79. $intro_message .= '<p>' . t('<a href="@url">Reload this page</a> to see cache in action.', array('@url' => request_uri())) . ' ';
  80. $intro_message .= t('You can use the button below to remove cached data.') . '</p>';
  81. $form['file_search'] = array(
  82. '#type' => 'fieldset',
  83. '#title' => t('File search caching'),
  84. );
  85. $form['file_search']['introduction'] = array(
  86. '#markup' => $intro_message,
  87. );
  88. $color = empty($cache) ? 'red' : 'green';
  89. $retrieval = empty($cache) ? t('calculated by traversing the filesystem') : t('retrieved from cache');
  90. $form['file_search']['statistics'] = array(
  91. '#type' => 'item',
  92. '#markup' => t('%count files exist in this Drupal installation; @retrieval in @time ms. <br/>(Source: <span style="color:@color;">@source</span>)',
  93. array(
  94. '%count' => $files_count,
  95. '@retrieval' => $retrieval,
  96. '@time' => number_format($duration * 1000, 2),
  97. '@color' => $color,
  98. '@source' => empty($cache) ? t('actual file search') : t('cached'),
  99. )
  100. ),
  101. );
  102. $form['file_search']['remove_file_count'] = array(
  103. '#type' => 'submit',
  104. '#submit' => array('cache_example_form_expire_files'),
  105. '#value' => t('Explicitly remove cached file count'),
  106. );
  107. $form['expiration_demo'] = array(
  108. '#type' => 'fieldset',
  109. '#title' => t('Cache expiration settings'),
  110. );
  111. $form['expiration_demo']['explanation'] = array(
  112. '#markup' => t('A cache item can be set as CACHE_PERMANENT, meaning that it will only be removed when explicitly cleared, or it can have an expiration time (a Unix timestamp).'),
  113. );
  114. $expiring_item = cache_get('cache_example_expiring_item');
  115. $item_status = $expiring_item ?
  116. t('Cache item exists and is set to expire at %time', array('%time' => $expiring_item->data)) :
  117. t('Cache item does not exist');
  118. $form['expiration_demo']['current_status'] = array(
  119. '#type' => 'item',
  120. '#title' => t('Current status of cache item "cache_example_expiring_item"'),
  121. '#markup' => $item_status,
  122. );
  123. $form['expiration_demo']['expiration'] = array(
  124. '#type' => 'select',
  125. '#title' => t('Time before cache expiration'),
  126. '#options' => array(
  127. 'never_remove' => t('CACHE_PERMANENT'),
  128. -10 => t('Immediate expiration'),
  129. 10 => t('10 seconds from form submission'),
  130. 60 => t('1 minute from form submission'),
  131. 300 => t('5 minutes from form submission'),
  132. ),
  133. '#default_value' => -10,
  134. '#description' => t('Any cache item can be set to only expire when explicitly cleared, or to expire at a given time.'),
  135. );
  136. $form['expiration_demo']['create_cache_item'] = array(
  137. '#type' => 'submit',
  138. '#value' => t('Create a cache item with this expiration'),
  139. '#submit' => array('cache_example_form_create_expiring_item'),
  140. );
  141. $form['cache_clearing'] = array(
  142. '#type' => 'fieldset',
  143. '#title' => t('Expire and remove options'),
  144. '#description' => t("We have APIs to expire cached items and also to just remove them. Unfortunately, they're all the same API, cache_clear_all"),
  145. );
  146. $form['cache_clearing']['cache_clear_type'] = array(
  147. '#type' => 'radios',
  148. '#title' => t('Type of cache clearing to do'),
  149. '#options' => array(
  150. 'expire' => t('Remove items from the "cache" bin that have expired'),
  151. 'remove_all' => t('Remove all items from the "cache" bin regardless of expiration (super-wildcard)'),
  152. 'remove_wildcard' => t('Remove all items from the "cache" bin that match the pattern "cache_example"'),
  153. ),
  154. '#default_value' => 'expire',
  155. );
  156. // Submit button to clear cached data.
  157. $form['cache_clearing']['clear_expired'] = array(
  158. '#type' => 'submit',
  159. '#value' => t('Clear or expire cache'),
  160. '#submit' => array('cache_example_form_cache_clearing'),
  161. '#access' => user_access('administer site configuration'),
  162. );
  163. return $form;
  164. }
  165. /**
  166. * Submit handler that explicitly clears cache_example_files_count from cache.
  167. */
  168. function cache_example_form_expire_files($form, &$form_state) {
  169. // Clear cached data. This function will delete cached object from cache bin.
  170. //
  171. // The first argument is cache id to be deleted. Since we've provided it
  172. // explicitly, it will be removed whether or not it has an associated
  173. // expiration time. The second argument (required here) is the cache bin.
  174. // Using cache_clear_all() explicitly in this way
  175. // forces removal of the cached item.
  176. cache_clear_all('cache_example_files_count', 'cache');
  177. // Display message to the user.
  178. drupal_set_message(t('Cached data key "cache_example_files_count" was cleared.'), 'status');
  179. }
  180. /**
  181. * Submit handler to create a new cache item with specified expiration.
  182. */
  183. function cache_example_form_create_expiring_item($form, &$form_state) {
  184. $interval = $form_state['values']['expiration'];
  185. if ($interval == 'never_remove') {
  186. $expiration = CACHE_PERMANENT;
  187. $expiration_friendly = t('Never expires');
  188. }
  189. else {
  190. $expiration = time() + $interval;
  191. $expiration_friendly = format_date($expiration);
  192. }
  193. // Set the expiration to the actual Unix timestamp of the end of the required
  194. // interval.
  195. cache_set('cache_example_expiring_item', $expiration_friendly, 'cache', $expiration);
  196. drupal_set_message(t('cache_example_expiring_item was set to expire at %time', array('%time' => $expiration_friendly)));
  197. }
  198. /**
  199. * Submit handler to demonstrate the various uses of cache_clear_all().
  200. */
  201. function cache_example_form_cache_clearing($form, &$form_state) {
  202. switch ($form_state['values']['cache_clear_type']) {
  203. case 'expire':
  204. // Here we'll remove all cache keys in the 'cache' bin that have expired.
  205. cache_clear_all(NULL, 'cache');
  206. drupal_set_message(t('cache_clear_all(NULL, "cache") was called, removing any expired cache items.'));
  207. break;
  208. case 'remove_all':
  209. // This removes all keys in a bin using a super-wildcard. This
  210. // has nothing to do with expiration. It's just brute-force removal.
  211. cache_clear_all('*', 'cache', TRUE);
  212. drupal_set_message(t('ALL entries in the "cache" bin were removed with cache_clear_all("*", "cache", TRUE).'));
  213. break;
  214. case 'remove_wildcard':
  215. // We can also explicitly remove all cache items whose cid begins with
  216. // 'cache_example' by using a wildcard. This again is brute-force
  217. // removal, not expiration.
  218. cache_clear_all('cache_example', 'cache', TRUE);
  219. drupal_set_message(t('Cache entries whose cid began with "cache_example" in the "cache" bin were removed with cache_clear_all("cache_example", "cache", TRUE).'));
  220. break;
  221. }
  222. }
  223. /**
  224. * @} End of "defgroup cache_example".
  225. */