2 // $Id: cache.inc,v 1.24 2009/02/17 23:31:05 merlinofchaos Exp $
6 * Functions to load Views' data so that it knows what is available to
11 * Load views files on behalf of modules.
13 function _views_include_handlers() {
14 views_module_include('views.inc');
18 * Load default views files on behalf of modules.
20 function _views_include_default_views() {
21 views_module_include('views_default.inc');
25 * Fetch Views' data from the cache
27 function _views_fetch_data($table = NULL) {
30 $start = views_microtime();
31 // NOTE: This happens whether we retrieve them from cache or otherwise.
32 views_include_handlers();
34 $data = views_cache_get('views_data', TRUE);
35 if (!empty($data->data)) {
40 $cache = module_invoke_all('views_data');
41 foreach (module_implements('views_data_alter') as $module) {
42 $function = $module . '_views_data_alter';
46 views_cache_set('views_data', $cache, TRUE);
49 vpr('Views data build time: ' . (views_microtime() - $start) * 1000 . ' ms');
55 if (isset($cache[$table])) {
56 return $cache[$table];
59 // Return an empty array if there is no match.
64 * Fetch the plugin data from cache.
66 function _views_fetch_plugin_data($type = NULL, $plugin = NULL) {
69 $start = views_microtime();
70 views_include_handlers();
72 $cache = views_discover_plugins();
74 vpr('Views plugins build time: ' . (views_microtime() - $start) * 1000 . ' ms');
77 if (!$type && !$plugin) {
81 // Not in the if above so the else below won't run
82 if (isset($cache[$type])) {
86 else if (isset($cache[$type][$plugin])) {
87 return $cache[$type][$plugin];
90 // Return an empty array if there is no match.
95 * Scan all modules for default views and rebuild the default views cache.
97 * @return An associative array of all known default views.
99 function _views_discover_default_views() {
100 static $cache = NULL;
102 if (!isset($cache)) {
103 $data = views_cache_get('views_default_views', TRUE);
105 if (isset($data->data) && is_array($data->data)) {
106 $cache = $data->data;
109 views_include_default_views();
110 $defaults = module_invoke_all('views_default_views');
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.
118 $cache[$name] = $view;
121 views_cache_set('views_default_views', $cache, TRUE);
129 * Set a cached item in the views cache.
131 * This is just a convenience wrapper around cache_set().
134 * The cache ID of the data to store.
136 * The data to store in the cache. Complex data types will be automatically serialized before insertion.
137 * Strings will be stored as plain text and not serialized.
138 * @param $use_language
139 * If TRUE, the data will be cached specific to the currently active language.
141 function views_cache_set($cid, $data, $use_language = FALSE) {
144 if (variable_get('views_skip_cache', FALSE)) {
148 $cid .= ':' . $language->language;
151 cache_set($cid, $data, 'cache_views');
155 * Return data from the persistent views cache.
157 * This is just a convenience wrapper around cache_get().
160 * The cache ID of the data to retrieve.
161 * @param $use_language
162 * If TRUE, the data will be requested specific to the currently active language.
164 function views_cache_get($cid, $use_language = FALSE) {
167 if (variable_get('views_skip_cache', FALSE)) {
171 $cid .= ':' . $language->language;
174 return cache_get($cid, 'cache_views');
178 * @defgroup views_object_cache Non-volatile cache storage
180 * The non-volatile object cache is used to store an object while it is
181 * being edited, so that we don't have to save until we're completely
182 * done. The cache should be 'cleaned' on a regular basis, meaning to
183 * remove old objects from the cache, but otherwise the data in this
184 * cache must remain stable, as it includes unsaved changes.
188 * Get an object from the non-volatile Views cache.
190 * This function caches in memory as well, so that multiple calls to this
191 * will not result in multiple database reads.
194 * A 32 character or less string to define what kind of object is being
195 * stored; primarily this is used to prevent collisions.
197 * The name of the view (or other object) being stored.
199 * Skip the memory cache, meaning this must be read from the db again.
202 * The data that was cached.
204 function views_object_cache_get($obj, $name, $skip_cache = FALSE) {
205 static $cache = array();
211 if (!array_key_exists($key, $cache)) {
212 $data = db_fetch_object(db_query("SELECT * FROM {views_object_cache} WHERE sid = '%s' AND obj = '%s' AND name = '%s'", session_id(), $obj, $name));
214 $cache[$key] = unserialize($data->data);
217 return isset($cache[$key]) ? $cache[$key] : NULL;
221 * Store an object in the non-volatile Views cache.
224 * A 32 character or less string to define what kind of object is being
225 * stored; primarily this is used to prevent collisions.
227 * The name of the view (or other object) being stored.
229 * The object to be cached. This will be serialized prior to writing.
231 function views_object_cache_set($obj, $name, $cache) {
232 views_object_cache_clear($obj, $name);
233 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());
237 * Remove an object from the non-volatile Views cache
240 * A 32 character or less string to define what kind of object is being
241 * stored; primarily this is used to prevent collisions.
243 * The name of the view (or other object) being stored.
245 function views_object_cache_clear($obj, $name) {
246 db_query("DELETE FROM {views_object_cache} WHERE sid = '%s' AND obj = '%s' AND name = '%s'", session_id(), $obj, $name);
250 * Remove all objects in the object cache that are older than the
254 * The minimum age of objects to remove, in seconds. For example, 86400 is
255 * one day. Defaults to 7 days.
257 function views_object_cache_clean($age = NULL) {
259 $age = 86400 * 7; // 7 days
261 db_query("DELETE FROM {views_object_cache} WHERE updated < %d", time() - $age);