note 90864 modified in function.memcache-increment by danbrown

From: Date: Sat, 19 Jun 2010 14:31:56 +0000
Subject: note 90864 modified in function.memcache-increment by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170305@lists.php.net to get a copy of this message
Instead of checking the value before incrementing, you can simply ADD it instead before incrementing each time. If it's already there, your ADD is ignored, and if it's not there, it's set. If you add($memcacheKey, 0) and then increment($memcacheKey, 1) in that order, you avoid all possible race conditions. If two threads are running this code concurrently, you will always end up with your value being 2 no matter which order the threads execute in. --was-- Instead of checking the value before incrementing, you can simply ADD it instead before incrementing each time. If it's already there, your ADD is ignored, and if it's not there, it's set. If you add($memcacheKey, 0) and then increment($memcacheKey, 1) in that order, you avoid all possible race conditions. If two threads are running this code concurrently, you will always end up with your value being 2 no matter which order the threads execute in. http://php.net/manual/en/function.memcache-increment.php

« previous php.notes (#170305) next »