annotate includes/cache.inc @ 8:85cbd6048071

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