views_plugin_cache_time.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_cache_time.
  5. */
  6. /**
  7. * Simple caching of query results for Views displays.
  8. *
  9. * @ingroup views_cache_plugins
  10. */
  11. class views_plugin_cache_time extends views_plugin_cache {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function option_definition() {
  16. $options = parent::option_definition();
  17. $options['results_lifespan'] = array('default' => 3600);
  18. $options['results_lifespan_custom'] = array('default' => 0);
  19. $options['output_lifespan'] = array('default' => 3600);
  20. $options['output_lifespan_custom'] = array('default' => 0);
  21. return $options;
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function options_form(&$form, &$form_state) {
  27. parent::options_form($form, $form_state);
  28. $options = array(60, 300, 1800, 3600, 21600, 518400);
  29. $options = drupal_map_assoc($options, 'format_interval');
  30. $options = array(-1 => t('Never cache')) + $options + array('custom' => t('Custom'));
  31. $form['results_lifespan'] = array(
  32. '#type' => 'select',
  33. '#title' => t('Query results'),
  34. '#description' => t('The length of time raw query results should be cached.'),
  35. '#options' => $options,
  36. '#default_value' => $this->options['results_lifespan'],
  37. );
  38. $form['results_lifespan_custom'] = array(
  39. '#type' => 'textfield',
  40. '#title' => t('Seconds'),
  41. '#size' => '25',
  42. '#maxlength' => '30',
  43. '#description' => t('Length of time in seconds raw query results should be cached.'),
  44. '#default_value' => $this->options['results_lifespan_custom'],
  45. '#process' => array('ctools_dependent_process'),
  46. '#dependency' => array(
  47. 'edit-cache-options-results-lifespan' => array('custom'),
  48. ),
  49. );
  50. $form['output_lifespan'] = array(
  51. '#type' => 'select',
  52. '#title' => t('Rendered output'),
  53. '#description' => t('The length of time rendered HTML output should be cached.'),
  54. '#options' => $options,
  55. '#default_value' => $this->options['output_lifespan'],
  56. );
  57. $form['output_lifespan_custom'] = array(
  58. '#type' => 'textfield',
  59. '#title' => t('Seconds'),
  60. '#size' => '25',
  61. '#maxlength' => '30',
  62. '#description' => t('Length of time in seconds rendered HTML output should be cached.'),
  63. '#default_value' => $this->options['output_lifespan_custom'],
  64. '#process' => array('ctools_dependent_process'),
  65. '#dependency' => array(
  66. 'edit-cache-options-output-lifespan' => array('custom'),
  67. ),
  68. );
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function options_validate(&$form, &$form_state) {
  74. $custom_fields = array('output_lifespan', 'results_lifespan');
  75. foreach ($custom_fields as $field) {
  76. if ($form_state['values']['cache_options'][$field] == 'custom' && !is_numeric($form_state['values']['cache_options'][$field . '_custom'])) {
  77. form_error($form[$field . '_custom'], t('Custom time values must be numeric.'));
  78. }
  79. }
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function summary_title() {
  85. $results_lifespan = $this->get_lifespan('results');
  86. $output_lifespan = $this->get_lifespan('output');
  87. return format_interval($results_lifespan, 1) . '/' . format_interval($output_lifespan, 1);
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function get_lifespan($type) {
  93. $lifespan = $this->options[$type . '_lifespan'] == 'custom' ? $this->options[$type . '_lifespan_custom'] : $this->options[$type . '_lifespan'];
  94. return $lifespan;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function cache_expire($type) {
  100. $lifespan = $this->get_lifespan($type);
  101. if ($lifespan) {
  102. $cutoff = REQUEST_TIME - $lifespan;
  103. return $cutoff;
  104. }
  105. else {
  106. return FALSE;
  107. }
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function cache_set_expire($type) {
  113. $lifespan = $this->get_lifespan($type);
  114. if ($lifespan) {
  115. return time() + $lifespan;
  116. }
  117. else {
  118. return CACHE_PERMANENT;
  119. }
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function cache_set($type) {
  125. $lifespan = $this->get_lifespan($type);
  126. if ($lifespan >= 0) {
  127. parent::cache_set($type);
  128. }
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function cache_get($type) {
  134. $lifespan = $this->get_lifespan($type);
  135. if ($lifespan >= 0) {
  136. return parent::cache_get($type);
  137. }
  138. else {
  139. return FALSE;
  140. }
  141. }
  142. }