<commit_before><?php

/**
 * @author <NAME>
 */

namespace Cachalot\Cache;

class Memcache extends AbstractCache
{
    /**
     * @var \Memcache
     */
    private $_cache;

    /**
     * @var string
     */
    private $host;

    /**
     * @var int
     */
    private $port;

    /**
     * @param string $prefix
     * @param string $host
     * @param int $port
     * @throws \RuntimeException
     */
    public function __construct($prefix = '', $host, $port)
    {
        if (!extension_loaded('memcache')) {
            throw new \RuntimeException('Unable to use Memcache cache as memcache extension is not enabled.');
        }

        $this->host = $host;
        $this->port = $port;

        parent::__construct($prefix);
    }

    /**
     * @return \Memcache
     */
    private function getCache()
    {
        if (null === $this->_cache) {
            $this->_cache = new \Memcache();
            $this->_cache->connect($this->host, $this->port);
        }

        return $this->_cache;
    }

    /**
     * @throws \InvalidArgumentException
     * @param \callable $callback
     * @param array $params
     * @param int $expireIn Seconds
     * @param mixed $cacheIdSuffix
     * @return mixed
     */
    public function getCached($callback, $params = array(), $expireIn = 0, $cacheIdSuffix = null)
    {
        $id = $this->getCallbackCacheId($callback, $params, $cacheIdSuffix);

        if (false === $result = $this->getCache()->get($id)) {
            $result = $this->call($callback, $params);
            $this->getCache()->set($id, $result, false, $expireIn);
        }

        return $result;
    }

}
<commit_msg>Switched to injection of Memcache connection instead of creating it inside the library<commit_after><?php

/**
 * @author <NAME>
 */

namespace Cachalot\Cache;

class Memcache extends AbstractCache
{
    /**
     * @var \Memcache
     */
    private $cache;

    /**
     * @param string $prefix
     * @param \Memcache $memcache
     * @throws \RuntimeException
     */
    public function __construct($prefix = '', $memcache)
    {
        $this->cache = $memcache;
        parent::__construct($prefix);
    }

    /**
     * @throws \InvalidArgumentException
     * @param \callable $callback
     * @param array $params
     * @param int $expireIn Seconds
     * @param mixed $cacheIdSuffix
     * @return mixed
     */
    public function getCached($callback, $params = array(), $expireIn = 0, $cacheIdSuffix = null)
    {
        $id = $this->getCallbackCacheId($callback, $params, $cacheIdSuffix);

        if (false === $result = $this->cache->get($id)) {
            $result = $this->call($callback, $params);
            $this->cache->set($id, $result, false, $expireIn);
        }

        return $result;
    }

}

{'fraction_non_alphanumeric': 0.15066522833513124, 'fraction_numerical': 0.000719165767709457, 'mean_word_length': 2.1541950113378685, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '894644', 'n_tokens_mistral': 850, 'n_tokens_neox': 834, 'n_words': 198}