memcache.inc 14.5 KB
Newer Older
1 2
<?php

3
require_once dirname(__FILE__) . '/dmemcache.inc';
4

5 6 7 8 9
/**
 * Defines the period after which wildcard clears are not considered valid.
 */
define('MEMCACHE_WILDCARD_INVALIDATE', 86400 * 28);

10 11
define('MEMCACHE_CONTENT_CLEAR', 'MEMCACHE_CONTENT_CLEAR');

12 13
/** Implementation of cache.inc with memcache logic included **/

14 15 16 17
class MemCacheDrupal implements DrupalCacheInterface {
  function __construct($bin) {
    $this->memcache = dmemcache_object($bin);
    $this->bin = $bin;
18

19
    $this->reloadVariables();
20
  }
21

22
  function get($cid) {
23 24 25 26 27
    $cache = dmemcache_get($cid, $this->bin, $this->memcache);
    return $this->valid($cid, $cache) ? $cache : FALSE;
  }

  function getMultiple(&$cids) {
28
    $results = dmemcache_get_multi($cids, $this->bin, $this->memcache);
29
    foreach ($results as $cid => $result) {
30
      if (!$this->valid($result->cid, $result)) {
31
        // This object has expired, so don't return it.
32 33 34
        unset($results[$cid]);
      }
    }
35 36 37
    // Remove items from the referenced $cids array that we are returning,
    // per the comment in cache_get_multiple() in includes/cache.inc.
    $cids = array_diff($cids, array_keys($results));
38 39 40 41
    return $results;
  }

