I am trying to store an array in memcached. And there will be new datas that need to be appended to my previously stored array.
Here is the code sample:
$lastDay = $MC->get('LastDayTopics');
$lastDay[] = $newTopic;
$MC->set('LastDayTopics', $lastDay );
What if two clients execute this code same time?
In my scenerio they both gets the array from memcached and loads the $lastDay before the other client sets the new array. So in the end, one of the new datas will not be stored in memcached. It will be destroyed without being stored.
Is there any way to solve this problem?
You have to use additional locking, e.g.: http://bluxte.net/musings/2009/10/28/simple-distributed-lock-memcached
You can also solve this with another DB which supports atomic updates/push like redis or mongodb.