You are here

protected function MemcacheStorage::validateItem in Memcache Storage 7

Validates cache item. Checks if it is still valid and not expired.

Parameters

$cache: Cache item loaded from memcache pool.

Return value

bool|object Return FALSE if object is not valid. Return cache item otherwise.

1 call to MemcacheStorage::validateItem()
MemcacheStorage::getMultiple in ./memcache_storage.inc
Implements DrupalCacheInterface::getMultiple().

File

./memcache_storage.inc, line 215
Provides class for memcached data handling.

Class

MemcacheStorage
Class handles memcached cache objects.

Code

protected function validateItem($cache) {

  // Check if cache object is valid.
  if (empty($cache->cid) || empty($cache->created) || !isset($cache->expire)) {
    return FALSE;
  }
  foreach ($this->wildcards as $wildcard => $flush_timestamp) {

    // See if wildcard is actual for current cache item.
    if ($cache->created <= $flush_timestamp) {

      // See if current cache id matches wildcard.
      if (strpos($cache->cid, $wildcard) === 0) {

        // Remove the current cache ID from the list of cache IDs in use.
        $this
          ->removeCacheID($cache->cid);

        // Remove expired item from memcache.
        MemcacheStorageAPI::delete($cache->cid, $this
          ->cacheBinName());

        // Return no value from cache.
        return FALSE;
      }
    }
  }

  // For temporary cache we should check last bin flush.
  // If temporary cache was created earlier than last cache flush - cache is invalid.
  if ($cache->expire == CACHE_TEMPORARY) {

    // Timestamp when cache bin was flushed last time.
    $last_bin_flush = variable_get('cache_flush_' . $this->bin, 0);

    // Custom behavior for cached pages.
    if ($this->bin == 'cache_page') {

      // Get timestamp of last page cache flush only if custom expiration is disabled.
      // In other case memcached will handle expiration of this cache itself.
      $custom_expiration = variable_get('memcache_storage_page_cache_custom_expiration', FALSE);
      if ($custom_expiration == FALSE) {

        // We should load variables for page cache earlier.
        // Drupal variables are load only during 4th bootstrap phase,
        // while page cache loads during 2th bootstrap phase.
        $variables = cache_get('variables', 'cache_bootstrap');
        if (!empty($variables->data) && !empty($variables->data['cache_flush_cache_page'])) {
          $last_bin_flush = $variables->data['cache_flush_cache_page'];
        }
      }
    }
    if ($cache->created <= $last_bin_flush) {

      // Remove the current cache ID from the list of cache IDs in use.
      $this
        ->removeCacheID($cache->cid);

      // Remove expired item from memcache.
      MemcacheStorageAPI::delete($cache->cid, $this
        ->cacheBinName());

      // Return no value from cache.
      return FALSE;
    }
  }
  return TRUE;
}