  protected function valid($cid, $cache) {
42
    if (!isset($cache) || !is_object($cache)) {
43
      return FALSE;
Jeremy's avatar
Jeremy committed
44
    }
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    if ($cache->created <= $this->cache_flush) {
      return FALSE;
    }

    if ($cache->expire != CACHE_PERMANENT && $cache->created + $this->cache_lifetime <= $this->cache_content_flush) {
      return FALSE;
    }

    $cache_tables = isset($_SESSION['cache_flush']) ? $_SESSION['cache_flush'] : NULL;

    // Items cached before the cache was last flushed by the current user are no
    // longer valid.
    if ($cache->expire != CACHE_PERMANENT && is_array($cache_tables) && isset($cache_tables[$this->bin]) && $cache_tables[$this->bin] >= $cache->created) {
      // Cache item expired, return FALSE.
      return FALSE;
    }

    if (is_array($cache_tables) && !empty($cache_tables) && $this->cache_lifetime) {
      // Expire the $_SESSION['cache_flush'] variable array if it is older than
      // the minimum cache lifetime, since after that the $cache_flush variable
      // will take over.
      if (max($cache_tables) < ($_SERVER['REQUEST_TIME'] - $this->cache_lifetime)) {
        unset($_SESSION['cache_flush']);
        $cache_tables = NULL;
      }
    }

    if (!$this->wildcard_valid($cid, $cache))  {
74 75 76
      return FALSE;
    }

77
    // Determine when the current bin was last flushed.
78 79 80
    $item_flushed_globally = $cache->created && $this->cache_flush &&
                             $this->cache_lifetime &&
                             ($cache->created <= $this->flushed);
81

82 83
    // Look in the session to determine if this item was flushed for the
    // current user (ie, if they posted a comment and are viewing a cached page)
84
    $cache_bins = isset($_SESSION['cache_flush']) ? $_SESSION['cache_flush'] : NULL;
85 86 87
    $item_flushed_for_user = !empty($cache_bins) &&
                             isset($cache_bins[$this->bin]) &&
                             ($cache->created < $cache_bins[$this->bin]);
88 89
    if ($item_flushed_for_user) {
      return FALSE;
90
    }
91 92 93 94 95 96 97 98 99 100 101 102 103

    // The item can be expired if:
    // - A liftetime is set and the item is older than both the lifetime and
    //   the global flush.
    // - The item has been create before the bin was flushed for this user.
    // - The item could simply expire.
    //
    // For the two global cases we try and grab a lock.  If we get the lock, we
    // return FALSE instead of the cached object which should cause it to be
    // rebuilt.  If we do not get the lock, we return the cached object despite
    // it's expired  The goal here is to avoid cache stampedes.
    // By default the cache stampede semaphore is held for 15 seconds.  This
    // can be adjusted by setting the memcache_stampede_semaphore variable.
104 105
    $item_expired = isset($cache->expire) &&
                    $cache->expire !== CACHE_PERMANENT &&
106
                    $cache->expire <= time();
107 108 109 110
    if ($item_flushed_globally || $item_expired) {
      // To avoid a stampede, return TRUE despite the item being expired if
      // a previous process set the stampede semaphore already. However only
      // do this if the data is less than 30 minutes stale.
111
      if ((time() - $cache->expire) >= variable_get('memcache_max_staleness', 1800) ||
112 113 114 115 116
          dmemcache_add($cid . '_semaphore', '', variable_get('memcache_stampede_semaphore', 15), $this->bin)) {
        return FALSE;
      }
    }
    return TRUE;
117
  }
Steve Rude's avatar
Steve Rude committed
118

119
  function set($cid, $data, $expire = CACHE_PERMANENT, array $headers = NULL) {
120
    $created = time();
121

122 123 124 125 126 127
    // Create new cache object.
    $cache = new stdClass;
    $cache->cid = $cid;
    $cache->data = is_object($data) ? clone $data : $data;
    $cache->created = $created;
    $cache->headers = $headers;
128 129
    // Record the previous number of wildcard flushes affecting our cid.
    $cache->flushes = $this->wildcard_flushes($cid);
130 131 132
    if ($expire == CACHE_TEMPORARY) {
      // Convert CACHE_TEMPORARY (-1) into something that will live in memcache
      // until the next flush.
133
      $cache->expire = REQUEST_TIME + 2591999;
134 135 136 137 138
    }
    // Expire time is in seconds if less than 30 days, otherwise is a timestamp.
    else if ($expire != CACHE_PERMANENT && $expire < 2592000) {
      // Expire is expressed in seconds, convert to the proper future timestamp
      // as expected in dmemcache_get().
139
      $cache->expire = REQUEST_TIME + $expire;
140 141 142 143
    }
    else {
      $cache->expire = $expire;
    }
144

145
    // We manually track the expire time in $cache->expire.  When the object
146 147 148
    // expires, we only allow one request to rebuild it to avoid cache
    // stampedes. Other requests for the expired object while it is still being
    // rebuilt get the expired object.
149
    dmemcache_set($cid, $cache, 0, $this->bin, $this->memcache);
150
  }
151 152

