plugin_cache.inc 4.2 KB

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