You are here

function dmemcache_add in Memcache API and Integration 7

Same name and namespace in other branches
  1. 6 dmemcache.inc \dmemcache_add()

Add 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: (optional) 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: (optional) 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: (optional) The memcache object. Normally this value is determined automatically based on the bin the object is being stored to.

bool $flag: (optional) If using the older memcache PECL extension as opposed to the newer memcached PECL extension, the MEMCACHE_COMPRESSED flag can be set to use zlib to store a compressed copy of the item. This flag option is completely ignored when using the newer memcached PECL extension.

Return value

bool FALSE if placing the item into memcache failed.

1 call to dmemcache_add()
lock_acquire in ./memcache-lock-code.inc
Acquire (or renew) a lock, but do not block if it fails.

File

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

Code

function dmemcache_add($key, $value, $exp = 0, $bin = 'cache', $mc = NULL, $flag = FALSE) {
  $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
        ->add($full_key, $value, $exp);
      if (empty($rc) && $mc
        ->getResultCode() == MEMCACHED_CLIENT_ERROR && _dmemcache_ensure_ascii_auth($full_key, $mc)) {
        $rc = $mc
          ->add($full_key, $value, $exp);
      }
    }
    else {
      $rc = $mc
        ->add($full_key, $value, $flag, $exp);
    }
  }
  if ($collect_stats) {
    dmemcache_stats_write('add', $bin, array(
      $full_key => $rc,
    ));
  }
  _dmemcache_write_debug('add', $bin, $full_key, $rc);
  return $rc;
}