protected function replace(string $key, $data)
    {
        // Write a new database to a temporary file
        $tmpFile = $this->getDatabase()->openTempFile();
        $file = $this->getDatabase()->readFromFile();

        foreach ($file as $line) {
            /** @var Line $line */
            if ($line->getKey() == $key) {
                if ($data !== false) {
                    $tmpFile->fwrite($this->getLineString($key, $data));
                }
            } else {
                $tmpFile->fwrite($line->getLine() . "\n");
            }
        }

        $tmpFile->rewind();

        // Overwrite the database with the temporary file
        $this->getDatabase()->writeTempToFile($tmpFile);

        // Delete the key from cache
        if ($cache = $this->getConfig()->getCache()) {
            $cache->delete($key);
        }
    }