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 );
And now imagine that the incrementing "part 2" is called from many independent clients and every is getting its unique ID.
Qestion is: How extend expiration of value 'id' with keeping consistency of value? Keep in mind that everytime some client can come to increment the value.
Some ideas how solve this problem:
Try to be fast
memcache_set( 'id' , memcache_get( 'id' ) , 0 , 60 );
But here is time hole between get and set and other client at time can change the value.(?)
Use semaphore e.g.:
not much 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 );
documentation is lack of what happened if you call it twice. Should it extend Expiration of first call or not?
Last surprising example (try it your self):
$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" ;
Returns me:
*1
*2
now delete with timeout...
*
*