Projet

Général

Profil

Paste
Télécharger (4,28 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / plugins / views_plugin_cache_time.inc @ 5d12d676

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_plugin_cache_time.
6
 */
7

    
8
/**
9
 * Simple caching of query results for Views displays.
10
 *
11
 * @ingroup views_cache_plugins
12
 */
13
class views_plugin_cache_time extends views_plugin_cache {
14

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function option_definition() {
19
    $options = parent::option_definition();
20
    $options['results_lifespan'] = array('default' => 3600);
21
    $options['results_lifespan_custom'] = array('default' => 0);
22
    $options['output_lifespan'] = array('default' => 3600);
23
    $options['output_lifespan_custom'] = array('default' => 0);
24

    
25
    return $options;
26
  }
27

    
28
  /**
29
   * {@inheritdoc}
30
   */
31
  public function options_form(&$form, &$form_state) {
32
    parent::options_form($form, $form_state);
33
    $options = array(60, 300, 1800, 3600, 21600, 518400);
34
    $options = drupal_map_assoc($options, 'format_interval');
35
    $options = array(-1 => t('Never cache')) + $options + array('custom' => t('Custom'));
36

    
37
    $form['results_lifespan'] = array(
38
      '#type' => 'select',
39
      '#title' => t('Query results'),
40
      '#description' => t('The length of time raw query results should be cached.'),
41
      '#options' => $options,
42
      '#default_value' => $this->options['results_lifespan'],
43
    );
44
    $form['results_lifespan_custom'] = array(
45
      '#type' => 'textfield',
46
      '#title' => t('Seconds'),
47
      '#size' => '25',
48
      '#maxlength' => '30',
49
      '#description' => t('Length of time in seconds raw query results should be cached.'),
50
      '#default_value' => $this->options['results_lifespan_custom'],
51
      '#process' => array('ctools_dependent_process'),
52
      '#dependency' => array(
53
        'edit-cache-options-results-lifespan' => array('custom'),
54
      ),
55
    );
56
    $form['output_lifespan'] = array(
57
      '#type' => 'select',
58
      '#title' => t('Rendered output'),
59
      '#description' => t('The length of time rendered HTML output should be cached.'),
60
      '#options' => $options,
61
      '#default_value' => $this->options['output_lifespan'],
62
    );
63
    $form['output_lifespan_custom'] = array(
64
      '#type' => 'textfield',
65
      '#title' => t('Seconds'),
66
      '#size' => '25',
67
      '#maxlength' => '30',
68
      '#description' => t('Length of time in seconds rendered HTML output should be cached.'),
69
      '#default_value' => $this->options['output_lifespan_custom'],
70
      '#process' => array('ctools_dependent_process'),
71
      '#dependency' => array(
72
        'edit-cache-options-output-lifespan' => array('custom'),
73
      ),
74
    );
75
  }
76

    
77
  /**
78
   * {@inheritdoc}
79
   */
80
  public function options_validate(&$form, &$form_state) {
81
    $custom_fields = array('output_lifespan', 'results_lifespan');
82
    foreach ($custom_fields as $field) {
83
      if ($form_state['values']['cache_options'][$field] == 'custom' && !is_numeric($form_state['values']['cache_options'][$field . '_custom'])) {
84
        form_error($form[$field . '_custom'], t('Custom time values must be numeric.'));
85
      }
86
    }
87
  }
88

    
89
  /**
90
   * {@inheritdoc}
91
   */
92
  public function summary_title() {
93
    $results_lifespan = $this->get_lifespan('results');
94
    $output_lifespan = $this->get_lifespan('output');
95
    return format_interval($results_lifespan, 1) . '/' . format_interval($output_lifespan, 1);
96
  }
97

    
98
  /**
99
   * {@inheritdoc}
100
   */
101
  public function get_lifespan($type) {
102
    $lifespan = $this->options[$type . '_lifespan'] == 'custom' ? $this->options[$type . '_lifespan_custom'] : $this->options[$type . '_lifespan'];
103
    return $lifespan;
104
  }
105

    
106
  /**
107
   * {@inheritdoc}
108
   */
109
  public function cache_expire($type) {
110
    $lifespan = $this->get_lifespan($type);
111
    if ($lifespan) {
112
      $cutoff = REQUEST_TIME - $lifespan;
113
      return $cutoff;
114
    }
115
    else {
116
      return FALSE;
117
    }
118
  }
119

    
120
  /**
121
   * {@inheritdoc}
122
   */
123
  public function cache_set_expire($type) {
124
    $lifespan = $this->get_lifespan($type);
125
    if ($lifespan) {
126
      return time() + $lifespan;
127
    }
128
    else {
129
      return CACHE_PERMANENT;
130
    }
131
  }
132

    
133
  /**
134
   * {@inheritdoc}
135
   */
136
  public function cache_set($type) {
137
    $lifespan = $this->get_lifespan($type);
138
    if ($lifespan >= 0) {
139
      parent::cache_set($type);
140
    }
141
  }
142

    
143
  /**
144
   * {@inheritdoc}
145
   */
146
  public function cache_get($type) {
147
    $lifespan = $this->get_lifespan($type);
148
    if ($lifespan >= 0) {
149
      return parent::cache_get($type);
150
    }
151
    else {
152
      return FALSE;
153
    }
154
  }
155

    
156
}