You are here

function ad_memcache_lock in Advertisement 5.2

Same name and namespace in other branches
  1. 5 cache/memcache/ad_cache_memcache.inc \ad_memcache_lock()
  2. 6 cache/memcache/ad_cache_memcache.inc \ad_memcache_lock()

Get a lock in memcache.

2 calls to ad_memcache_lock()
ad_cache_memcache_sync_ad in cache/memcache/ad_cache_memcache.module
Syncronize counts for given advertisement with database.
ad_memcache_increment_map in cache/memcache/ad_cache_memcache.inc
Update mapping which allows us to quickly find stats in memcache when feeding them into the database.

File

cache/memcache/ad_cache_memcache.inc, line 256

Code

function ad_memcache_lock($key, $wait = TRUE) {
  $loop = 0;
  $lock = FALSE;
  while ($lock == FALSE) {
    $lock = ad_memcache_add("LOCK-{$key}-LOCK", TRUE, AD_MEMCACHE_LOCK_LIMIT);
    if (!$lock && $wait) {
      if ($loop++ > 50) {

        // Hard limit of 5 seconds, after which we fail to grab a lock.
        return FALSE;
      }

      // Wait 1/10th of a second and try again.
      usleep(100000);
    }
    else {
      if (!$lock && !$wait) {
        return FALSE;
      }
    }
  }
  return TRUE;
}