Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Test module for Drupal which get a page.
  6.  */
  7.  
  8. /**
  9.  * Implements hook_menu().
  10.  */
  11. function pv_test_cache_menu() {
  12.   $items = array();
  13.   $items['testcache'] = array(
  14.     'title' => 'Test cache',
  15.     'description' => 'Test Cache',
  16.     'page callback' => 'pv_test_cache_content',
  17.     'access callback' => TRUE,
  18.     'type' => MENU_NORMAL_ITEM,
  19.     'menu_name' => 'main-menu',
  20.   );
  21.   return $items;
  22. }
  23.  
  24. /**
  25.  * Provide data to template.
  26.  */
  27. function pv_test_cache_content() {
  28.   $content = "<h1> User First Name: " . pv_test_cache_check_cache() . "</h1>";
  29.   return $content;
  30. }
  31.  
  32. /**
  33.  * Function which look at cache.
  34.  */
  35. function pv_test_cache_check_cache() {
  36.   $user_first_name_from_cache = cache_get('pv_test_cache');
  37.   $user_first_name_from_db = get_user_first_name();
  38.   if ($user_first_name_from_cache && $user_first_name_from_cache->data == $user_first_name_from_db) {
  39.     return $user_first_name_from_cache->data;
  40.   }
  41.   else {
  42.     cache_clear_all('pv_test_cache', 'cache', TRUE);
  43.     cache_set('pv_test_cache', $user_first_name_from_db, 'cache');
  44.     return $user_first_name_from_db;
  45.   }
  46. }
  47.  
  48. /**
  49.  * Function which take User First Name from db.
  50.  */
  51. function get_user_first_name() {
  52.   global $user;
  53.   $query = db_select('field_data_field_account_first_name', 'fdfafn');
  54.   $query->fields('fdfafn', array('field_account_first_name_value'));
  55.   $query->condition('fdfafn.entity_id', $user->uid);
  56.   $first_name = $query->execute()->fetchAll();
  57.   return $first_name[0]->field_account_first_name_value;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement