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