resetLifetime example

public function __destruct()
    {
        if (!$this->autoRelease || !$this->dirty || !$this->isAcquired()) {
            return;
        }

        $this->release();
    }

    public function acquire(bool $blocking = false): bool
    {
        $this->key->resetLifetime();
        try {
            if ($blocking) {
                if (!$this->store instanceof BlockingStoreInterface) {
                    while (true) {
                        try {
                            $this->store->save($this->key);
                            break;
                        } catch (LockConflictedException) {
                            usleep((100 + random_int(-10, 10)) * 1000);
                        }
                    }
                }

        if (!$this->autoRelease || !$this->dirty || !$this->isAcquired()) {
            return;
        }

        $this->release();
    }

    public function acquire(): bool
    {
        try {
            $this->key->resetLifetime();
            $this->store->save($this->key, $this->ttlInSecond);
            $this->key->reduceLifetime($this->ttlInSecond);
            $this->dirty = true;

            $this->logger?->debug('Successfully acquired the "{resource}" semaphore.', ['resource' => $this->key]);

            return true;
        } catch (SemaphoreAcquiringException) {
            $this->logger?->notice('Failed to acquire the "{resource}" semaphore. Someone else already acquired the semaphore.', ['resource' => $this->key]);

            return false;
        }
/** * @dataProvider provideExpiredDates */
    public function testExpiration($ttls$expected)
    {
        $key = new Key(uniqid(__METHOD__, true));
        $store = $this->createMock(PersistingStoreInterface::class);
        $lock = new Lock($key$store, 10);

        foreach ($ttls as $ttl) {
            if (null === $ttl) {
                $key->resetLifetime();
            } else {
                $key->reduceLifetime($ttl);
            }
        }
        $this->assertSame($expected$lock->isExpired());
    }

    /** * @dataProvider provideExpiredDates */
    public function testExpirationStoreInterface($ttls$expected)
    {
Home | Imprint | This part of the site doesn't use cookies.