]> git.mxchange.org Git - friendica.git/commitdiff
Fixing redis cachekey
authorPhilipp Holzer <admin@philipp.info>
Mon, 4 Mar 2019 20:28:36 +0000 (21:28 +0100)
committerPhilipp Holzer <admin@philipp.info>
Mon, 4 Mar 2019 20:28:36 +0000 (21:28 +0100)
src/Core/Lock/AbstractLockDriver.php
src/Core/Lock/CacheLockDriver.php
src/Core/Lock/DatabaseLockDriver.php
src/Core/Lock/ILockDriver.php
tests/src/Core/Lock/DatabaseLockDriverTest.php
tests/src/Core/Lock/LockTest.php

index 033d6f356e74ce263f9909877e276db80ddb597a..0aedeeb1b09ab61cdf71c144c7df3b6cd02e6263 100644 (file)
@@ -23,7 +23,8 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver
         * @param string key The Name of the lock
         * @return bool      Returns true if the lock is set
         */
-       protected function hasAcquiredLock($key) {
+       protected function hasAcquiredLock($key)
+       {
                return isset($this->acquireLock[$key]) && $this->acquiredLocks[$key] === true;
        }
 
@@ -32,7 +33,8 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver
         *
         * @param string $key The Name of the lock
         */
-       protected function markAcquire($key) {
+       protected function markAcquire($key)
+       {
                $this->acquiredLocks[$key] = true;
        }
 
@@ -41,18 +43,26 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver
         *
         * @param string $key The Name of the lock
         */
-       protected function markRelease($key) {
+       protected function markRelease($key)
+       {
                unset($this->acquiredLocks[$key]);
        }
 
        /**
         * Releases all lock that were set by us
         *
-        * @return void
+        * @return boolean Was the unlock of all locks successful?
         */
-       public function releaseAll() {
+       public function releaseAll()
+       {
+               $return = true;
+
                foreach ($this->acquiredLocks as $acquiredLock => $hasLock) {
-                       $this->releaseLock($acquiredLock);
+                       if (!$this->releaseLock($acquiredLock)) {
+                               $return = false;
+                       }
                }
+
+               return $return;
        }
 }
index c1cd8ffb43a3ab407324c757e2cbd0190737ed75..6f34a0d6a948b9bdd3c5fafd6302675490d6591c 100644 (file)
@@ -65,12 +65,16 @@ class CacheLockDriver extends AbstractLockDriver
        {
                $cachekey = self::getLockKey($key);
 
+               $return = false;
+
                if ($override) {
-                       $this->cache->delete($cachekey);
+                       $return = $this->cache->delete($cachekey);
                } else {
-                       $this->cache->compareDelete($cachekey, getmypid());
+                       $return = $this->cache->compareDelete($cachekey, getmypid());
                }
                $this->markRelease($key);
+
+               return $return;
        }
 
        /**
index a137ef12edcac6e6766af4f704128e3ce97085d3..a8269bc92ea6ad16004c9dcdad2447e569d0f8a5 100644 (file)
@@ -76,11 +76,11 @@ class DatabaseLockDriver extends AbstractLockDriver
                        $where = ['name' => $key, 'pid' => $this->pid];
                }
 
-               DBA::delete('locks', $where);
+               $return = DBA::delete('locks', $where);
 
                $this->markRelease($key);
 
-               return;
+               return $return;
        }
 
        /**
@@ -88,9 +88,11 @@ class DatabaseLockDriver extends AbstractLockDriver
         */
        public function releaseAll()
        {
-               DBA::delete('locks', ['pid' => $this->pid]);
+               $return = DBA::delete('locks', ['pid' => $this->pid]);
 
                $this->acquiredLocks = [];
+
+               return $return;
        }
 
        /**
index 7cbaa4fc6908a701721c1886705a3a85c2ff3fae..7df5b3f87aaad43e7a1064fcc0113100c4485779 100644 (file)
@@ -36,14 +36,14 @@ interface ILockDriver
         * @param string $key      The Name of the lock
         * @param bool   $override Overrides the lock to get released
         *
-        * @return void
+        * @return boolean Was the unlock successful?
         */
        public function releaseLock($key, $override = false);
 
        /**
         * Releases all lock that were set by us
         *
-        * @return void
+        * @return boolean Was the unlock of all locks successful?
         */
        public function releaseAll();
 }
index 3d641376995549a91e3c1664b8f41a3919dbc5c8..297e76d50b0fd25c70613fbe10aaefac1fef5a90 100644 (file)
@@ -104,4 +104,15 @@ class DatabaseLockDriverTest extends LockTest
 
                parent::testReleaseAfterUnlock();
        }
+
+       public function testReleaseWitTTL()
+       {
+               $this->mockIsLocked('test', false, $this->startTime, 1);
+               $this->mockAcquireLock('test', 10, false, $this->pid, false, $this->startTime, 1);
+               $this->mockIsLocked('test', true, $this->startTime, 1);
+               $this->mockReleaseLock('test', $this->pid, 1);
+               $this->mockIsLocked('test', false, $this->startTime, 1);
+
+               parent::testReleaseWitTTL();
+       }
 }
index 6dc170e51429bdfc659e8979b5e36cdc0e239d86..28f51733f428d9f7748dd0d52fa5a037a50bc047 100644 (file)
@@ -87,7 +87,7 @@ abstract class LockTest extends MockedTest
                $this->assertTrue($this->instance->isLocked('bar'));
                $this->assertTrue($this->instance->isLocked('nice'));
 
-               $this->instance->releaseAll();
+               $this->assertTrue($this->instance->releaseAll());
 
                $this->assertFalse($this->instance->isLocked('foo'));
                $this->assertFalse($this->instance->isLocked('bar'));
@@ -105,18 +105,30 @@ abstract class LockTest extends MockedTest
                $this->assertTrue($this->instance->acquireLock('bar', 1));
                $this->assertTrue($this->instance->acquireLock('nice', 1));
 
-               $this->instance->releaseLock('foo');
+               $this->assertTrue($this->instance->releaseLock('foo'));
 
                $this->assertFalse($this->instance->isLocked('foo'));
                $this->assertTrue($this->instance->isLocked('bar'));
                $this->assertTrue($this->instance->isLocked('nice'));
 
-               $this->instance->releaseAll();
+               $this->assertTrue($this->instance->releaseAll());
 
                $this->assertFalse($this->instance->isLocked('bar'));
                $this->assertFalse($this->instance->isLocked('nice'));
        }
 
+       /**
+        * @small
+        */
+       public function testReleaseWitTTL()
+       {
+               $this->assertFalse($this->instance->isLocked('test'));
+               $this->assertTrue($this->instance->acquireLock('test', 1, 10));
+               $this->assertTrue($this->instance->isLocked('test'));
+               $this->assertTrue($this->instance->releaseLock('test'));
+               $this->assertFalse($this->instance->isLocked('test'));
+       }
+
        /**
         * @medium
         */