<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the ************************ package.
 * _____________                           _______________
 *  ______/     \__  _____  ____  ______  / /_  _________
 *   ____/ __   / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
 *    __/ / /  / /_/ /  __/ /  \  / /_/ / / / / /_/ /__
 *      \_\ \_/\____/\___/_/   / / .___/_/ /_/ .___/
 *         \_\                /_/_/         /_/
 *
 * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
 * (c) 2010-2019 http://queryphp.com All rights reserved.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Leevel\Cache\Redis;

use Redis;
use RuntimeException;

/**
 * php redis 扩展缓存.
 *
 * @author <NAME> <<EMAIL>>
 *
 * @since 2018.07.29
 *
 * @version 1.0
 */
class PhpRedis implements IRedis
{
    /**
     * redis 句柄.
     *
     * @var \Redis
     */
    protected $handle;

    /**
     * 配置.
     *
     * @var array
     */
    protected $option = [
        'host'        => '127.0.0.1',
        'port'        => 6379,
        'password'    => '',
        'select'      => 0,
        'timeout'     => 0,
        'persistent'  => false,
    ];

    /**
     * 构造函数.
     *
     * @param array $option
     *
     * @throws \RuntimeException
     */
    public function __construct(array $option = [])
    {
        if (!extension_loaded('redis')) {
            // @codeCoverageIgnoreStart
            throw new RuntimeException('Redis extension must be loaded before use.');
            // @codeCoverageIgnoreEnd
        }

        $this->option = array_merge($this->option, $option);

        $this->handle = $this->createRedis();

        $this->handle->{$this->option['persistent'] ? 'pconnect' : 'connect'}(
            $this->option['host'],
            (int) ($this->option['port']),
            $this->option['timeout']
        );

        if ($this->option['password']) {
            // @codeCoverageIgnoreStart
            $this->handle->auth($this->option['password']);
            // @codeCoverageIgnoreEnd
        }

        if ($this->option['select']) {
            $this->handle->select($this->option['select']);
        }
    }

    /**
     * 返回缓存句柄.
     *
     * @return null|object
     */
    public function handle(): ?object
    {
        return $this->handle;
    }

    /**
     * 获取缓存.
     *
     * @param string $name
     *
     * @return mixed
     */
    public function get(string $name)
    {
        return $this->handle->get($name);
    }

    /**
     * 设置缓存.
     *
     * @param string   $name
     * @param mixed    $data
     * @param null|int $expire
     */
    public function set(string $name, $data, ?int $expire = null): void
    {
        if ($expire) {
            $this->handle->setex($name, $expire, $data);
        } else {
            $this->handle->set($name, $data);
        }
    }

    /**
     * 清除缓存.
     *
     * @param string $name
     */
    public function delete(string $name): void
    {
        $this->handle->delete($name);
    }

    /**
     * 关闭 redis.
     */
    public function close(): void
    {
        $this->handle->close();
        $this->handle = null;
    }

    /**
     * 返回 redis 对象
     *
     * @return \Redis
     */
    protected function createRedis(): Redis
    {
        return new Redis();
    }
}
<commit_msg>fix(test): try to fix `Function Redis::delete() is deprecated`<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the ************************ package.
 * _____________                           _______________
 *  ______/     \__  _____  ____  ______  / /_  _________
 *   ____/ __   / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
 *    __/ / /  / /_/ /  __/ /  \  / /_/ / / / / /_/ /__
 *      \_\ \_/\____/\___/_/   / / .___/_/ /_/ .___/
 *         \_\                /_/_/         /_/
 *
 * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
 * (c) 2010-2019 http://queryphp.com All rights reserved.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Leevel\Cache\Redis;

use Redis;
use RuntimeException;

/**
 * php redis 扩展缓存.
 *
 * @author <NAME> <<EMAIL>>
 *
 * @since 2018.07.29
 *
 * @version 1.0
 */
class PhpRedis implements IRedis
{
    /**
     * redis 句柄.
     *
     * @var \Redis
     */
    protected $handle;

    /**
     * 配置.
     *
     * @var array
     */
    protected $option = [
        'host'        => '127.0.0.1',
        'port'        => 6379,
        'password'    => '',
        'select'      => 0,
        'timeout'     => 0,
        'persistent'  => false,
    ];

    /**
     * 构造函数.
     *
     * @param array $option
     *
     * @throws \RuntimeException
     */
    public function __construct(array $option = [])
    {
        if (!extension_loaded('redis')) {
            // @codeCoverageIgnoreStart
            throw new RuntimeException('Redis extension must be loaded before use.');
            // @codeCoverageIgnoreEnd
        }

        $this->option = array_merge($this->option, $option);

        $this->handle = $this->createRedis();

        $this->handle->{$this->option['persistent'] ? 'pconnect' : 'connect'}(
            $this->option['host'],
            (int) ($this->option['port']),
            $this->option['timeout']
        );

        if ($this->option['password']) {
            // @codeCoverageIgnoreStart
            $this->handle->auth($this->option['password']);
            // @codeCoverageIgnoreEnd
        }

        if ($this->option['select']) {
            $this->handle->select($this->option['select']);
        }
    }

    /**
     * 返回缓存句柄.
     *
     * @return null|object
     */
    public function handle(): ?object
    {
        return $this->handle;
    }

    /**
     * 获取缓存.
     *
     * @param string $name
     *
     * @return mixed
     */
    public function get(string $name)
    {
        return $this->handle->get($name);
    }

    /**
     * 设置缓存.
     *
     * @param string   $name
     * @param mixed    $data
     * @param null|int $expire
     */
    public function set(string $name, $data, ?int $expire = null): void
    {
        if ($expire) {
            $this->handle->setex($name, $expire, $data);
        } else {
            $this->handle->set($name, $data);
        }
    }

    /**
     * 清除缓存.
     *
     * @param string $name
     */
    public function delete(string $name): void
    {
        $this->handle->del($name);
    }

    /**
     * 关闭 redis.
     */
    public function close(): void
    {
        $this->handle->close();
        $this->handle = null;
    }

    /**
     * 返回 redis 对象
     *
     * @return \Redis
     */
    protected function createRedis(): Redis
    {
        return new Redis();
    }
}

{'fraction_non_alphanumeric': 0.16922853828306264, 'fraction_numerical': 0.008990719257540603, 'mean_word_length': 1.5043572984749456, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1194498', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2256, 'n_words': 429}