PHP APC以原子方式获取和设置值
有可能用原子的方式来做吗PHP APC以原子方式获取和设置值,php,apc,Php,Apc,有可能用原子的方式来做吗 $myvalue = apc_get("mykey"); apc_store("mykey",0); // log/deal with myvalue “mykey”在其他进程上频繁增加,我不想漏掉它们的计数。您正在寻找的函数是。“cas”代表“比较和交换”。它将在缓存中保存一个值,但前提是自上次获取后该值没有更改。如果函数失败,您只需重新获取缓存的值并再次尝试保存它。这确保不会跳过任何更改 假设您希望原子地递增一个计数器。这项技术将是: apc_add('coun
$myvalue = apc_get("mykey");
apc_store("mykey",0);
// log/deal with myvalue
“mykey”在其他进程上频繁增加,我不想漏掉它们的计数。您正在寻找的函数是。“cas”代表“比较和交换”。它将在缓存中保存一个值,但前提是自上次获取后该值没有更改。如果函数失败,您只需重新获取缓存的值并再次尝试保存它。这确保不会跳过任何更改 假设您希望原子地递增一个计数器。这项技术将是:
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');
}
恰好APC为此提供了专门的递增器和递减器
Memcache有一个同样适用于非整数值的函数。不清楚您想要实现什么。看起来您总是希望将其设置为零。如果是这样的话,为什么还要保存它呢?你的答案是误导性的。apc_cas除了整数之外什么都不能用。阅读文档后,你完全正确。我想到的是memcache的
cas()
函数,它适用于任何值,尽管函数签名有点不同。固定柱。。。