Reputation: 207
I have some set of entities and users. The entity can be processed only by one user. So if the user did GET request on this entity it will be "connected" to him in some time.
I found that I can use cache for this thing, but the explanation about Atomic locks isn't clear for me, could someone help with a simple example that demonstrates using?
Upvotes: 6
Views: 4165
Reputation: 86
This is what I have implemented recently:
try {
// Trying to acquiring lock.
// If lock is already acquired, waiting 5 seconds to try again.
Cache::lock($key)->block(5);
} catch (LockTimeoutException $e) {
// Unable to acquire lock, can't cache the token
return;
}
// Lock acquired, caching the token
Cache::put($key, $token, 1);
To test this we've used 2 devices logged in as the same user and making the same request at the same time. Hope this helps.
Upvotes: 5