PHP APC以原子方式获取和设置值

Is it possible to do this in the atomic way?

$myvalue = apc_get("mykey");
apc_store("mykey",0);
// log/deal with myvalue 

"mykey" is increasing on other process frequently, and I don't want to miss count them.

The function you're looking for is apc_cas(). The 'cas' stands for 'compare and swap'. It will save a value in the cache, but only if it hasn't changed since you last fetched it. If the function fails, you just re-fetch the cached value and try to save it again. This ensures no changes are skipped.

Let's say you want to atomically increment a counter. The technique would be:

apc_add('counter', 0); // set counter to zero, only if it does not already exist.    
$oldVar = apc_fetch('counter'); // get current counter

// do whatever you need to do with the counter ...

// ... when you are ready to increment it you can do this
while ( apc_cas('counter', $oldVar, intval($oldVar)+1) === false ) {
    // huh.  Someone else must have updated the counter while we were busy.
    // Fetch the current value, then try to increment it again.
    $oldVar = apc_fetch('counter');
}

It just so happens that APC provides specialized incrementer and decrementer for this, apc_inc() and apc_dec().

Memcache has a cas() that also works with non-integer values.