plugin_cache.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_results_key().
  70. *
  71. * Use the Search API query as the main source for the key.
  72. */
  73. public function get_results_key() {
  74. global $user;
  75. if (!isset($this->_results_key)) {
  76. $query = $this->getSearchApiQuery();
  77. $query->preExecute();
  78. $key_data = array(
  79. 'query' => $query,
  80. 'roles' => array_keys($user->roles),
  81. 'super-user' => $user->uid == 1, // special caching for super user.
  82. 'language' => $GLOBALS['language']->language,
  83. 'base_url' => $GLOBALS['base_url'],
  84. );
  85. // Not sure what gets passed in exposed_info, so better include it. All
  86. // other parameters used in the parent method are already reflected in the
  87. // Search API query object we use.
  88. if (isset($_GET['exposed_info'])) {
  89. $key_data['exposed_info'] = $_GET['exposed_info'];
  90. }
  91. $this->_results_key = $this->view->name . ':' . $this->display->id . ':results:' . md5(serialize($key_data));
  92. }
  93. return $this->_results_key;
  94. }
  95. /**
  96. * Get the Search API query object associated with the current view.
  97. *
  98. * @return SearchApiQueryInterface|null
  99. * The Search API query object associated with the current view; or NULL if
  100. * there is none.
  101. */
  102. protected function getSearchApiQuery() {
  103. if (!isset($this->search_api_query)) {
  104. $this->search_api_query = FALSE;
  105. if (isset($this->view->query) && $this->view->query instanceof SearchApiViewsQuery) {
  106. $this->search_api_query = $this->view->query->getSearchApiQuery();
  107. }
  108. }
  109. return $this->search_api_query ? $this->search_api_query : NULL;
  110. }
  111. }