  function clear($cid = NULL, $wildcard = FALSE) {
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

    // It is not possible to detect a cache_clear_all() call other than looking
    // at the backtrace unless http://drupal.org/node/81461 is added.
    $backtrace = debug_backtrace();
    if ($cid == MEMCACHE_CONTENT_CLEAR || (isset($backtrace[2]) && $backtrace[2]['function'] == 'cache_clear_all' && empty($backtrace[2]['args']))) {
      // Update the timestamp of the last global flushing of this table.  When
      // retrieving data from this table, we will compare the cache creation
      // time minus the cache_flush time to the cache_lifetime to determine
      // whether or not the cached item is still valid.
      $this->cache_content_flush = time();
      $this->variable_set('cache_content_flush_' . $this->bin, $this->cache_content_flush);
      if (variable_get('cache_lifetime', 0)) {
        // We store the time in the current user's session which is saved into
        // the sessions table by sess_write().  We then simulate that the cache
        // was flushed for this user by not returning cached data to this user
        // that was cached before the timestamp.
        if (isset($_SESSION['cache_flush']) && is_array($_SESSION['cache_flush'])) {
          $cache_bins = $_SESSION['cache_flush'];
        }
        else {
          $cache_bins = array();
        }
        // Use time() rather than request time here for correctness.
        $cache_tables[$this->bin] = $this->cache_content_flush;
        $_SESSION['cache_flush'] = $cache_tables;
      }
    }
180
    if (empty($cid) || $wildcard === TRUE) {
181 182 183 184 185 186 187 188
      // system_cron() flushes all cache bins returned by hook_flush_caches()
      // with cache_clear_all(NULL, $bin); This is for garbage collection with
      // the database cache, but serves no purpose with memcache. So return
      // early here.
      if (!isset($cid)) {
        return;
      }
      elseif ($cid == '*') {
189 190
        $cid = '';
      }
191
      if (empty($cid)) {
192 193 194 195
        // Update the timestamp of the last global flushing of this bin.  When
        // retrieving data from this bin, we will compare the cache creation
        // time minus the cache_flush time to the cache_lifetime to determine
        // whether or not the cached item is still valid.
196 197 198
        $this->cache_flush = time();
        $this->variable_set("cache_flush_$this->bin", $this->cache_flush);
        $this->flushed = min($this->cache_flush, time() - $this->cache_lifetime);
199

200 201 202 203 204 205 206 207 208 209 210 211 212
        if ($this->cache_lifetime) {
          // We store the time in the current user's session which is saved into
          // the sessions table by sess_write().  We then simulate that the cache
          // was flushed for this user by not returning cached data to this user
          // that was cached before the timestamp.
          if (isset($_SESSION['cache_flush']) && is_array($_SESSION['cache_flush'])) {
            $cache_bins = $_SESSION['cache_flush'];
          }
          else {
            $cache_bins = array();
          }
          $cache_bins[$this->bin] = $this->cache_flush;
          $_SESSION['cache_flush'] = $cache_bins;
213 214 215
        }
      }
      else {
216 217
        // Register a wildcard flush for current cid
        $this->wildcards($cid, TRUE);
218 219 220 221 222 223
      }
    }
    else {
      $cids = is_array($cid) ? $cid : array($cid);
      foreach ($cids as $cid) {
        dmemcache_delete($cid, $this->bin, $this->memcache);
224 225 226
      }
    }
  }
227

228
  /**
229 230 231
   * Sum of all matching wildcards.  Checking any single cache item's flush
   * value against this single-value sum tells us whether or not a new wildcard
   * flush has affected the cached item.
232 233 234 235 236 237
   */
  private function wildcard_flushes($cid) {
    return array_sum($this->wildcards($cid));
  }

