Code coverage for /20081101/includes/cache.inc

Line #Times calledCode
1
<?php
2
// $Id: cache.inc,v 1.27 2008/10/12 04:30:05 webchick 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
 */
172367
function cache_get($cid, $table = 'cache') {
182498
  global $user;
19
20
  // Garbage collection necessary when enforcing a minimum cache lifetime
212498
  $cache_flush = variable_get('cache_flush', 0);
222498
  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.
240
    variable_set('cache_flush', 0);
25
    // Time to flush old cache data
260
    db_delete($table)
270
      ->condition('expire', CACHE_PERMANENT, '<>')
280
      ->condition('expire', $cache_flush, '<=')
290
      ->execute();
300
  }
31
322498
  $cache = db_query("SELECT data, created, headers, expire, serialized FROM
{" . $table . "} WHERE cid = :cid", array(':cid' => $cid))->fetchObject();
332498
  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.
362498
    if ($cache->expire == CACHE_PERMANENT ||
!variable_get('cache_lifetime', 0)) {
372498
      if ($cache->serialized) {
382498
        $cache->data = unserialize($cache->data);
392498
      }
402498
    }
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 {
470
      if ($user->cache > $cache->created) {
48
        // This cache data is too old and thus not valid for us, ignore it.
490
        return FALSE;
500
      }
51
      else {
520
        if ($cache->serialized) {
530
          $cache->data = unserialize($cache->data);
540
        }
55
      }
56
    }
572498
    return $cache;
580
  }
591676
  return FALSE;
600
}
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
 */
1072367
function cache_set($cid, $data, $table = 'cache', $expire =
CACHE_PERMANENT, $headers = NULL) {
108
  $fields = array(
1091564
    'serialized' => 0,
1101564
    'created' => REQUEST_TIME,
1111564
    'expire' => $expire,
1121564
    'headers' => $headers,
1131564
  );
1141564
  if (!is_string($data)) {
115931
    $fields['data'] = serialize($data);
116931
    $fields['serialized'] = 1;
117931
  }
118
  else {
1191411
    $fields['data'] = $data;
1201411
    $fields['serialized'] = 0;
121
  }
122
1231564
  db_merge($table)
1241564
    ->key(array('cid' => $cid))
1251564
    ->fields($fields)
1261564
    ->execute();
1271564
}
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
 */
1472367
function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
148718
  global $user;
149
150718
  if (!isset($cid) && !isset($table)) {
151
    // Clear the block cache first, so stale data will
152
    // not end up in the page cache.
153359
    cache_clear_all(NULL, 'cache_block');
154359
    cache_clear_all(NULL, 'cache_page');
155359
    return;
1560
  }
157
158718
  if (empty($cid)) {
159360
    if (variable_get('cache_lifetime', 0)) {
160
      // We store the time in the current user's $user->cache variable
which
161
      // will be saved into the sessions table by _sess_write(). We then
162
      // simulate that the cache was flushed for this user by not returning
163
      // cached data that was cached before the timestamp.
1640
      $user->cache = REQUEST_TIME;
165
1660
      $cache_flush = variable_get('cache_flush', 0);
1670
      if ($cache_flush == 0) {
168
        // This is the first request to clear the cache, start a timer.
1690
        variable_set('cache_flush', REQUEST_TIME);
1700
      }
1710
      elseif (REQUEST_TIME > ($cache_flush + variable_get('cache_lifetime',
0))) {
172
        // Clear the cache for everyone, cache_flush_delay seconds have
173
        // passed since the first request to clear the cache.
1740
        db_delete($table)
1750
          ->condition('expire', CACHE_PERMANENT, '<>')
1760
          ->condition('expire', REQUEST_TIME, '<')
1770
          ->execute();
1780
        variable_set('cache_flush', 0);
1790
      }
1800
    }
181
    else {
182
      // No minimum cache lifetime, flush all temporary cache entries now.
183360
      db_delete($table)
184360
        ->condition('expire', CACHE_PERMANENT, '<>')
185360
        ->condition('expire', REQUEST_TIME, '<')
186360
        ->execute();
187
    }
188360
  }
189
  else {
190694
    if ($wildcard) {
191216
      if ($cid == '*') {
192161
        db_delete($table)->execute();
193161
      }
194
      else {
195202
        db_delete($table)
196202
          ->condition('cid', $cid . '%', 'LIKE')
197202
          ->execute();
198
      }
199216
    }
200
    else {
201694
      db_delete($table)
202694
        ->condition('cid', $cid)
203694
        ->execute();
204
    }
205
  }
206718
}
2072367