You are here

function dmemcache_set in Memcache API and Integration 7

Same name and namespace in other branches
  1. 5.2 dmemcache.inc \dmemcache_set()
  2. 5 dmemcache.inc \dmemcache_set()
  3. 6 dmemcache.inc \dmemcache_set()

Place an item into memcache.

Parameters

string $key: The string with which you will retrieve this item later.

mixed $value: The item to be stored.

int $exp: Parameter expire is expiration time in seconds. If it's 0, the item never expires (but memcached server doesn't guarantee this item to be stored all the time, it could be deleted from the cache to make place for other items).

string $bin: The name of the Drupal subsystem that is making this call. Examples could be 'cache', 'alias', 'taxonomy term' etc. It is possible to map different $bin values to different memcache servers.

object $mc: Optionally pass in the memcache object. Normally this value is determined automatically based on the bin the object is being stored to.

Return value

bool TRUE on succes, FALSE otherwise.

7 calls to dmemcache_set()
dmemcache_piece_cache_get in ./dmemcache.inc
Determine if a key has multi-piece values.
dmemcache_piece_cache_set in ./dmemcache.inc
Track active keys with multi-piece values, necessary for efficient cleanup.
lock_acquire in ./memcache-lock-code.inc
Acquire (or renew) a lock, but do not block if it fails.
MemCacheDrupal::set in ./memcache.inc
Implements DrupalCacheInterface::set().
MemCacheDrupal::wildcards in ./memcache.inc
Retrieves all matching wildcards for the given cache id.

... See full list

1 string reference to 'dmemcache_set'
memcache_requirements in ./memcache.install
Implements hook_requirements().

File

./dmemcache.inc, line 45
A memcache API for Drupal.

Code

function dmemcache_set($key, $value, $exp = 0, $bin = 'cache', $mc = NULL) {
  $collect_stats = dmemcache_stats_init();
  $full_key = dmemcache_key($key, $bin);
  $rc = FALSE;
  if ($mc || ($mc = dmemcache_object($bin))) {
    if ($mc instanceof Memcached) {
      $rc = $mc
        ->set($full_key, $value, $exp);
      if (empty($rc)) {
        if ($mc
          ->getResultCode() == MEMCACHED_CLIENT_ERROR && _dmemcache_ensure_ascii_auth($full_key, $mc)) {
          $rc = $mc
            ->set($full_key, $value, $exp);
        }

        // If there was a MEMCACHED_E2BIG error, split the value into pieces
        // and cache them individually.
        if ($mc
          ->getResultCode() == MEMCACHED_E2BIG) {
          $rc = _dmemcache_set_pieces($key, $value, $exp, $bin, $mc);
        }
      }
    }
    else {

      // The PECL Memcache library throws an E_NOTICE level error, which
      // $php_errormsg doesn't catch, so we need to log it ourselves.
      // Catch it with our own error handler.
      drupal_static_reset('_dmemcache_error_handler');
      set_error_handler('_dmemcache_error_handler');
      $rc = $mc
        ->set($full_key, $value, MEMCACHE_COMPRESSED, $exp);

      // Restore the Drupal error handler.
      restore_error_handler();
      if (empty($rc)) {

        // If the object was too big, split the value into pieces and cache
        // them individually.
        $dmemcache_errormsg =& drupal_static('_dmemcache_error_handler');
        if (!empty($dmemcache_errormsg) && (strpos($dmemcache_errormsg, 'SERVER_ERROR object too large for cache') !== FALSE || strpos($dmemcache_errormsg, 'SERVER_ERROR out of memory storing object') !== FALSE)) {
          $rc = _dmemcache_set_pieces($key, $value, $exp, $bin, $mc);
        }
      }
    }
  }
  if ($collect_stats) {
    dmemcache_stats_write('set', $bin, array(
      $full_key => $rc,
    ));
  }
  _dmemcache_write_debug('set', $bin, $full_key, $rc);
  return $rc;
}