- <?php
- * @file
- * Hooks implementation for the Cache Example module.
- *
- * @todo: Demonstrate cache expiration.
- */
-
- * @defgroup cache_example Example: Cache API
- * @ingroup examples
- * @{
- * This example demonstrates how a module can use the Cache API.
- *
- * Cache API allows us to cache data that is heavy to calculate. As this can
- * significantly speed up the Backdrop site, it is recommended to use cache
- * mechanism when it is appropriate.
- *
- * Cache in Backdrop is very easy to use. This example will search the entire
- * Backdrop folder and display all files. Since this operation involves the
- * filesystem, it can take a while. This list will not change much on production
- * websites, so we decided to cache it.
- *
- * @see cache()
- * @see cache_get()
- * @see cache_set()
- * @see cache_clear_all()
- */
-
- * Implements hook_menu().
- */
- function cache_example_menu() {
- $items = array();
- $items['examples/cache_example'] = array(
- 'title' => 'Cache example',
- 'description' => 'Example of Backdrop Cache API',
- 'page callback' => 'backdrop_get_form',
- 'page arguments' => array('cache_example_page_form'),
- 'access callback' => TRUE,
- );
-
- return $items;
- }
-
- * Main page for cache_example.
- *
- * Displays a page/form which outlines how Backdrop's cache works.
- */
- function cache_example_page_form($form, &$form_state) {
-
- $start_time = microtime(TRUE);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if ($cache = cache('cache')->get('cache_example_files_count')) {
-
- * Get cached data. Complex data types will be unserialized automatically.
- */
- $files_count = $cache->data;
- }
- else {
-
-
- $files_count = count(file_scan_directory('.', '/.*/'));
-
-
-
-
-
-
-
- cache('cache')->set('cache_example_files_count', $files_count);
- }
-
- $end_time = microtime(TRUE);
- $duration = $end_time - $start_time;
-
-
- $intro_message = '<p>' . t('This example will search the entire Backdrop folder and display a count of the files in it.') . ' ';
- $intro_message .= t('This can take a while, since there are a lot of files to be searched.') . ' ';
- $intro_message .= t('We will search filesystem just once and save output to the cache. We will use cached data for later requests.') . '</p>';
- $intro_message .= '<p>' . t('<a href="@url">Reload this page</a> to see cache in action.', array('@url' => request_uri())) . ' ';
- $intro_message .= t('You can use the button below to remove cached data.') . '</p>';
-
- $form['file_search'] = array(
- '#type' => 'fieldset',
- '#title' => t('File search caching'),
- );
- $form['file_search']['introduction'] = array(
- '#markup' => $intro_message,
- );
-
- $color = empty($cache) ? 'red' : 'green';
- $retrieval = empty($cache) ? t('calculated by traversing the filesystem') : t('retrieved from cache');
-
- $form['file_search']['statistics'] = array(
- '#type' => 'item',
- '#markup' => t('%count files exist in this Backdrop installation; @retrieval in @time ms. <br/>(Source: <span style="color:@color;">@source</span>)',
- array(
- '%count' => $files_count,
- '@retrieval' => $retrieval,
- '@time' => number_format($duration * 1000, 2),
- '@color' => $color,
- '@source' => empty($cache) ? t('actual file search') : t('cached'),
- )
- ),
- );
- $form['file_search']['remove_file_count'] = array(
- '#type' => 'submit',
- '#submit' => array('cache_example_form_expire_files'),
- '#value' => t('Explicitly remove cached file count'),
- );
-
- $form['expiration_demo'] = array(
- '#type' => 'fieldset',
- '#title' => t('Cache expiration settings'),
- );
- $form['expiration_demo']['explanation'] = array(
- '#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).'),
- );
- $expiring_item = cache('cache')->get('cache_example_expiring_item');
- $item_status = $expiring_item ?
- t('Cache item exists and is set to expire at %time', array('%time' => $expiring_item->data)) :
- t('Cache item does not exist');
- $form['expiration_demo']['current_status'] = array(
- '#type' => 'item',
- '#title' => t('Current status of cache item "cache_example_expiring_item"'),
- '#markup' => $item_status,
- );
- $form['expiration_demo']['expiration'] = array(
- '#type' => 'select',
- '#title' => t('Time before cache expiration'),
- '#options' => array(
- 'never_remove' => t('CACHE_PERMANENT'),
- -10 => t('Immediate expiration'),
- 10 => t('10 seconds from form submission'),
- 60 => t('1 minute from form submission'),
- 300 => t('5 minutes from form submission'),
- ),
- '#default_value' => -10,
- '#description' => t('Any cache item can be set to only expire when explicitly cleared, or to expire at a given time.'),
- );
- $form['expiration_demo']['create_cache_item'] = array(
- '#type' => 'submit',
- '#value' => t('Create a cache item with this expiration'),
- '#submit' => array('cache_example_form_create_expiring_item'),
- );
-
- $form['cache_clearing'] = array(
- '#type' => 'fieldset',
- '#title' => t('Expire and remove options'),
- '#description' => t("We have APIs to expire cached items and also to just remove them."),
- );
- $form['cache_clearing']['cache_clear_type'] = array(
- '#type' => 'radios',
- '#title' => t('Type of cache clearing to do'),
- '#options' => array(
- 'expire' => t('Remove items from the "cache" bin that have expired'),
- 'remove_all' => t('Remove all items from the "cache" bin regardless of expiration'),
- 'remove_wildcard' => t('Remove all items from the "cache" bin that match the pattern "cache_example"'),
- ),
- '#default_value' => 'expire',
- );
-
- $form['cache_clearing']['clear_expired'] = array(
- '#type' => 'submit',
- '#value' => t('Clear or expire cache'),
- '#submit' => array('cache_example_form_cache_clearing'),
- '#access' => user_access('administer site configuration'),
- );
- return $form;
- }
-
- * Submit handler that explicitly clears cache_example_files_count from cache.
- */
- function cache_example_form_expire_files($form, &$form_state) {
-
-
-
-
-
-
- cache('cache')->delete('cache_example_files_count');
-
-
- backdrop_set_message(t('Cached data key "cache_example_files_count" was cleared.'), 'status');
- }
-
- * Submit handler to create a new cache item with specified expiration.
- */
- 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);
- }
-
-
-
- cache('cache')->set('cache_example_expiring_item', $expiration_friendly, $expiration);
- backdrop_set_message(t('cache_example_expiring_item was set to expire at %time', array('%time' => $expiration_friendly)));
- }
-
- * Submit handler to demonstrate the various methods for clearing cache.
- */
- function cache_example_form_cache_clearing($form, &$form_state) {
- switch ($form_state['values']['cache_clear_type']) {
- case 'expire':
-
-
- cache('cache')->garbageCollection();
- backdrop_set_message(t('Expired cache items were removed.'));
- break;
-
- case 'remove_all':
-
-
-
- cache('cache')->flush();
- backdrop_set_message(t('ALL entries in the "cache" bin were removed.'));
- break;
-
- case 'remove_wildcard':
-
-
-
-
- cache('cache')->deletePrefix('cache_example');
- backdrop_set_message(t('Cache entries whose cid began with "cache_example" in the "cache" bin were removed.'));
- break;
- }
- }
-
- * @} End of "defgroup cache_example".
- */