You are here

function dmemcache_set in Memcache API and Integration 6

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

Place an item into memcache

Parameters

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

$value The item to be stored.:

$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).

$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.

Return value

bool

10 calls to dmemcache_set()
cache_set in ./memcache.db.inc
Store data in the persistent cache.
cache_set in ./memcache.inc
Store data in memcache.
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.

... See full list

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

File

./dmemcache.inc, line 34

Code

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

        // 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);
        }
      }
    }
    elseif ($mc instanceof Memcache) {

      // 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.
      dmemcache_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 =& dmemcache_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,
    ));
  }
  return $rc;
}