cache.inc
5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* @file
* Load Views' data so that it knows what is available to build queries from.
*/
/**
* Fetch Views' data from the cache
*
* @param $move
* Under certain circumstances it makes sense to not get the moved table, but the old one.
* One example is views_get_handler.
*/
function _views_fetch_data($table = NULL, $move = TRUE, $reset = FALSE) {
$cache = &drupal_static(__FUNCTION__ . '_cache');
$recursion_protection = &drupal_static(__FUNCTION__ . '_recursion_protected');
$fully_loaded = &drupal_static(__FUNCTION__ . '_fully_loaded');
if ($reset) {
$cache = NULL;
$fully_loaded = FALSE;
}
if ($table) {
if (!isset($cache[$table])) {
$cid = 'views_data:' . $table;
$data = views_cache_get($cid, TRUE);
if (!empty($data->data)) {
$cache[$table] = $data->data;
}
else {
if (!$fully_loaded) {
// No cache entry, rebuild.
$cache = _views_fetch_data_build();
$fully_loaded = TRUE;
}
}
}
if (isset($cache[$table])) {
if (isset($cache[$table]['moved to']) && $move) {
$moved_table = $cache[$table]['moved to'];
if (!empty($recursion_protection[$table])) {
// recursion detected!
return NULL;
}
$recursion_protection[$table] = TRUE;
$data = _views_fetch_data($moved_table);
$recursion_protection = array();
return $data;
}
return $cache[$table];
}
}
else {
if (!$fully_loaded) {
$data = views_cache_get('views_data', TRUE);
if (!empty($data->data)) {
$cache = $data->data;
}
if (empty($cache)) {
$cache = _views_fetch_data_build();
}
$fully_loaded = TRUE;
}
return $cache;
}
// Return an empty array if there is no match.
return array();
}
/**
* Build and set the views data cache if empty.
*/
function _views_fetch_data_build() {
views_include_handlers();
$cache = module_invoke_all('views_data');
foreach (module_implements('views_data_alter') as $module) {
$function = $module . '_views_data_alter';
$function($cache);
}
_views_data_process_entity_types($cache);
// Keep a record with all data.
views_cache_set('views_data', $cache, TRUE);
// Save data in seperate cache entries.
foreach ($cache as $key => $data) {
$cid = 'views_data:' . $key;
views_cache_set($cid, $data, TRUE);
}
return $cache;
}
/**
* Links tables having an 'entity type' specified to the respective generic entity-type tables.
*/
function _views_data_process_entity_types(&$data) {
foreach ($data as $table_name => $table_info) {
// Add in a join from the entity-table if an entity-type is given.
if (!empty($table_info['table']['entity type'])) {
$entity_table = 'views_entity_' . $table_info['table']['entity type'];
$data[$entity_table]['table']['join'][$table_name] = array(
'left_table' => $table_name,
);
$data[$entity_table]['table']['entity type'] = $table_info['table']['entity type'];
// Copy over the default table group if we have none yet.
if (!empty($table_info['table']['group']) && empty($data[$entity_table]['table']['group'])) {
$data[$entity_table]['table']['group'] = $table_info['table']['group'];
}
}
}
}
/**
* Fetch the plugin data from cache.
*/
function _views_fetch_plugin_data($type = NULL, $plugin = NULL, $reset = FALSE) {
static $cache = NULL;
if (!isset($cache) || $reset) {
$start = microtime(TRUE);
views_include('plugins');
views_include_handlers();
$cache = views_discover_plugins();
}
if (!$type && !$plugin) {
return $cache;
}
elseif (!$plugin) {
// Not in the if above so the else below won't run
if (isset($cache[$type])) {
return $cache[$type];
}
}
elseif (isset($cache[$type][$plugin])) {
return $cache[$type][$plugin];
}
// Return an empty array if there is no match.
return array();
}
/**
* Set a cached item in the views cache.
*
* This is just a convenience wrapper around cache_set().
*
* @param $cid
* The cache ID of the data to store.
* @param $data
* The data to store in the cache. Complex data types will be automatically serialized before insertion.
* Strings will be stored as plain text and not serialized.
* @param $use_language
* If TRUE, the data will be cached specific to the currently active language.
*/
function views_cache_set($cid, $data, $use_language = FALSE) {
global $language;
if (variable_get('views_skip_cache', FALSE)) {
return;
}
if ($use_language) {
$cid .= ':' . $language->language;
}
cache_set($cid, $data, 'cache_views');
}
/**
* Return data from the persistent views cache.
*
* This is just a convenience wrapper around cache_get().
*
* @param int $cid
* The cache ID of the data to retrieve.
* @param bool $use_language
* If TRUE, the data will be requested specific to the currently active language.
*
* @return stdClass|bool
* The cache or FALSE on failure.
*/
function views_cache_get($cid, $use_language = FALSE) {
global $language;
if (variable_get('views_skip_cache', FALSE)) {
return FALSE;
}
if ($use_language) {
$cid .= ':' . $language->language;
}
return cache_get($cid, 'cache_views');
}