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