public function getAndSet($value, $ttl = null)
    {
        $keys = $this->queryBuilder->getQueryKeys();
        if (count($keys) > 1) {
            throw new Exception('GetAndSet doesnt support multiple keys');
        } elseif (count($keys) == 0) {
            throw new Exception('No query keys');
        }
        $key = $keys[0];
        if (!$this->isCompleteKey($key)) {
            throw new Exception('Not complete key');
        }

        $value = $this->castValueForUpdate($value);
        $commandName = 'getset' . ucfirst($this->type);
        $command = $this->commandFactory->getCommand($commandName, [$key], $value);
        if (!is_null($ttl)) {
            $command->setTtl($ttl);
        }
        $result = $this->executeCommand($command);
        $data = isset($result[$key]) ? $result[$key] : null;
        if ($data && $this->type == static::TYPE_HASH) {
            $data = $this->resolveHash($data);
        }

        return $data;

    }