This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Let's say I have a Counter model that saves a value that needs to be increased with every call to increaseValue(). How can I safely do this?
// This function needs to be safe from being executed concurrently
function increaseValue()
{
$c = Counter::first(); $c->value = $c->value + 1; $c->save();
}
Let's say two users simultaneously request a page that increase this counter (The infamous visitor counter). How can I prevent that user 1 reads the current count (say 300) and adds 1 to it, while user 2 simultaneously does the same?
Last updated
I missed the part of the documentation about pessimistic locking. So one can lock the table using lockForUpdate() like in:
Db::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
1-2 of 2