memcache.inc 10.6 KB
Newer Older
1
<?php
robertDouglass's avatar
robertDouglass committed
2
// $Id$
3

4
require_once 'dmemcache.inc';
5

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

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

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

    $this->wildcard_timestamps = variable_get('memcache_wildcard_timestamps', 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->flushed = min($this->cache_flush, REQUEST_TIME - $this->cache_lifetime);
23 24
  }
  function get($cid) {
25 26 27 28 29
    $cache = dmemcache_get($cid, $this->bin, $this->memcache);
    return $this->valid($cid, $cache) ? $cache : FALSE;
  }

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

  protected function valid($cid, $cache) {
46
    if (!isset($cache) || !is_object($cache)) {
47
      return FALSE;
Jeremy's avatar
Jeremy committed
48
    }
49

50 51 52 53 54 55
    // wildcard_valid() has some overhead due to hashing cids and a
    // dmemcache_get_multi() to fetch the flushes. Since some bins never
    // have wildcard clears with a cid, we can shortcut these checks.
    if (!empty($this->wildcard_timestamps[$this->bin]) &&
        $this->wildcard_timestamps[$this->bin] >= (REQUEST_TIME - $this->invalidate) &&
        !$this->wildcard_valid($cid, $cache))  {
56 57 58
      return FALSE;
    }

59
    // Determine when the current bin was last flushed.
60
    $item_flushed_globally = $cache->created && $this->cache_flush && $this->cache_lifetime && ($cache->created < $this->flushed);
61 62 63

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

64
    $item_flushed_for_user = !empty($cache_bins) && isset($cache_bins[$this->bin]) && ($cache->created < $cache_bins[$this->bin]);
65 66
    if ($item_flushed_for_user) {
      return FALSE;
67
    }
68 69 70 71 72 73 74 75 76 77 78 79 80

    // 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.
81 82 83 84 85 86 87 88 89 90 91
    $item_expired = isset($cache->expire) && $cache->expire !== CACHE_PERMANENT && $cache->expire <= REQUEST_TIME;
    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.
      if ((REQUEST_TIME - $cache->expire) >= variable_get('memcache_max_staleness', 1800) ||
          dmemcache_add($cid . '_semaphore', '', variable_get('memcache_stampede_semaphore', 15), $this->bin)) {
        return FALSE;
      }
    }
    return TRUE;
92
  }
Steve Rude's avatar
Steve Rude committed
93

94
  function set($cid, $data, $expire = CACHE_PERMANENT, array $headers = NULL) {
95
    $created = REQUEST_TIME;
96

97 98 99 100 101 102
    // Create new cache object.
    $cache = new stdClass;
    $cache->cid = $cid;
    $cache->data = is_object($data) ? clone $data : $data;
    $cache->created = $created;
    $cache->headers = $headers;
103 104
    // Record the previous number of wildcard flushes affecting our cid.
    $cache->flushes = $this->wildcard_flushes($cid);
105 106 107
    if ($expire == CACHE_TEMPORARY) {
      // Convert CACHE_TEMPORARY (-1) into something that will live in memcache
      // until the next flush.
108
      $cache->expire = REQUEST_TIME + 2591999;
109 110 111 112 113
    }
    // 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().
114
      $cache->expire = REQUEST_TIME + $expire;
115 116 117 118
    }
    else {
      $cache->expire = $expire;
    }
119

120 121 122 123 124
    // We manually track the expire time in $cache->expire.  When the object
    // 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.
    dmemcache_set($cid, $cache, 0, $this->bin, $this->memcache);
125
  }
126 127

  function clear($cid = NULL, $wildcard = FALSE) {
Jeremy's avatar
Jeremy committed
128

129
    if (empty($cid) || $wildcard === TRUE) {
130 131 132 133
      if ($cid == '*') {
        $cid = '';
      }
      if (variable_get('cache_lifetime', 0) && empty($cid)) {
134 135 136 137
        // 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.
138
        variable_set("cache_flush_$this->bin", REQUEST_TIME);
139 140 141 142 143

        // 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.
144
        if (isset($_SESSION['cache_flush']) && is_array($_SESSION['cache_flush'])) {
145 146 147 148 149
          $cache_bins = $_SESSION['cache_flush'];
        }
        else {
          $cache_bins = array();
        }
150
        $cache_bins[$this->bin] = REQUEST_TIME;
151 152 153
        $_SESSION['cache_flush'] = $cache_bins;
      }
      else {
154 155
        // Register a wildcard flush for current cid
        $this->wildcards($cid, TRUE);
156 157 158 159 160 161
      }
    }
    else {
      $cids = is_array($cid) ? $cid : array($cid);
      foreach ($cids as $cid) {
        dmemcache_delete($cid, $this->bin, $this->memcache);
162 163 164
      }
    }
  }
