views_plugin_cache_time.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. function option_definition() {
  13. $options = parent::option_definition();
  14. $options['results_lifespan'] = array('default' => 3600);
  15. $options['results_lifespan_custom'] = array('default' => 0);
  16. $options['output_lifespan'] = array('default' => 3600);
  17. $options['output_lifespan_custom'] = array('default' => 0);
  18. return $options;
  19. }
  20. function options_form(&$form, &$form_state) {
  21. parent::options_form($form, $form_state);
  22. $options = array(60, 300, 1800, 3600, 21600, 518400);
  23. $options = drupal_map_assoc($options, 'format_interval');
  24. $options = array(-1 => t('Never cache')) + $options + array('custom' => t('Custom'));
  25. $form['results_lifespan'] = array(
  26. '#type' => 'select',
  27. '#title' => t('Query results'),
  28. '#description' => t('The length of time raw query results should be cached.'),
  29. '#options' => $options,
  30. '#default_value' => $this->options['results_lifespan'],
  31. );
  32. $form['results_lifespan_custom'] = array(
  33. '#type' => 'textfield',
  34. '#title' => t('Seconds'),
  35. '#size' => '25',
  36. '#maxlength' => '30',
  37. '#description' => t('Length of time in seconds raw query results should be cached.'),
  38. '#default_value' => $this->options['results_lifespan_custom'],
  39. '#process' => array('form_process_select','ctools_dependent_process'),
  40. '#dependency' => array(
  41. 'edit-cache-options-results-lifespan' => array('custom'),
  42. ),
  43. );
  44. $form['output_lifespan'] = array(
  45. '#type' => 'select',
  46. '#title' => t('Rendered output'),
  47. '#description' => t('The length of time rendered HTML output should be cached.'),
  48. '#options' => $options,
  49. '#default_value' => $this->options['output_lifespan'],
  50. );
  51. $form['output_lifespan_custom'] = array(
  52. '#type' => 'textfield',
  53. '#title' => t('Seconds'),
  54. '#size' => '25',
  55. '#maxlength' => '30',
  56. '#description' => t('Length of time in seconds rendered HTML output should be cached.'),
  57. '#default_value' => $this->options['output_lifespan_custom'],
  58. '#process' => array('form_process_select','ctools_dependent_process'),
  59. '#dependency' => array(
  60. 'edit-cache-options-output-lifespan' => array('custom'),
  61. ),
  62. );
  63. }
  64. function options_validate(&$form, &$form_state) {
  65. $custom_fields = array('output_lifespan', 'results_lifespan');
  66. foreach ($custom_fields as $field) {
  67. if ($form_state['values']['cache_options'][$field] == 'custom' && !is_numeric($form_state['values']['cache_options'][$field . '_custom'])) {
  68. form_error($form[$field .'_custom'], t('Custom time values must be numeric.'));
  69. }
  70. }
  71. }
  72. function summary_title() {
  73. $results_lifespan = $this->get_lifespan('results');
  74. $output_lifespan = $this->get_lifespan('output');
  75. return format_interval($results_lifespan, 1) . '/' . format_interval($output_lifespan, 1);
  76. }
  77. function get_lifespan($type) {
  78. $lifespan = $this->options[$type . '_lifespan'] == 'custom' ? $this->options[$type . '_lifespan_custom'] : $this->options[$type . '_lifespan'];
  79. return $lifespan;
  80. }
  81. function cache_expire($type) {
  82. $lifespan = $this->get_lifespan($type);
  83. if ($lifespan) {
  84. $cutoff = REQUEST_TIME - $lifespan;
  85. return $cutoff;
  86. }
  87. else {
  88. return FALSE;
  89. }
  90. }
  91. function cache_set_expire($type) {
  92. $lifespan = $this->get_lifespan($type);
  93. if ($lifespan) {
  94. return time() + $lifespan;
  95. }
  96. else {
  97. return CACHE_PERMANENT;
  98. }
  99. }
  100. function cache_set($type) {
  101. $lifespan = $this->get_lifespan($type);
  102. if ($lifespan >= 0) {
  103. parent::cache_set($type);
  104. }
  105. }
  106. function cache_get($type) {
  107. $lifespan = $this->get_lifespan($type);
  108. if ($lifespan >= 0) {
  109. return parent::cache_get($type);
  110. }
  111. else {
  112. return FALSE;
  113. }
  114. }
  115. }