-
Notifications
You must be signed in to change notification settings - Fork 12
Description
Hello,
I'm using PHP SyncReaderWriter model for cache synchronization that is, the Alghoritm:
- Write Lock:
$sync = new \SyncReaderWriter( $lockKey );
$result = $sync->readlock( $this->lockTimeoutMilliseconds );
-
Check if value exists in the cache and is not expired
-
If it exists and it's valid, unlock and return value:
if ($result) { $sync->readunlock(); }
return $value;
- If it not exists and it's not valid unlock readlock, lock writelock:
$sync->readunlock();
$sync = new \SyncReaderWriter( $lockKey );
$result = $sync->writelock( $this->lockTimeoutMilliseconds );
- Check if value has been created in meantime by other process, if it is so, unlock writelock, and return value
if ($result) { $sync->writeunlock(); }
return $value;
- If it's still not created, create value, put into cache, unlock writelock, return value:
if ($result) { $sync->writeunlock(); }
return $value;
Problem is, sometimes, it looks like a lock is not released and hangs forever until I reboot linux machine, restarting php-fpm doesn't unlocking it.
There are no fatal errors noticed in PHP error log.
There is one thing maybe I'm doing wrong:
If the lock fails, like after 5 sec of waiting, I'm not releasing the lock (Steps 3, 5 or 6) but just getting value from the cache and returning it, or trying to writelock nad create the value.
I'm releasing the lock only if acquired:
if ($result) { $sync->writeunlock(); }
Should I always release it even if acquiring failed:
$sync->writeunlock(); // Releasing the lock even if acquiring failed? ($result != true)
Should I always try to release the lock even if I failed to acquire it, or it's just something else here?
I'm running pretty busy system/website where there are many visitors and tens of thousands items in the cache, so synchronisation is heavly used, but generates a lot of hanging locks, so finally i had to switch synchronisation off in favor of cache slamming until I find solution to the problem, or other way to synchronise it.