See this simple piece of code in PHP:
//Documentation:
//memcache_set ( string $key , mixed $var [, int $flag [, int $expire ]] )
//memcache_increment ( string $key [, int $value = 1 ] )
//part 1
memcache_set ( 'id' , 1 , 0 , 60 );
//part 2
$id = memcache_increment ( 'id' , 1 );
Now imagine that incremental "part 2" is called from many independent clients, and each gets its own unique identifier.
Qestion: how to extend id value while maintaining value consistency? Keep in mind that every time a client can increase the value.
Some ideas on how to solve this problem:
Try fast
memcache_set( 'id' , memcache_get( 'id' ) , 0 , 60 );
But here a temporary hole between get and set and another client at a point in time can change the value. (?)
Use a semaphore, for example:
not very effective ...
memcache_set ( 'lock' , 1 , 0 , 60 );
memcache_set( 'id' , memcache_get( 'id' ) , 0 , 60 );
memcache_delete( 'lock' );
//client will not increment if the lock is present and wait for lock will get out
use memcache_delete:
memcache::delete ( 'id' , 60 );
- , , . ?
( ):
$memcache_obj = new Memcache;
$memcache_obj->connect('127.0.0.1', 11211);
$memcache_obj->set('id', '1', 0 , 30);
echo "*" . $memcache_obj->get('id') . "\n" ;
$memcache_obj->increment('id');
echo "*" . $memcache_obj->get('id') . "\n" ;
echo " now delete with timeout..." . "\n";
$memcache_obj->delete('id' , 10 ) . "\n" ;
echo "*" . $memcache_obj->get('id') . "\n" ;
sleep(11);
echo "*" . $memcache_obj->get('id') . "\n" ;
:
*1
*2
now delete with timeout...
*
*