  /**
238 239 240
   * Utilize multiget to retrieve all possible wildcard matches, storing
   * statically so multiple cache requests for the same item on the same page
   * load doesn't add overhead.
241 242 243
   */
  private function wildcards($cid, $flush = FALSE) {
    static $wildcards = array();
244
    $matching = array();
245

246 247
    $length = strlen($cid);

248 249
    if (isset($this->wildcard_flushes[$this->bin]) &&
      is_array($this->wildcard_flushes[$this->bin])) {
250 251 252 253 254 255 256
      // Determine which lookups we need to perform to determine whether or not
      // our cid was impacted by a wildcard flush.
      $lookup = array();

      // Find statically cached wildcards, and determine possibly matching
      // wildcards for this cid based on a history of the lengths of past
      // valid wildcard flushes in this bin.
257 258 259
      foreach ($this->wildcard_flushes[$this->bin] as $flush_length => $timestamp) {
        if ($length >= $flush_length && $timestamp >= ($_SERVER['REQUEST_TIME'] - $this->invalidate)) {
          $key = '.wildcard-' . substr($cid, 0, $flush_length);
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
          $wildcard = dmemcache_key($key, $this->bin);
          if (isset($wildcards[$this->bin][$wildcard])) {
            $matching[$wildcard] = $wildcards[$this->bin][$wildcard];
          }
          else {
            $lookup[$wildcard] = $key;
          }
        }
      }

      // Do a multi-get to retrieve all possibly matching wildcard flushes.
      if (!empty($lookup)) {
        $values = dmemcache_get_multi($lookup, $this->bin, $this->memcache);
        if (is_array($values)) {
          // Prepare an array of matching wildcards.
          $matching = array_merge($matching, $values);
          // Store matches in the static cache.
          if (isset($wildcards[$this->bin])) {
            $wildcards[$this->bin] = array_merge($wildcards[$this->bin], $values);
          }
          else {
            $wildcards[$this->bin] = $values;
          }
          $lookup = array_diff_key($lookup, $values);
        }
285

286 287 288 289 290
        // Also store failed lookups in our static cache, so we don't have to
        // do repeat lookups on single page loads.
        foreach ($lookup as $wildcard => $key) {
          $wildcards[$this->bin][$wildcard] = 0;
        }
291 292 293
      }
    }
    if ($flush) {
294 295 296 297
      // Avoid too many calls to variable_set() by only recording a flush for
      // a fraction of the wildcard invalidation variable, per cid length.
      // Defaults to 28 / 4, or one week.
      $length = strlen($cid);
298 299 300 301 302
      if (!isset($this->wildcard_flushes[$this->bin][$length]) ||
          ($_SERVER['REQUEST_TIME'] - $this->wildcard_flushes[$this->bin][$length] > $this->invalidate / 4)) {
        $this->wildcard_flushes = variable_get('memcache_wildcard_flushes', array());
        $this->wildcard_flushes[$this->bin][$length] = $_SERVER['REQUEST_TIME'];
        variable_set('memcache_wildcard_flushes', $this->wildcard_flushes);
303
      }
304 305 306 307
      $wildcard = dmemcache_key('.wildcard-' . $cid, $this->bin);
      if (isset($wildcards[$this->bin][$wildcard]) && $wildcards[$this->bin][$wildcard] != 0) {
        $this->memcache->increment($wildcard);
        $wildcards[$this->bin][$wildcard]++;
308 309
      }
      else {
310 311
        $wildcards[$this->bin][$wildcard] = 1;
        dmemcache_set('.wildcard-' . $cid, '1', 0, $this->bin);
312 313
      }
    }
314
    return $matching;
315 316 317 318 319 320
  }

  /**
   * Check if a wildcard flush has invalidated the current cached copy.
   */
  private function wildcard_valid($cid, $cache) {
321 322 323 324
    // Previously cached content won't have ->flushes defined.  We could
    // force flush, but instead leave this up to the site admin.
    $flushes = isset($cache->flushes) ? (int)$cache->flushes : 0;
    if ($flushes < (int)$this->wildcard_flushes($cid)) {
325 326 327 328 329
      return FALSE;
    }
    return TRUE;
  }

330 331 332
  function isEmpty() {
    // We do not know so err on the safe side?
    return FALSE;
333
  }
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

  /**
   * Helper function to reload variables.
   *
   * This is used by the tests to verify that the cache object used the correct
   * settings.
   */
  function reloadVariables() {
    $this->wildcard_flushes = variable_get('memcache_wildcard_flushes', array());
    $this->invalidate = variable_get('memcache_wildcard_invalidate', MEMCACHE_WILDCARD_INVALIDATE);
    $this->cache_lifetime = variable_get('cache_lifetime', 0);
    $this->cache_flush = variable_get('cache_flush_' . $this->bin);
    $this->cache_content_flush = variable_get('cache_content_flush_' . $this->bin, 0);
    $this->flushed = min($this->cache_flush, REQUEST_TIME - $this->cache_lifetime);
  }

  /**
   * Re-implementation of variable_set() that writes through instead of clearing.
   */
  function variable_set($name, $value) {
    global $conf;

    db_merge('variable')
      ->key(array('name' => $name))
      ->fields(array('value' => serialize($value)))
      ->execute();
    // If the variables are cached, get a fresh copy, update with the new value
    // and set it again.
    if ($cached = cache_get('variables', 'cache')) {
      $variables = $cached->data;
      $variables[$name] = $value;
      cache_set('variables', $variables);
    }
    // If the variables aren't cached, there's no need to do anything.
    $conf[$name] = $value;
  }
370
}
371