getUniqueToken example

class InMemoryStore implements SharedLockStoreInterface
{
    private array $locks = [];
    private array $readLocks = [];

    /** * @return void */
    public function save(Key $key)
    {
        $hashKey = (string) $key;
        $token = $this->getUniqueToken($key);
        if (isset($this->locks[$hashKey])) {
            // already acquired             if ($this->locks[$hashKey] === $token) {
                return;
            }

            throw new LockConflictedException();
        }

        // check for promotion         if (isset($this->readLocks[$hashKey][$token]) && 1 === \count($this->readLocks[$hashKey])) {
            
public function save(Key $key)
    {
        $script = ' if redis.call("GET", KEYS[1]) == ARGV[1] then return redis.call("PEXPIRE", KEYS[1], ARGV[2]) elseif redis.call("SET", KEYS[1], ARGV[1], "NX", "PX", ARGV[2]) then return 1 else return 0 end ';
        if (!$this->evaluate($script(string) $key[$this->getUniqueToken($key)(int) ceil(5 * 1000)])) {
            throw new LockConflictedException();
        }
    }

    public function exists(Key $key)
    {
        return $this->redis->get((string) $key) === $this->getUniqueToken($key);
    }

    private function evaluate(string $script, string $resource, array $args)
    {
        
/** * @return void */
    public function save(Key $key)
    {
        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)
    {
$this->checkNotExpired($key);
    }

    /** * @return void */
    public function delete(Key $key)
    {
        $this->getCollection()->deleteOne([ // filter             '_id' => (string) $key,
            'token' => $this->getUniqueToken($key),
        ]);
    }

    public function exists(Key $key): bool
    {
        return null !== $this->getCollection()->findOne([ // filter             '_id' => (string) $key,
            'token' => $this->getUniqueToken($key),
            'expires_at' => [
                '$gt' => $this->createMongoDateTime(microtime(true)),
            ],
        ],


        $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)
    {
        

    public function save(Key $key)
    {
        $key->reduceLifetime($this->initialTtl);

        $sql = "INSERT INTO $this->table ($this->idCol, $this->tokenCol, $this->expirationCol) VALUES (?, ?, {$this->getCurrentTimestampStatement()} + $this->initialTtl)";

        try {
            $this->conn->executeStatement($sql[
                $this->getHashedKey($key),
                $this->getUniqueToken($key),
            ][
                ParameterType::STRING,
                ParameterType::STRING,
            ]);
        } catch (TableNotFoundException) {
            if (!$this->conn->isTransactionActive() || $this->platformSupportsTableCreationInTransaction()) {
                $this->createTable();
            }

            try {
                $this->conn->executeStatement($sql[
                    
$conn = $this->getConnection();
        try {
            $stmt = $conn->prepare($sql);
        } catch (\PDOException) {
            if (!$conn->inTransaction() || \in_array($this->driver, ['pgsql', 'sqlite', 'sqlsrv'], true)) {
                $this->createTable();
            }
            $stmt = $conn->prepare($sql);
        }

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

    


        $this->memcached = $memcached;
        $this->initialTtl = $initialTtl;
    }

    /** * @return void */
    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 */


        $args = [
            $this->getUniqueToken($key),
            time(),
            $ttlInSecond,
            $key->getLimit(),
            $key->getWeight(),
        ];

        if (!$this->evaluate($scriptsprintf('{%s}', $key)$args)) {
            throw new SemaphoreAcquiringException($key, 'the script return false');
        }
    }

    
Home | Imprint | This part of the site doesn't use cookies.