Memcached concurrency problem

$m = new Memcached();
$res = $m->get("key");

if(empty($res)) {
    $ret = $m->set("key");
    $ret && $this->insert($data);
}

Why does the insert data repeat in the above code? What is the correct way to write it?

Mar.28,2021

does anyone know how to solve it? Wait on the line! Currently, there are the following abnormal data

clipboard.png

clipboard.png


solve this problem with mem locks


there is obviously something wrong with the logic of your code
$ret & & $this- > insert ($data);

) Insert

only when

ret is empty.


MC is not persistent either. It can only be intercepted at the first level. To prevent repetition, it still depends on the unique index of the database


if there is too much concurrency, you can consider using locks, and $ret & $this- > insert ($data);) in your logic. Without a rigorous judgment, that is, when concurrency is realized, as long as $ret is implemented, it will result in repeated insertion of $this- > insert, and at the same time limit duplicate values in combination with the unique index of the database

.
Menu