You are here

function dmemcache_add in Memcache API and Integration 6

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

Add an item into memcache

Parameters

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

$value The item to be stored.:

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

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

$mc: (optional) The memcache object. Normally this value is determined automatically based on the bin the object is being stored to.

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

NOTE: dmemcache_get calls dmemcache_add when grabbing a semaphore. For that reason, dmemcache_add can't call dmemcache_get for any reason or you'll end up in an infinite loop.

Return value

bool

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 216

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);
    }
    elseif ($mc instanceof Memcache) {
      $rc = $mc
        ->add($full_key, $value, $flag, $exp);
    }
  }
  if ($collect_stats) {
    dmemcache_stats_write('add', $bin, array(
      $full_key => $rc,
    ));
  }
  return $rc;
}