If you make use of the Laravel framework, here is a code example from Taylor Otwell on how to use cache locking.
try {
$lock = Cache::lock('foo', 10)->block(5);
// Lock acquired after waiting maximum of 5 seconds...
} catch (LockTimeoutException $e) {
// Unable to acquire lock...
} finally {
optional($lock)->release();
}