<commit_before><?php
/**
 * Widget Framework
 *
 * @copyright   <NAME>
 * @license     http://www.opensource.org/licenses/apache2.0.php Apache License
 */

namespace Widget;

use Widget\Cache\AbstractCache;

/**
 * The redis cache widget
 *
 * @author      <NAME> <<EMAIL>>
 */
class Redis extends AbstractCache
{
    /**
     * The redis object
     *
     * @var \Redis
     */
    protected $object;

    /**
     * The host, or the path to a unix domain socket
     *
     * @var string
     */
    protected $host = '127.0.0.1';

    /**
     * The port of the host
     *
     * @var int
     */
    protected $port = 6379;

    /**
     * The timeout seconds
     *
     * @var float|int
     */
    protected $timeout = 0.0;

    /**
     * Whether persistent connection or not
     *
     * @var bool
     */
    protected $persistent = true;

    /**
     * Constructor
     *
     * @param array $options
     */
    public function __construct(array $options = array())
    {
        // force the constructor call "setObject" to init the redis object
        !array_key_exists('object', $options);
        $options['object'] = $this->object;

        parent::__construct($options);

        $this->connect();
    }

    /**
     * Connet the redis server
     *
     * @return bool true on success, false on error
     */
    public function connect()
    {
        $connect = $this->persistent ? 'pconnect' : 'connect';

        return $this->object->$connect($this->host, $this->port, $this->timeout);
    }

    /**
     * {@inheritdoc}
     */
    public function __invoke($key, $value = null, $expire = 0)
    {
        if (1 == func_num_args()) {
            return $this->get($key);
        } else {
            return $this->set($key, $value, $expire);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function get($key, &$success = null)
    {
        return $this->object->get($key);
    }

    /**
     * {@inheritdoc}
     */
    public function set($key, $value, $expire = 0)
    {
        return $this->object->set($key, $value, $expire);
    }

    /**<commit_msg>added setOption for redis widget<commit_after><?php
/**
 * Widget Framework
 *
 * @copyright   <NAME>
 * @license     http://www.opensource.org/licenses/apache2.0.php Apache License
 */

namespace Widget;

use Widget\Cache\AbstractCache;

/**
 * The redis cache widget
 *
 * @author      <NAME> <<EMAIL>>
 */
class Redis extends AbstractCache
{
    /**
     * The redis object
     *
     * @var \Redis
     */
    protected $object;

    /**
     * The host, or the path to a unix domain socket
     *
     * @var string
     */
    protected $host = '127.0.0.1';

    /**
     * The port of the host
     *
     * @var int
     */
    protected $port = 6379;

    /**
     * The timeout seconds
     *
     * @var float|int
     */
    protected $timeout = 0.0;

    /**
     * Whether persistent connection or not
     *
     * @var bool
     */
    protected $persistent = true;
    
    /**
     * The options for \Redis::setOptions()
     * 
     * @var array
     */
    protected $options = array(
        \Redis::OPT_SERIALIZER => \Redis::SERIALIZER_IGBINARY
    );

    /**
     * Constructor
     *
     * @param array $options
     */
    public function __construct(array $options = array())
    {
        // force the constructor call "setObject" to init the redis object
        !array_key_exists('object', $options);
        $options['object'] = $this->object;

        parent::__construct($options);

        $this->connect();
    }

    /**
     * Connet the redis server and set redis options
     *
     * @return bool true on success, false on error
     */
    public function connect()
    {
        $connect = $this->persistent ? 'pconnect' : 'connect';

        $result = $this->object->$connect($this->host, $this->port, $this->timeout);
        
        if ($result) {
            foreach ($this->options as $key => $value) {
                $this->object->setOption($key, $value);
            }
        }
        
        return $result;
    }

    /**
     * {@inheritdoc}
     */
    public function __invoke($key, $value = null, $expire = 0)
    {
        if (1 == func_num_args()) {
            return $this->get($key);
        } else {
            return $this->set($key, $value, $expire);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function get($key, &$success = null)
    {
        return $this->object->get($key);
    }

    /**
     * {@inheritdoc}
     */
    public function set($key, $value, $expire = 0)
    {
        return $this->object->set($key, $value, $expire);
    }

    /**
{'fraction_non_alphanumeric': 0.15133276010318142, 'fraction_numerical': 0.007308684436801376, 'mean_word_length': 1.767995240928019, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, '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': '727534', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1440, 'n_words': 348}