cache.inc

  1. 3.x includes/cache.inc
  2. 2.x includes/cache.inc

cache.inc

Functions to load Views' data so that it knows what is available to build queries from.

File

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