cache.inc

  1. 7.x drupal-7.x/includes/cache.inc
  2. 6.x drupal-6.x/includes/cache.inc

File

drupal-6.x/includes/cache.inc
View source
  1. <?php
  2. /**
  3. * Return data from the persistent cache. Data may be stored as either plain text or as serialized data.
  4. * cache_get will automatically return unserialized objects and arrays.
  5. *
  6. * @param $cid
  7. * The cache ID of the data to retrieve.
  8. * @param $table
  9. * The table $table to store the data in. Valid core values are 'cache_filter',
  10. * 'cache_menu', 'cache_page', or 'cache' for the default cache.
  11. *
  12. * @see cache_set()
  13. */
  14. function cache_get($cid, $table = 'cache') {
  15. global $user;
  16. // Garbage collection necessary when enforcing a minimum cache lifetime
  17. $cache_flush = variable_get('cache_flush_'. $table, 0);
  18. if ($cache_flush && ($cache_flush + variable_get('cache_lifetime', 0) <= time())) {
  19. // Reset the variable immediately to prevent a meltdown in heavy load situations.
  20. variable_set('cache_flush_'. $table, 0);
  21. // Time to flush old cache data
  22. db_query("DELETE FROM {". $table ."} WHERE expire != %d AND expire <= %d", CACHE_PERMANENT, $cache_flush);
  23. }
  24. $cache = db_fetch_object(db_query("SELECT data, created, headers, expire, serialized FROM {". $table ."} WHERE cid = '%s'", $cid));
  25. if (isset($cache->data)) {
  26. // If the data is permanent or we're not enforcing a minimum cache lifetime
  27. // always return the cached data.
  28. if ($cache->expire == CACHE_PERMANENT || !variable_get('cache_lifetime', 0)) {
  29. $cache->data = db_decode_blob($cache->data);
  30. if ($cache->serialized) {
  31. $cache->data = unserialize($cache->data);
  32. }
  33. }
  34. // If enforcing a minimum cache lifetime, validate that the data is
  35. // currently valid for this user before we return it by making sure the
  36. // cache entry was created before the timestamp in the current session's
  37. // cache timer. The cache variable is loaded into the $user object by
  38. // sess_read() in session.inc.
  39. else {
  40. if (isset($user->cache) && $user->cache > $cache->created) {
  41. // This cache data is too old and thus not valid for us, ignore it.
  42. return 0;
  43. }
  44. else {
  45. $cache->data = db_decode_blob($cache->data);
  46. if ($cache->serialized) {
  47. $cache->data = unserialize($cache->data);
  48. }
  49. }
  50. }
  51. return $cache;
  52. }
  53. return 0;
  54. }
  55. /**
  56. * Store data in the persistent cache.
  57. *
  58. * The persistent cache is split up into four database
  59. * tables. Contributed modules can add additional tables.
  60. *
  61. * 'cache_page': This table stores generated pages for anonymous
  62. * users. This is the only table affected by the page cache setting on
  63. * the administrator panel.
  64. *
  65. * 'cache_menu': Stores the cachable part of the users' menus.
  66. *
  67. * 'cache_filter': Stores filtered pieces of content. This table is
  68. * periodically cleared of stale entries by cron.
  69. *
  70. * 'cache': Generic cache storage table.
  71. *
  72. * The reasons for having several tables are as follows:
  73. *
  74. * - smaller tables allow for faster selects and inserts
  75. * - we try to put fast changing cache items and rather static
  76. * ones into different tables. The effect is that only the fast
  77. * changing tables will need a lot of writes to disk. The more
  78. * static tables will also be better cachable with MySQL's query cache
  79. *
  80. * @param $cid
  81. * The cache ID of the data to store.
  82. * @param $data
  83. * The data to store in the cache. Complex data types will be automatically serialized before insertion.
  84. * Strings will be stored as plain text and not serialized.
  85. * @param $table
  86. * The table $table to store the data in. Valid core values are 'cache_filter',
  87. * 'cache_menu', 'cache_page', or 'cache'.
  88. * @param $expire
  89. * One of the following values:
  90. * - CACHE_PERMANENT: Indicates that the item should never be removed unless
  91. * explicitly told to using cache_clear_all() with a cache ID.
  92. * - CACHE_TEMPORARY: Indicates that the item should be removed at the next
  93. * general cache wipe.
  94. * - A Unix timestamp: Indicates that the item should be kept at least until
  95. * the given time, after which it behaves like CACHE_TEMPORARY.
  96. * @param $headers
  97. * A string containing HTTP header information for cached pages.
  98. *
  99. * @see cache_get()
  100. */
  101. function cache_set($cid, $data, $table = 'cache', $expire = CACHE_PERMANENT, $headers = NULL) {
  102. $serialized = 0;
  103. if (is_object($data) || is_array($data)) {
  104. $data = serialize($data);
  105. $serialized = 1;
  106. }
  107. $created = time();
  108. db_query("UPDATE {". $table ."} SET data = %b, created = %d, expire = %d, headers = '%s', serialized = %d WHERE cid = '%s'", $data, $created, $expire, $headers, $serialized, $cid);
  109. if (!db_affected_rows()) {
  110. @db_query("INSERT INTO {". $table ."} (cid, data, created, expire, headers, serialized) VALUES ('%s', %b, %d, %d, '%s', %d)", $cid, $data, $created, $expire, $headers, $serialized);
  111. }
  112. }
  113. /**
  114. *
  115. * Expire data from the cache. If called without arguments, expirable
  116. * entries will be cleared from the cache_page and cache_block tables.
  117. *
  118. * @param $cid
  119. * If set, the cache ID to delete. Otherwise, all cache entries that can
  120. * expire are deleted.
  121. *
  122. * @param $table
  123. * If set, the table $table to delete from. Mandatory
  124. * argument if $cid is set.
  125. *
  126. * @param $wildcard
  127. * If $wildcard is TRUE, cache IDs starting with $cid are deleted in
  128. * addition to the exact cache ID specified by $cid. If $wildcard is
  129. * TRUE and $cid is '*' then the entire table $table is emptied.
  130. */
  131. function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
  132. global $user;
  133. if (!isset($cid) && !isset($table)) {
  134. // Clear the block cache first, so stale data will
  135. // not end up in the page cache.
  136. cache_clear_all(NULL, 'cache_block');
  137. cache_clear_all(NULL, 'cache_page');
  138. return;
  139. }
  140. if (empty($cid)) {
  141. if (variable_get('cache_lifetime', 0)) {
  142. // We store the time in the current user's $user->cache variable which
  143. // will be saved into the sessions table by sess_write(). We then
  144. // simulate that the cache was flushed for this user by not returning
  145. // cached data that was cached before the timestamp.
  146. $user->cache = time();
  147. $cache_flush = variable_get('cache_flush_'. $table, 0);
  148. if ($cache_flush == 0) {
  149. // This is the first request to clear the cache, start a timer.
  150. variable_set('cache_flush_'. $table, time());
  151. }
  152. else if (time() > ($cache_flush + variable_get('cache_lifetime', 0))) {
  153. // Clear the cache for everyone, cache_lifetime seconds have
  154. // passed since the first request to clear the cache.
  155. db_query("DELETE FROM {". $table ."} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time());
  156. variable_set('cache_flush_'. $table, 0);
  157. }
  158. }
  159. else {
  160. // No minimum cache lifetime, flush all temporary cache entries now.
  161. db_query("DELETE FROM {". $table ."} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time());
  162. }
  163. }
  164. else {
  165. if ($wildcard) {
  166. if ($cid == '*') {
  167. db_query("TRUNCATE TABLE {". $table ."}");
  168. }
  169. else {
  170. db_query("DELETE FROM {". $table ."} WHERE cid LIKE '%s%%'", $cid);
  171. }
  172. }
  173. else {
  174. db_query("DELETE FROM {". $table ."} WHERE cid = '%s'", $cid);
  175. }
  176. }
  177. }