I wonder what a real life example that I need to use the apc_cas
function in PHP.

- 21,247
- 6
- 47
- 54

- 1,026
- 3
- 14
- 25
-
This change the cache variable with a new one but I also saw in the comments for apc_store: Note APC version 3.1.3 there is a bug (http://pecl.php.net/bugs/bug.php?id=16814) that will display a cache slam averted warning for all writes to a cache var that exists. I am guessing that using this function will not generate that warning – ka_lin Aug 10 '15 at 11:03
-
Hope link will help you: http://www.cnblogs.com/argb/p/3604344.html – Vishal Bharti Aug 10 '15 at 11:09
2 Answers
Assures you that no one else updated the value in that key. Is for that reason that you have to pass the $old value. If the current value is different to your knowlegment of the key value (in this case $old) the key will not be updated and false is returned.
Implementation of http://en.wikipedia.org/wiki/Compare-and-swap commonly used in key stores databases.

- 702
- 6
- 10
A simple example is a counter. Assume we have a key counter
and a function which increases the counter (note that this would be better suited for the apc_inc
function but let's play along):
function incCounter(){
//Get the current value increment and set
$c = apc_fetch('counter');
$c++;
apc_store('counter',$c);
}
However the above has an issue. If two requests occur at the same time both will get the same value of $c
and increment it meaning the counter will only be incremented by one.
Using apc_cas
however let's you guarantee that the value you are updating is the old one and that it hasn't been changed in the meantime.

- 22,354
- 6
- 52
- 80