webmaster@1: data)) { webmaster@1: // If the data is permanent or we're not enforcing a minimum cache lifetime webmaster@1: // always return the cached data. webmaster@1: if ($cache->expire == CACHE_PERMANENT || !variable_get('cache_lifetime', 0)) { webmaster@1: $cache->data = db_decode_blob($cache->data); webmaster@1: if ($cache->serialized) { webmaster@1: $cache->data = unserialize($cache->data); webmaster@1: } webmaster@1: } webmaster@1: // If enforcing a minimum cache lifetime, validate that the data is webmaster@1: // currently valid for this user before we return it by making sure the webmaster@1: // cache entry was created before the timestamp in the current session's webmaster@1: // cache timer. The cache variable is loaded into the $user object by webmaster@1: // sess_read() in session.inc. webmaster@1: else { webmaster@1: if ($user->cache > $cache->created) { webmaster@1: // This cache data is too old and thus not valid for us, ignore it. webmaster@1: return 0; webmaster@1: } webmaster@1: else { webmaster@1: $cache->data = db_decode_blob($cache->data); webmaster@1: if ($cache->serialized) { webmaster@1: $cache->data = unserialize($cache->data); webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: return $cache; webmaster@1: } webmaster@1: return 0; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Store data in the persistent cache. webmaster@1: * webmaster@1: * The persistent cache is split up into four database webmaster@1: * tables. Contributed modules can add additional tables. webmaster@1: * webmaster@1: * 'cache_page': This table stores generated pages for anonymous webmaster@1: * users. This is the only table affected by the page cache setting on webmaster@1: * the administrator panel. webmaster@1: * webmaster@1: * 'cache_menu': Stores the cachable part of the users' menus. webmaster@1: * webmaster@1: * 'cache_filter': Stores filtered pieces of content. This table is webmaster@1: * periodically cleared of stale entries by cron. webmaster@1: * webmaster@1: * 'cache': Generic cache storage table. webmaster@1: * webmaster@1: * The reasons for having several tables are as follows: webmaster@1: * webmaster@1: * - smaller tables allow for faster selects and inserts webmaster@1: * - we try to put fast changing cache items and rather static webmaster@1: * ones into different tables. The effect is that only the fast webmaster@1: * changing tables will need a lot of writes to disk. The more webmaster@1: * static tables will also be better cachable with MySQL's query cache webmaster@1: * webmaster@1: * @param $cid webmaster@1: * The cache ID of the data to store. webmaster@1: * @param $data webmaster@1: * The data to store in the cache. Complex data types will be automatically serialized before insertion. webmaster@1: * Strings will be stored as plain text and not serialized. webmaster@1: * @param $table webmaster@1: * The table $table to store the data in. Valid core values are 'cache_filter', webmaster@1: * 'cache_menu', 'cache_page', or 'cache'. webmaster@1: * @param $expire webmaster@1: * One of the following values: webmaster@1: * - CACHE_PERMANENT: Indicates that the item should never be removed unless webmaster@1: * explicitly told to using cache_clear_all() with a cache ID. webmaster@1: * - CACHE_TEMPORARY: Indicates that the item should be removed at the next webmaster@1: * general cache wipe. webmaster@1: * - A Unix timestamp: Indicates that the item should be kept at least until webmaster@1: * the given time, after which it behaves like CACHE_TEMPORARY. webmaster@1: * @param $headers webmaster@1: * A string containing HTTP header information for cached pages. webmaster@1: */ webmaster@1: function cache_set($cid, $data, $table = 'cache', $expire = CACHE_PERMANENT, $headers = NULL) { webmaster@1: $serialized = 0; webmaster@1: if (is_object($data) || is_array($data)) { webmaster@1: $data = serialize($data); webmaster@1: $serialized = 1; webmaster@1: } webmaster@1: $created = time(); webmaster@1: 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: if (!db_affected_rows()) { webmaster@1: @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: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * webmaster@1: * Expire data from the cache. If called without arguments, expirable webmaster@1: * entries will be cleared from the cache_page and cache_block tables. webmaster@1: * webmaster@1: * @param $cid webmaster@1: * If set, the cache ID to delete. Otherwise, all cache entries that can webmaster@1: * expire are deleted. webmaster@1: * webmaster@1: * @param $table webmaster@1: * If set, the table $table to delete from. Mandatory webmaster@1: * argument if $cid is set. webmaster@1: * webmaster@1: * @param $wildcard webmaster@1: * If set to TRUE, the $cid is treated as a substring webmaster@1: * to match rather than a complete ID. The match is a right hand webmaster@1: * match. If '*' is given as $cid, the table $table will be emptied. webmaster@1: */ webmaster@1: function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) { webmaster@1: global $user; webmaster@1: webmaster@1: if (!isset($cid) && !isset($table)) { webmaster@1: // Clear the block cache first, so stale data will webmaster@1: // not end up in the page cache. webmaster@1: cache_clear_all(NULL, 'cache_block'); webmaster@1: cache_clear_all(NULL, 'cache_page'); webmaster@1: return; webmaster@1: } webmaster@1: webmaster@1: if (empty($cid)) { webmaster@1: if (variable_get('cache_lifetime', 0)) { webmaster@1: // We store the time in the current user's $user->cache variable which webmaster@1: // will be saved into the sessions table by sess_write(). We then webmaster@1: // simulate that the cache was flushed for this user by not returning webmaster@1: // cached data that was cached before the timestamp. webmaster@1: $user->cache = time(); webmaster@1: webmaster@1: $cache_flush = variable_get('cache_flush', 0); webmaster@1: if ($cache_flush == 0) { webmaster@1: // This is the first request to clear the cache, start a timer. webmaster@1: variable_set('cache_flush', time()); webmaster@1: } webmaster@1: else if (time() > ($cache_flush + variable_get('cache_lifetime', 0))) { webmaster@1: // Clear the cache for everyone, cache_flush_delay seconds have webmaster@1: // passed since the first request to clear the cache. webmaster@1: db_query("DELETE FROM {". $table ."} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time()); webmaster@1: variable_set('cache_flush', 0); webmaster@1: } webmaster@1: } webmaster@1: else { webmaster@1: // No minimum cache lifetime, flush all temporary cache entries now. webmaster@1: db_query("DELETE FROM {". $table ."} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time()); webmaster@1: } webmaster@1: } webmaster@1: else { webmaster@1: if ($wildcard) { webmaster@1: if ($cid == '*') { webmaster@1: db_query("DELETE FROM {". $table ."}"); webmaster@1: } webmaster@1: else { webmaster@1: db_query("DELETE FROM {". $table ."} WHERE cid LIKE '%s%%'", $cid); webmaster@1: } webmaster@1: } webmaster@1: else { webmaster@1: db_query("DELETE FROM {". $table ."} WHERE cid = '%s'", $cid); webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: