| 1 |
<?php
|
| 2 |
// $Id: cache.inc,v 1.24 2009/02/17 23:31:05 merlinofchaos Exp $
|
| 3 |
/**
|
| 4 |
* @file cache.inc
|
| 5 |
*
|
| 6 |
* Functions to load Views' data so that it knows what is available to
|
| 7 |
* build queries from.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Load views files on behalf of modules.
|
| 12 |
*/
|
| 13 |
function _views_include_handlers() {
|
| 14 |
views_module_include('views.inc');
|
| 15 |
}
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Load default views files on behalf of modules.
|
| 19 |
*/
|
| 20 |
function _views_include_default_views() {
|
| 21 |
views_module_include('views_default.inc');
|
| 22 |
}
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Fetch Views' data from the cache
|
| 26 |
*/
|
| 27 |
function _views_fetch_data($table = NULL) {
|
| 28 |
static $cache = NULL;
|
| 29 |
if (!isset($cache)) {
|
| 30 |
$start = views_microtime();
|
| 31 |
// NOTE: This happens whether we retrieve them from cache or otherwise.
|
| 32 |
views_include_handlers();
|
| 33 |
|
| 34 |
$data = views_cache_get('views_data', TRUE);
|
| 35 |
if (!empty($data->data)) {
|
| 36 |
$cache = $data->data;
|
| 37 |
}
|
| 38 |
|
| 39 |
if (empty($cache)) {
|
| 40 |
$cache = module_invoke_all('views_data');
|
| 41 |
foreach (module_implements('views_data_alter') as $module) {
|
| 42 |
$function = $module . '_views_data_alter';
|
| 43 |
$function($cache);
|
| 44 |
}
|
| 45 |
|
| 46 |
views_cache_set('views_data', $cache, TRUE);
|
| 47 |
}
|
| 48 |
|
| 49 |
vpr('Views data build time: ' . (views_microtime() - $start) * 1000 . ' ms');
|
| 50 |
}
|
| 51 |
|
| 52 |
if (!$table) {
|
| 53 |
return $cache;
|
| 54 |
}
|
| 55 |
if (isset($cache[$table])) {
|
| 56 |
return $cache[$table];
|
| 57 |
}
|
| 58 |
|
| 59 |
// Return an empty array if there is no match.
|
| 60 |
return array();
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Fetch the plugin data from cache.
|
| 65 |
*/
|
| 66 |
function _views_fetch_plugin_data($type = NULL, $plugin = NULL) {
|
| 67 |
static $cache = NULL;
|
| 68 |
if (!isset($cache)) {
|
| 69 |
$start = views_microtime();
|
| 70 |
views_include_handlers();
|
| 71 |
|
| 72 |
$cache = views_discover_plugins();
|
| 73 |
|
| 74 |
vpr('Views plugins build time: ' . (views_microtime() - $start) * 1000 . ' ms');
|
| 75 |
}
|
| 76 |
|
| 77 |
if (!$type && !$plugin) {
|
| 78 |
return $cache;
|
| 79 |
}
|
| 80 |
else if (!$plugin) {
|
| 81 |
// Not in the if above so the else below won't run
|
| 82 |
if (isset($cache[$type])) {
|
| 83 |
return $cache[$type];
|
| 84 |
}
|
| 85 |
}
|
| 86 |
else if (isset($cache[$type][$plugin])) {
|
| 87 |
return $cache[$type][$plugin];
|
| 88 |
}
|
| 89 |
|
| 90 |
// Return an empty array if there is no match.
|
| 91 |
return array();
|
| 92 |
}
|
| 93 |
|
| 94 |
/**
|
| 95 |
* Scan all modules for default views and rebuild the default views cache.
|
| 96 |
*
|
| 97 |
* @return An associative array of all known default views.
|
| 98 |
*/
|
| 99 |
function _views_discover_default_views() {
|
| 100 |
static $cache = NULL;
|
| 101 |
|
| 102 |
if (!isset($cache)) {
|
| 103 |
$data = views_cache_get('views_default_views', TRUE);
|
| 104 |
|
| 105 |
if (isset($data->data) && is_array($data->data)) {
|
| 106 |
$cache = $data->data;
|
| 107 |
}
|
| 108 |
else {
|
| 109 |
views_include_default_views();
|
| 110 |
$defaults = module_invoke_all('views_default_views');
|
| 111 |
$cache = array();
|
| 112 |
|
| 113 |
foreach ($defaults as $name => $view) {
|
| 114 |
// Only views with a sufficiently high api version are eligible.
|
| 115 |
if (isset($view->api_version) && $view->api_version >= 2) {
|
| 116 |
// Do not cache dead handlers.
|
| 117 |
$view->destroy();
|
| 118 |
$cache[$name] = $view;
|
| 119 |
}
|
| 120 |
}
|
| 121 |
|
| 122 |
// Allow modules to modify default views before they are cached.
|
| 123 |
drupal_alter('views_default_views', $cache);
|
| 124 |
|
| 125 |
views_cache_set('views_default_views', $cache, TRUE);
|
| 126 |
}
|
| 127 |
}
|
| 128 |
|
| 129 |
return $cache;
|
| 130 |
}
|
| 131 |
|
| 132 |
/**
|
| 133 |
* Set a cached item in the views cache.
|
| 134 |
*
|
| 135 |
* This is just a convenience wrapper around cache_set().
|
| 136 |
*
|
| 137 |
* @param $cid
|
| 138 |
* The cache ID of the data to store.
|
| 139 |
* @param $data
|
| 140 |
* The data to store in the cache. Complex data types will be automatically serialized before insertion.
|
| 141 |
* Strings will be stored as plain text and not serialized.
|
| 142 |
* @param $use_language
|
| 143 |
* If TRUE, the data will be cached specific to the currently active language.
|
| 144 |
*/
|
| 145 |
function views_cache_set($cid, $data, $use_language = FALSE) {
|
| 146 |
global $language;
|
| 147 |
|
| 148 |
if (variable_get('views_skip_cache', FALSE)) {
|
| 149 |
return;
|
| 150 |
}
|
| 151 |
if ($use_language) {
|
| 152 |
$cid .= ':' . $language->language;
|
| 153 |
}
|
| 154 |
|
| 155 |
cache_set($cid, $data, 'cache_views');
|
| 156 |
}
|
| 157 |
|
| 158 |
/**
|
| 159 |
* Return data from the persistent views cache.
|
| 160 |
*
|
| 161 |
* This is just a convenience wrapper around cache_get().
|
| 162 |
*
|
| 163 |
* @param $cid
|
| 164 |
* The cache ID of the data to retrieve.
|
| 165 |
* @param $use_language
|
| 166 |
* If TRUE, the data will be requested specific to the currently active language.
|
| 167 |
*/
|
| 168 |
function views_cache_get($cid, $use_language = FALSE) {
|
| 169 |
global $language;
|
| 170 |
|
| 171 |
if (variable_get('views_skip_cache', FALSE)) {
|
| 172 |
return 0;
|
| 173 |
}
|
| 174 |
if ($use_language) {
|
| 175 |
$cid .= ':' . $language->language;
|
| 176 |
}
|
| 177 |
|
| 178 |
return cache_get($cid, 'cache_views');
|
| 179 |
}
|
| 180 |
|
| 181 |
/**
|
| 182 |
* @defgroup views_object_cache Non-volatile cache storage
|
| 183 |
* @{
|
| 184 |
* The non-volatile object cache is used to store an object while it is
|
| 185 |
* being edited, so that we don't have to save until we're completely
|
| 186 |
* done. The cache should be 'cleaned' on a regular basis, meaning to
|
| 187 |
* remove old objects from the cache, but otherwise the data in this
|
| 188 |
* cache must remain stable, as it includes unsaved changes.
|
| 189 |
*/
|
| 190 |
|
| 191 |
/**
|
| 192 |
* Get an object from the non-volatile Views cache.
|
| 193 |
*
|
| 194 |
* This function caches in memory as well, so that multiple calls to this
|
| 195 |
* will not result in multiple database reads.
|
| 196 |
*
|
| 197 |
* @param $obj
|
| 198 |
* A 32 character or less string to define what kind of object is being
|
| 199 |
* stored; primarily this is used to prevent collisions.
|
| 200 |
* @param $name
|
| 201 |
* The name of the view (or other object) being stored.
|
| 202 |
* @param $skip_cache
|
| 203 |
* Skip the memory cache, meaning this must be read from the db again.
|
| 204 |
*
|
| 205 |
* @return
|
| 206 |
* The data that was cached.
|
| 207 |
*/
|
| 208 |
function views_object_cache_get($obj, $name, $skip_cache = FALSE) {
|
| 209 |
static $cache = array();
|
| 210 |
$key = "$obj:$name";
|
| 211 |
if ($skip_cache) {
|
| 212 |
unset($cache[$key]);
|
| 213 |
}
|
| 214 |
|
| 215 |
if (!array_key_exists($key, $cache)) {
|
| 216 |
$data = db_fetch_object(db_query("SELECT * FROM {views_object_cache} WHERE sid = '%s' AND obj = '%s' AND name = '%s'", session_id(), $obj, $name));
|
| 217 |
if ($data) {
|
| 218 |
$cache[$key] = unserialize($data->data);
|
| 219 |
}
|
| 220 |
}
|
| 221 |
return isset($cache[$key]) ? $cache[$key] : NULL;
|
| 222 |
}
|
| 223 |
|
| 224 |
/**
|
| 225 |
* Store an object in the non-volatile Views cache.
|
| 226 |
*
|
| 227 |
* @param $obj
|
| 228 |
* A 32 character or less string to define what kind of object is being
|
| 229 |
* stored; primarily this is used to prevent collisions.
|
| 230 |
* @param $name
|
| 231 |
* The name of the view (or other object) being stored.
|
| 232 |
* @param $cache
|
| 233 |
* The object to be cached. This will be serialized prior to writing.
|
| 234 |
*/
|
| 235 |
function views_object_cache_set($obj, $name, $cache) {
|
| 236 |
views_object_cache_clear($obj, $name);
|
| 237 |
db_query("INSERT INTO {views_object_cache} (sid, obj, name, data, updated) VALUES ('%s', '%s', '%s', '%s', %d)", session_id(), $obj, $name, serialize($cache), time());
|
| 238 |
}
|
| 239 |
|
| 240 |
/**
|
| 241 |
* Remove an object from the non-volatile Views cache
|
| 242 |
*
|
| 243 |
* @param $obj
|
| 244 |
* A 32 character or less string to define what kind of object is being
|
| 245 |
* stored; primarily this is used to prevent collisions.
|
| 246 |
* @param $name
|
| 247 |
* The name of the view (or other object) being stored.
|
| 248 |
*/
|
| 249 |
function views_object_cache_clear($obj, $name) {
|
| 250 |
db_query("DELETE FROM {views_object_cache} WHERE sid = '%s' AND obj = '%s' AND name = '%s'", session_id(), $obj, $name);
|
| 251 |
}
|
| 252 |
|
| 253 |
/**
|
| 254 |
* Remove all objects in the object cache that are older than the
|
| 255 |
* specified age.
|
| 256 |
*
|
| 257 |
* @param $age
|
| 258 |
* The minimum age of objects to remove, in seconds. For example, 86400 is
|
| 259 |
* one day. Defaults to 7 days.
|
| 260 |
*/
|
| 261 |
function views_object_cache_clean($age = NULL) {
|
| 262 |
if (empty($age)) {
|
| 263 |
$age = 86400 * 7; // 7 days
|
| 264 |
}
|
| 265 |
db_query("DELETE FROM {views_object_cache} WHERE updated < %d", time() - $age);
|
| 266 |
}
|
| 267 |
|
| 268 |
/**
|
| 269 |
* @}
|
| 270 |
*/
|