| 1 | <?php |
| 2 | // $Id: cache.inc,v 1.28 2009/02/03 12:30:14 dries 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 | function cache_get($cid, $table = 'cache') { |
| 18 | global $user; |
| 19 | |
| 20 | // Garbage collection necessary when enforcing a minimum cache lifetime |
| 21 | $cache_flush = variable_get('cache_flush', 0); |
| 22 | 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 | variable_set('cache_flush', 0); |
| 25 | // Time to flush old cache data |
| 26 | db_delete($table) |
| 27 | ->condition('expire', CACHE_PERMANENT, '<>') |
| 28 | ->condition('expire', $cache_flush, '<=') |
| 29 | ->execute(); |
| 30 | } |
| 31 | |
| 32 | $cache = db_query("SELECT data, created, headers, expire, serialized FROM {" . $table . "} WHERE cid = :cid", array(':cid' => $cid))->fetchObject(); |
| 33 | 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 | if ($cache->expire == CACHE_PERMANENT || !variable_get('cache_lifetime', 0)) { |
| 37 | if ($cache->serialized) { |
| 38 | $cache->data = unserialize($cache->data); |
| 39 | } |
| 40 | } |
| 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 | if ($user->cache > $cache->created) { |
| 48 | // This cache data is too old and thus not valid for us, ignore it. |
| 49 | return FALSE; |
| 50 | } |
| 51 | else { |
| 52 | if ($cache->serialized) { |
| 53 | $cache->data = unserialize($cache->data); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | return $cache; |
| 58 | } |
| 59 | return FALSE; |
| 60 | } |
| 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 | function cache_set($cid, $data, $table = 'cache', $expire = CACHE_PERMANENT, $headers = NULL) { |
| 108 | $fields = array( |
| 109 | 'serialized' => 0, |
| 110 | 'created' => REQUEST_TIME, |
| 111 | 'expire' => $expire, |
| 112 | 'headers' => $headers, |
| 113 | ); |
| 114 | if (!is_string($data)) { |
| 115 | $fields['data'] = serialize($data); |
| 116 | $fields['serialized'] = 1; |
| 117 | } |
| 118 | else { |
| 119 | $fields['data'] = $data; |
| 120 | $fields['serialized'] = 0; |
| 121 | } |
| 122 | |
| 123 | db_merge($table) |
| 124 | ->key(array('cid' => $cid)) |
| 125 | ->fields($fields) |
| 126 | ->execute(); |
| 127 | } |
| 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 | function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) { |
| 148 | global $user; |
| 149 | |
| 150 | if (!isset($cid) && !isset($table)) { |
| 151 | // Clear the block cache first, so stale data will |
| 152 | // not end up in the page cache. |
| 153 | if (module_exists('block')) { |
| 154 | cache_clear_all(NULL, 'cache_block'); |
| 155 | } |
| 156 | cache_clear_all(NULL, 'cache_page'); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if (empty($cid)) { |
| 161 | if (variable_get('cache_lifetime', 0)) { |
| 162 | // We store the time in the current user's $user->cache variable which |
| 163 | // will be saved into the sessions table by _sess_write(). We then |
| 164 | // simulate that the cache was flushed for this user by not returning |
| 165 | // cached data that was cached before the timestamp. |
| 166 | $user->cache = REQUEST_TIME; |
| 167 | |
| 168 | $cache_flush = variable_get('cache_flush', 0); |
| 169 | if ($cache_flush == 0) { |
| 170 | // This is the first request to clear the cache, start a timer. |
| 171 | variable_set('cache_flush', REQUEST_TIME); |
| 172 | } |
| 173 | elseif (REQUEST_TIME > ($cache_flush + variable_get('cache_lifetime', 0))) { |
| 174 | // Clear the cache for everyone, cache_flush_delay seconds have |
| 175 | // passed since the first request to clear the cache. |
| 176 | db_delete($table) |
| 177 | ->condition('expire', CACHE_PERMANENT, '<>') |
| 178 | ->condition('expire', REQUEST_TIME, '<') |
| 179 | ->execute(); |
| 180 | variable_set('cache_flush', 0); |
| 181 | } |
| 182 | } |
| 183 | else { |
| 184 | // No minimum cache lifetime, flush all temporary cache entries now. |
| 185 | db_delete($table) |
| 186 | ->condition('expire', CACHE_PERMANENT, '<>') |
| 187 | ->condition('expire', REQUEST_TIME, '<') |
| 188 | ->execute(); |
| 189 | } |
| 190 | } |
| 191 | else { |
| 192 | if ($wildcard) { |
| 193 | if ($cid == '*') { |
| 194 | db_delete($table)->execute(); |
| 195 | } |
| 196 | else { |
| 197 | db_delete($table) |
| 198 | ->condition('cid', $cid . '%', 'LIKE') |
| 199 | ->execute(); |
| 200 | } |
| 201 | } |
| 202 | else { |
| 203 | db_delete($table) |
| 204 | ->condition('cid', $cid) |
| 205 | ->execute(); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 |