Definition

cache_get($cid, $table = 'cache')
drupal/includes/cache.inc, line 16

Description

Return data from the persistent cache. Data may be stored as either plain text or as serialized data. cache_get will automatically return unserialized objects and arrays.

Parameters

$cid The cache ID of the data to retrieve.

$table The table $table to store the data in. Valid core values are 'cache_filter', 'cache_menu', 'cache_page', or 'cache' for the default cache.

Return value

The cache or FALSE on failure.

Code

function cache_get($cid, $table = 'cache') {
  global $user;

  // Garbage collection necessary when enforcing a minimum cache lifetime
  $cache_flush = variable_get('cache_flush', 0);
  if ($cache_flush && ($cache_flush + variable_get('cache_lifetime', 0) <= REQUEST_TIME)) {
    // Reset the variable immediately to prevent a meltdown in heavy load situations.
    variable_set('cache_flush', 0);
    // Time to flush old cache data
    db_delete($table)
      ->condition('expire', CACHE_PERMANENT, '<>')
      ->condition('expire', $cache_flush, '<=')
      ->execute();
  }

  $cache = db_query("SELECT data, created, headers, expire, serialized FROM {" . $table . "} WHERE cid = :cid", array(':cid' => $cid))->fetchObject();
  if (isset($cache->data)) {
    // If the data is permanent or we're not enforcing a minimum cache lifetime
    // always return the cached data.
    if ($cache->expire == CACHE_PERMANENT || !variable_get('cache_lifetime', 0)) {
      if ($cache->serialized) {
        $cache->data = unserialize($cache->data);
      }
    }
    // If enforcing a minimum cache lifetime, validate that the data is
    // currently valid for this user before we return it by making sure the
    // cache entry was created before the timestamp in the current session's
    // cache timer. The cache variable is loaded into the $user object by
    // _sess_read() in session.inc.
    else {
      if ($user->cache > $cache->created) {
        // This cache data is too old and thus not valid for us, ignore it.
        return FALSE;
      }
      else {
        if ($cache->serialized) {
          $cache->data = unserialize($cache->data);
        }
      }
    }
    return $cache;
  }
  return FALSE;
}