Code coverage for /20080809/includes/cache.inc

Line #Times calledCode
1
<?php
2
// $Id: cache.inc,v 1.20 2008/07/02 20:42:25 dries Exp $
3
4
/**
5
 * Return data from the persistent cache. Data may be stored as either
plain text or as serialized data.
6
 * cache_get will automatically return unserialized objects and arrays.
7
 *
8
 * @param $cid
9
 *   The cache ID of the data to retrieve.
10
 * @param $table
11
 *   The table $table to store the data in. Valid core values are
'cache_filter',
12
 *   'cache_menu', 'cache_page', or 'cache' for the default cache.
13
 * @return The cache or FALSE on failure.
14
 */
152027
function cache_get($cid, $table = 'cache') {
162087
  global $user;
17
18
  // Garbage collection necessary when enforcing a minimum cache lifetime
192087
  $cache_flush = variable_get('cache_flush', 0);
202087
  if ($cache_flush && ($cache_flush + variable_get('cache_lifetime', 0) <=
time())) {
21
    // Reset the variable immediately to prevent a meltdown in heavy load
situations.
220
    variable_set('cache_flush', 0);
23
    // Time to flush old cache data
240
    db_query("DELETE FROM {" . $table . "} WHERE expire != %d AND expire <=
%d", CACHE_PERMANENT, $cache_flush);
250
  }
26
272087
  $cache = db_fetch_object(db_query("SELECT data, created, headers, expire,
serialized FROM {" . $table . "} WHERE cid = '%s'", $cid));
282087
  if (isset($cache->data)) {
29
    // If the data is permanent or we're not enforcing a minimum cache
lifetime
30
    // always return the cached data.
312087
    if ($cache->expire == CACHE_PERMANENT ||
!variable_get('cache_lifetime', 0)) {
322087
      $cache->data = db_decode_blob($cache->data);
332087
      if ($cache->serialized) {
342087
        $cache->data = unserialize($cache->data);
352087
      }
362087
    }
37
    // If enforcing a minimum cache lifetime, validate that the data is
38
    // currently valid for this user before we return it by making sure
the
39
    // cache entry was created before the timestamp in the current
session's
40
    // cache timer. The cache variable is loaded into the $user object by
41
    // sess_read() in session.inc.
42
    else {
430
      if ($user->cache > $cache->created) {
44
        // This cache data is too old and thus not valid for us, ignore
it.
450
        return FALSE;
460
      }
47
      else {
480
        $cache->data = db_decode_blob($cache->data);
490
        if ($cache->serialized) {
500
          $cache->data = unserialize($cache->data);
510
        }
52
      }
53
    }
542087
    return $cache;
550
  }
561383
  return FALSE;
570
}
58
59
/**
60
 * Store data in the persistent cache.
61
 *
62
 * The persistent cache is split up into four database
63
 * tables. Contributed modules can add additional tables.
64
 *
65
 * 'cache_page': This table stores generated pages for anonymous
66
 * users. This is the only table affected by the page cache setting on
67
 * the administrator panel.
68
 *
69
 * 'cache_menu': Stores the cachable part of the users' menus.
70
 *
71
 * 'cache_filter': Stores filtered pieces of content. This table is
72
 * periodically cleared of stale entries by cron.
73
 *
74
 * 'cache': Generic cache storage table.
75
 *
76
 * The reasons for having several tables are as follows:
77
 *
78
 * - smaller tables allow for faster selects and inserts
79
 * - we try to put fast changing cache items and rather static
80
 *   ones into different tables. The effect is that only the fast
81
 *   changing tables will need a lot of writes to disk. The more
82
 *   static tables will also be better cachable with MySQL's query cache
83
 *
84
 * @param $cid
85
 *   The cache ID of the data to store.
86
 * @param $data
87
 *   The data to store in the cache. Complex data types will be
automatically serialized before insertion.
88
 *   Strings will be stored as plain text and not serialized.
89
 * @param $table
90
 *   The table $table to store the data in. Valid core values are
'cache_filter',
91
 *   'cache_menu', 'cache_page', or 'cache'.
92
 * @param $expire
93
 *   One of the following values:
94
 *   - CACHE_PERMANENT: Indicates that the item should never be removed
unless
95
 *     explicitly told to using cache_clear_all() with a cache ID.
96
 *   - CACHE_TEMPORARY: Indicates that the item should be removed at the
next
97
 *     general cache wipe.
98
 *   - A Unix timestamp: Indicates that the item should be kept at least
until
99
 *     the given time, after which it behaves like CACHE_TEMPORARY.
100
 * @param $headers
101
 *   A string containing HTTP header information for cached pages.
102
 */
1032027
function cache_set($cid, $data, $table = 'cache', $expire =
CACHE_PERMANENT, $headers = NULL) {
1041295
  $serialized = 0;
1051295
  if (!is_string($data)) {
1061254
    $data = serialize($data);
1071254
    $serialized = 1;
1081254
  }
1091295
  $created = time();
1101295
  db_query("UPDATE {" . $table . "} SET data = %b, created = %d, expire =
%d, headers = '%s', serialized = %d WHERE cid = '%s'", $data, $created,
$expire, $headers, $serialized, $cid);
1111295
  if (!db_affected_rows()) {
112906
    @db_query("INSERT INTO {" . $table . "} (cid, data, created, expire,
headers, serialized) VALUES ('%s', %b, %d, %d, '%s', %d)", $cid, $data,
$created, $expire, $headers, $serialized);
113906
  }
1141295
}
115
116
/**
117
 *
118
 * Expire data from the cache. If called without arguments, expirable
119
 * entries will be cleared from the cache_page and cache_block tables.
120
 *
121
 * @param $cid
122
 *   If set, the cache ID to delete. Otherwise, all cache entries that can
123
 *   expire are deleted.
124
 *
125
 * @param $table
126
 *   If set, the table $table to delete from. Mandatory
127
 *   argument if $cid is set.
128
 *
129
 * @param $wildcard
130
 *   If set to TRUE, the $cid is treated as a substring
131
 *   to match rather than a complete ID. The match is a right hand
132
 *   match. If '*' is given as $cid, the table $table will be emptied.
133
 */
1342027
function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
135567
  global $user;
136
137567
  if (!isset($cid) && !isset($table)) {
138
    // Clear the block cache first, so stale data will
139
    // not end up in the page cache.
140267
    cache_clear_all(NULL, 'cache_block');
141267
    cache_clear_all(NULL, 'cache_page');
142267
    return;
1430
  }
144
145567
  if (empty($cid)) {
146268
    if (variable_get('cache_lifetime', 0)) {
147
      // We store the time in the current user's $user->cache variable
which
148
      // will be saved into the sessions table by sess_write(). We then
149
      // simulate that the cache was flushed for this user by not
returning
150
      // cached data that was cached before the timestamp.
1510
      $user->cache = time();
152
1530
      $cache_flush = variable_get('cache_flush', 0);
1540
      if ($cache_flush == 0) {
155
        // This is the first request to clear the cache, start a timer.
1560
        variable_set('cache_flush', time());
1570
      }
1580
      else if (time() > ($cache_flush + variable_get('cache_lifetime', 0)))
{
159
        // Clear the cache for everyone, cache_flush_delay seconds have
160
        // passed since the first request to clear the cache.
1610
        db_query("DELETE FROM {" . $table . "} WHERE expire != %d AND
expire < %d", CACHE_PERMANENT, time());
1620
        variable_set('cache_flush', 0);
1630
      }
1640
    }
165
    else {
166
      // No minimum cache lifetime, flush all temporary cache entries now.
167268
      db_query("DELETE FROM {" . $table . "} WHERE expire != %d AND expire
< %d", CACHE_PERMANENT, time());
168
    }
169268
  }
170
  else {
171547
    if ($wildcard) {
172136
      if ($cid == '*') {
17384
        db_query("DELETE FROM {" . $table . "}");
17484
      }
175
      else {
176134
        db_query("DELETE FROM {" . $table . "} WHERE cid LIKE '%s%%'",
$cid);
177
      }
178136
    }
179
    else {
180547
      db_query("DELETE FROM {" . $table . "} WHERE cid = '%s'", $cid);
181
    }
182
  }
183567
}
184
1852027