165

166 167 168 169 170 171 172 173
  /**
   * We hash cids to keep them a consistent, managable length.  Alternative algorithms
   * can be specified if you're looking for better performance (benchmark first!).
   * Hash collissions are not a big deal, simply leads to all collided items being
   * flushed together.
   */
  function hash_cid($cid) {
    static $hashes = array();
174 175 176 177 178 179 180
    $memcache_hash = variable_get('memcache_hash', 'md5');
    if (function_exists($memcache_hash)) {
      $hashes[$cid] = $memcache_hash($cid);
    }
    else {
      $hashes[$cid] = $cid;
    }
181 182 183 184 185 186 187 188 189
    return $hashes[$cid];
  }

  /**
   * Determine all possible hashes that could match our cid.  We optimize away
   * the overhead of checking all possible matches by using multiget.
   */
  private function multihash_cid($cid) {
    static $hashes = array();
190 191 192 193
    if (!isset($hashes[$this->bin])) {
      $hashes[$this->bin] = array();
    }
    if (!isset($hashes[$this->bin][$cid])) {
194 195
      for ($i = 0; $i <= strlen($cid); $i++) {
        $subcid = substr($cid, 0, $i);
196
        $hashes[$this->bin][$cid][$subcid] = '.wildcard-'. $this->bin . $this->hash_cid($subcid);
197 198
      }
    }
199
    return $hashes[$this->bin][$cid];
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  }

  /**
   * 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.
   */
  private function wildcard_flushes($cid) {
    return array_sum($this->wildcards($cid));
  }

  /**
   * 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.
   */
  private function wildcards($cid, $flush = FALSE) {
    static $wildcards = array();
218 219 220 221 222 223 224 225 226

    // If this bin has never had a wildcard flush, or the last wildcard flush
    // was more than a month ago, simply return an empty array to indicate that
    // it has never been flushed and to avoid the overhead of a wildcard lookup.
    if (!$flush && (!isset($this->wildcard_timestamps[$this->bin]) ||
        $this->wildcard_timestamps[$this->bin] <= (REQUEST_TIME - $this->invalidate))) {
      return array();
    }

227
    if (!isset($wildcards[$this->bin]) || !isset($wildcards[$this->bin][$cid])) {
228 229 230 231 232 233 234
      $multihash = $this->multihash_cid($cid);
      $wildcards[$this->bin][$cid] = dmemcache_get_multi($multihash, $this->bin);
      if (!is_array($wildcards[$this->bin][$cid])) {
        $wildcards[$this->bin][$cid] = array();
      }
    }
    if ($flush) {
235 236 237 238 239 240 241 242 243 244
      if (!empty($cid)) {
        // Avoid too many variable_set() by only recording a flush for
        // a fraction of the wildcard invalidation variable. Defaults to
        // 28 / 4 = one week.
        if (!isset($this->wildcard_timestamps[$this->bin]) ||
            (REQUEST_TIME - $this->wildcard_timestamps[$this->bin] > $this->invalidate / 4)) {
          $this->wildcard_timestamps[$this->bin] = REQUEST_TIME;
          variable_set('memcache_wildcard_timestamps', $this->wildcard_timestamps);
        }
      }
245 246 247 248 249 250 251
      $hash = $this->hash_cid($cid);
      $wildcard = dmemcache_key('.wildcard-' . $this->bin . $hash, $this->bin);
      if (isset($wildcards[$this->bin][$cid][$wildcard])) {
        $mc = dmemcache_object($this->bin);
        $mc->increment($wildcard);
      }
      else {
Jeremy's avatar
Jeremy committed
252
        dmemcache_set('.wildcard-' . $this->bin . $hash, '1', 0, $this->bin);
253
      }
Jeremy's avatar
Jeremy committed
254 255 256
      // A wildcard flush may invalidate anything in our static cache, so we
      // dump it and rebuild it later if necessary.
      unset($wildcards[$this->bin]);
257 258 259 260 261 262 263 264
    }
    return $wildcards[$this->bin][$cid];
  }

  /**
   * Check if a wildcard flush has invalidated the current cached copy.
   */
  private function wildcard_valid($cid, $cache) {
265 266 267 268
    // 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)) {
269 270 271 272 273
      return FALSE;
    }
    return TRUE;
  }

274 275 276
  function isEmpty() {
    // We do not know so err on the safe side?
    return FALSE;
277 278
  }
}
279