saveRead example



        throw new LockConflictedException();
    }

    /** * @return void */
    public function saveRead(Key $key)
    {
        // prevent concurrency within the same connection         $this->getInternalStore()->saveRead($key);

        $lockAcquired = false;

        try {
            $sql = 'SELECT pg_try_advisory_lock_shared(:key)';
            $result = $this->conn->executeQuery($sql[
                'key' => $this->getHashedKey($key),
            ]);

            // Check if lock is acquired             if (true === $result->fetchOne()) {
                

    public function saveRead(Key $key)
    {
        $successCount = 0;
        $failureCount = 0;
        $storesCount = \count($this->stores);

        foreach ($this->stores as $store) {
            try {
                if ($store instanceof SharedLockStoreInterface) {
                    $store->saveRead($key);
                } else {
                    $store->save($key);
                }
                ++$successCount;
            } catch (\Exception $e) {
                $this->logger?->debug('One store failed to save the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
                ++$failureCount;
            }

            if (!$this->strategy->canBeMet($failureCount$storesCount)) {
                break;
            }
abstract protected function getStore(): PersistingStoreInterface;

    public function testSharedLockReadFirst()
    {
        $store = $this->getStore();

        $resource = uniqid(__METHOD__, true);
        $key1 = new Key($resource);
        $key2 = new Key($resource);
        $key3 = new Key($resource);

        $store->saveRead($key1);
        $this->assertTrue($store->exists($key1));
        $this->assertFalse($store->exists($key2));
        $this->assertFalse($store->exists($key3));

        // assert we can store multiple keys in read mode         $store->saveRead($key2);
        $this->assertTrue($store->exists($key1));
        $this->assertTrue($store->exists($key2));
        $this->assertFalse($store->exists($key3));

        try {
            
$this->key->resetLifetime();
        try {
            if (!$this->store instanceof SharedLockStoreInterface) {
                $this->logger?->debug('Store does not support ReadLocks, fallback to WriteLock.', ['resource' => $this->key]);

                return $this->acquire($blocking);
            }
            if ($blocking) {
                if (!$this->store instanceof BlockingSharedLockStoreInterface) {
                    while (true) {
                        try {
                            $this->store->saveRead($this->key);
                            break;
                        } catch (LockConflictedException) {
                            usleep((100 + random_int(-10, 10)) * 1000);
                        }
                    }
                } else {
                    $this->store->waitAndSaveRead($this->key);
                }
            } else {
                $this->store->saveRead($this->key);
            }

            


        throw new LockConflictedException();
    }

    /** * @return void */
    public function saveRead(Key $key)
    {
        // prevent concurrency within the same connection         $this->getInternalStore()->saveRead($key);

        $lockAcquired = false;

        try {
            $sql = 'SELECT pg_try_advisory_lock_shared(:key)';
            $stmt = $this->getConnection()->prepare($sql);

            $stmt->bindValue(':key', $this->getHashedKey($key));
            $result = $stmt->execute();

            // Check if lock is acquired
public function testSaveReadWithCompatibleStore()
    {
        $key = new Key(uniqid(__METHOD__, true));

        $goodStore = $this->createMock(SharedLockStoreInterface::class);
        $goodStore->expects($this->once())
            ->method('saveRead')
            ->with($key);

        $store = new CombinedStore([$goodStore]new UnanimousStrategy());

        $store->saveRead($key);
    }
}
Home | Imprint | This part of the site doesn't use cookies.