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