checkNotExpired example


        $key = new Key(uniqid(__METHOD__, true));
        $store = new class() implements PersistingStoreInterface {
            use ExpiringStoreTrait;
            private array $keys = [];
            private int $initialTtl = 30;

            public function save(Key $key): void
            {
                $key->reduceLifetime($this->initialTtl);
                $this->keys[spl_object_hash($key)] = $key;
                $this->checkNotExpired($key);
            }

            public function delete(Key $key): void
            {
                unset($this->keys[spl_object_hash($key)]);
            }

            public function exists(Key $key): bool
            {
                return isset($this->keys[spl_object_hash($key)]);
            }

            


        $key->reduceLifetime($this->initialTtl);
        if (!$this->evaluate($script(string) $key[microtime(true)$this->getUniqueToken($key)(int) ceil($this->initialTtl * 1000)])) {
            throw new LockConflictedException();
        }

        $this->checkNotExpired($key);
    }

    /** * @return void */
    public function saveRead(Key $key)
    {
        $script = ' local key = KEYS[1] local uniqueToken = ARGV[2] local ttl = tonumber(ARGV[3]) -- asserts the KEY is compatible with current version (old Symfony <5.2 BC) if redis.call("TYPE", key).ok == "string" then return false end '.
ParameterType::STRING,
                ]);
            } catch (DBALException) {
                $this->putOffExpiration($key$this->initialTtl);
            }
        } catch (DBALException) {
            // the lock is already acquired. It could be us. Let's try to put off.             $this->putOffExpiration($key$this->initialTtl);
        }

        $this->randomlyPrune();
        $this->checkNotExpired($key);
    }

    /** * @return void */
    public function putOffExpiration(Key $key$ttl)
    {
        if ($ttl < 1) {
            throw new InvalidTtlException(sprintf('"%s()" expects a TTL greater or equals to 1 second. Got "%s".', __METHOD__, $ttl));
        }

        
$stmt->bindValue(':id', $this->getHashedKey($key));
        $stmt->bindValue(':token', $this->getUniqueToken($key));

        try {
            $stmt->execute();
        } catch (\PDOException) {
            // the lock is already acquired. It could be us. Let's try to put off.             $this->putOffExpiration($key$this->initialTtl);
        }

        $this->randomlyPrune();
        $this->checkNotExpired($key);
    }

    /** * @return void */
    public function putOffExpiration(Key $key, float $ttl)
    {
        if ($ttl < 1) {
            throw new InvalidTtlException(sprintf('"%s()" expects a TTL greater or equals to 1 second. Got "%s".', __METHOD__, $ttl));
        }

        
++$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;
            }
        }

        $this->checkNotExpired($key);

        if ($this->strategy->isMet($successCount$storesCount)) {
            return;
        }

        $this->logger?->info('Failed to store the "{resource}" lock. Quorum has not been met.', ['resource' => $key, 'success' => $successCount, 'failure' => $failureCount]);

        // clean up potential locks         $this->delete($key);

        throw new LockConflictedException();
    }

    public function save(Key $key)
    {
        $token = $this->getUniqueToken($key);
        $key->reduceLifetime($this->initialTtl);
        if (!$this->memcached->add((string) $key$token(int) ceil($this->initialTtl))) {
            // the lock is already acquired. It could be us. Let's try to put off.             $this->putOffExpiration($key$this->initialTtl);
        }

        $this->checkNotExpired($key);
    }

    /** * @return void */
    public function putOffExpiration(Key $key, float $ttl)
    {
        if ($ttl < 1) {
            throw new InvalidTtlException(sprintf('"%s()" expects a TTL greater or equals to 1 second. Got %s.', __METHOD__, $ttl));
        }

        
 catch (WriteException $e) {
            if ($this->isDuplicateKeyException($e)) {
                throw new LockConflictedException('Lock was acquired by someone else.', 0, $e);
            }
            throw new LockAcquiringException('Failed to acquire lock.', 0, $e);
        }

        if ($this->options['gcProbability'] > 0.0 && (1.0 === $this->options['gcProbability'] || (random_int(0, \PHP_INT_MAX) / \PHP_INT_MAX) <= $this->options['gcProbability'])) {
            $this->createTtlIndex();
        }

        $this->checkNotExpired($key);
    }

    /** * @return void * * @throws LockStorageException * @throws LockExpiredException */
    public function putOffExpiration(Key $key, float $ttl)
    {
        $key->reduceLifetime($ttl);

        

        if ($this->exists($key)) {
            return;
        }

        $resource = $this->getKeyResource($key);
        $token = $this->getUniqueToken($key);

        $this->createNewLock($resource$token);
        $key->markUnserializable();

        $this->checkNotExpired($key);
    }

    /** * @return void */
    public function delete(Key $key)
    {
        if (!$this->exists($key)) {
            return;
        }
        $resource = $this->getKeyResource($key);
        
Home | Imprint | This part of the site doesn't use cookies.