plugin_cache.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * @file
  4. * Contains the SearchApiViewsCache class.
  5. */
  6. /**
  7. * Plugin class for caching Search API views.
  8. */
  9. class SearchApiViewsCache extends views_plugin_cache_time {
  10. /**
  11. * Static cache for get_results_key().
  12. *
  13. * @var string
  14. */
  15. protected $_results_key = NULL;
  16. /**
  17. * Static cache for getSearchApiQuery().
  18. *
  19. * @var SearchApiQueryInterface
  20. */
  21. protected $search_api_query = NULL;
  22. /**
  23. * Overrides views_plugin_cache::cache_set().
  24. *
  25. * Also stores Search API's internal search results.
  26. */
  27. public function cache_set($type) {
  28. if ($type != 'results') {
  29. return parent::cache_set($type);
  30. }
  31. $cid = $this->get_results_key();
  32. $results = NULL;
  33. $query_plugin = $this->view->query;
  34. if ($query_plugin instanceof SearchApiViewsQuery) {
  35. $results = $query_plugin->getSearchApiResults();
  36. }
  37. $data = array(
  38. 'result' => $this->view->result,
  39. 'total_rows' => isset($this->view->total_rows) ? $this->view->total_rows : 0,
  40. 'current_page' => $this->view->get_current_page(),
  41. 'search_api results' => $results,
  42. );
  43. cache_set($cid, $data, $this->table, $this->cache_set_expire($type));
  44. }
  45. /**
  46. * Overrides views_plugin_cache::cache_get().
  47. *
  48. * Additionally stores successfully retrieved results with
  49. * search_api_current_search().
  50. */
  51. public function cache_get($type) {
  52. if ($type != 'results') {
  53. return parent::cache_get($type);
  54. }
  55. // Values to set: $view->result, $view->total_rows, $view->execute_time,
  56. // $view->current_page.
  57. if ($cache = cache_get($this->get_results_key(), $this->table)) {
  58. $cutoff = $this->cache_expire($type);
  59. if (!$cutoff || $cache->created > $cutoff) {
  60. $this->view->result = $cache->data['result'];
  61. $this->view->total_rows = $cache->data['total_rows'];
  62. $this->view->set_current_page($cache->data['current_page']);
  63. $this->view->execute_time = 0;
  64. // Trick Search API into believing a search happened, to make facetting
  65. // et al. work.
  66. $query = $this->getSearchApiQuery();
  67. search_api_current_search($query->getOption('search id'), $query, $cache->data['search_api results']);
  68. return TRUE;
  69. }
  70. }
  71. return FALSE;
  72. }
  73. /**
  74. * Overrides views_plugin_cache::get_cache_key().
  75. *
  76. * Use the Search API query as the main source for the key. Note that in
  77. * Views < 3.8, this method does not exist.
  78. */
  79. public function get_cache_key($key_data = array()) {
  80. global $user;
  81. if (!isset($this->_results_key)) {
  82. $query = $this->getSearchApiQuery();
  83. $query->preExecute();
  84. $key_data += array(
  85. 'query' => $query,
  86. 'roles' => array_keys($user->roles),
  87. 'super-user' => $user->uid == 1, // special caching for super user.
  88. 'language' => $GLOBALS['language']->language,
  89. 'base_url' => $GLOBALS['base_url'],
  90. 'offset' => $this->view->get_current_page() . '*' . $this->view->get_items_per_page() . '+' . $this->view->get_offset(),
  91. );
  92. // Not sure what gets passed in exposed_info, so better include it. All
  93. // other parameters used in the parent method are already reflected in the
  94. // Search API query object we use.
  95. if (isset($_GET['exposed_info'])) {
  96. $key_data['exposed_info'] = $_GET['exposed_info'];
  97. }
  98. }
  99. $key = drupal_hash_base64(serialize($key_data));
  100. return $key;
  101. }
  102. /**
  103. * Overrides views_plugin_cache::get_results_key().
  104. *
  105. * This is unnecessary for Views >= 3.8.
  106. */
  107. public function get_results_key() {
  108. if (!isset($this->_results_key)) {
  109. $this->_results_key = $this->view->name . ':' . $this->display->id . ':results:' . $this->get_cache_key();
  110. }
  111. return $this->_results_key;
  112. }
  113. /**
  114. * Retrieves the Search API query object associated with the current view.
  115. *
  116. * @return SearchApiQueryInterface|null
  117. * The Search API query object associated with the current view; or NULL if
  118. * there is none.
  119. */
  120. protected function getSearchApiQuery() {
  121. if (!isset($this->search_api_query)) {
  122. $this->search_api_query = FALSE;
  123. if (isset($this->view->query) && $this->view->query instanceof SearchApiViewsQuery) {
  124. $this->search_api_query = $this->view->query->getSearchApiQuery();
  125. }
  126. }
  127. return $this->search_api_query ? $this->search_api_query : NULL;
  128. }
  129. }