You are here

public function MemCacheDrupal::variable_set in Memcache API and Integration 7

Re-implementation of variable_set() that writes through instead of clearing.

2 calls to MemCacheDrupal::variable_set()
MemCacheDrupal::clear in ./memcache.inc
Implements DrupalCacheInterface::clear().
MemCacheDrupal::wildcards in ./memcache.inc
Retrieves all matching wildcards for the given cache id.

File

./memcache.inc, line 649

Class

MemCacheDrupal
Implementation of cache.inc with memcache logic included

Code

public function variable_set($name, $value) {
  global $conf;

  // If the value appears unchanged, do nothing.
  if (isset($conf[$name]) && $conf[$name] === $value) {
    return;
  }

  // When lots of writes happen in a short period of time db_merge can throw
  // errors. This should only happen if another request has written the
  // variable first, so we catch the error to prevent a fatal error.
  try {
    db_merge('variable')
      ->key(array(
      'name' => $name,
    ))
      ->fields(array(
      'value' => serialize($value),
    ))
      ->execute();
  } catch (Exception $e) {

    // We can safely ignore the error, since it's likely a cache flush
    // timestamp which should still be accurate.
  }

  // If the variables are cached, get a fresh copy, update with the new value
  // and set it again.
  if ($cached = cache_get('variables', 'cache_bootstrap')) {
    $variables = $cached->data;
    $variables[$name] = $value;
    cache_set('variables', $variables, 'cache_bootstrap');
  }

  // If the variables aren't cached, there's no need to do anything.
  $conf[$name] = $value;
}