<commit_before><?php

namespace gear\plugins\gear\cache;
use gear\Core;
use gear\library\cache\GCache;
use gear\library\GModel;

/** 
 * Плагин для Memcache
 * 
 * @package Gear Framework
 * @author <NAME>
 * @copyright <NAME> 2013
 * @version 0.0.1
 * @since 29.01.2014
 */
class GMemCache extends GCache
{
    /* Const */
    /* Private */
    /* Protected */
    protected $_servers = null;
    protected $_serializer = 'serialize';
    protected $_unserializer = 'unserialize';
    /* Public */
    public $defaultHost = '127.0.0.1';
    public $defaultPort = 11211;
    
    /**
     * Установка списка серверов
     * 
     * @access public
     * @param array $servers
     * @return void
     */
    public function addServers($servers)
    {
        foreach($servers as $server)
            $this->_servers[] = new GModel($server);
    }
    
    /**
     * Возвращает список серверов
     * 
     * @access public
     * @return array
     */
    public function getServers()
    {
        return $this->_servers;
    }
    
    /**
     * Добавление сервера
     * 
     * @access public
     * @param array|object $server
     * @return void
     */
    public function addServer($server)
    {
        if (is_array($server))
            $server = new GModel($server);
        else
        if (!($server instanceof GModel))
            $this->e('Invalid server');
        $this->_servers[] = $server;
    }
    
    /**
     * Добавление значения в кэш
     * 
     * @access public
     * @param string|array $key array as array(key => value, key => value, ...)
     * @param mixed $value as $expire when $key is array
     * @param integer $expire
     * @return boolean
     */
    public function add($key, $value = null, $expire = 30)
    {
        if (is_array($key))
        {
            if ($value !== null)
                $expire = (int)$value;
            $size = $result = count($key);
            foreach($key as $k => $v)
                $result = $this->_cache->add($k, $v, 0, $expire ? time() + $expire : 0) ? $result - 1 : $result;
            return !$result ? true : ($size === $result ? false : $result);
        }
        return $this->_cache->add($key, $value, 0, $expire ? time() + $expire : 0);
    }
    
    /**
     * Добавление значения или обновление существующего в 
     * кэше
     * 
     * @access public
     * @param string|array $key array as array(key => value, key => value, ...)
     * @param mixed $value as $expire when $key is array
     * @param integer $expire
     * @return boolean
     */
    public function set($key, $value, $expire = 30)
    {
        if (is_array($key))
        {
            if ($value !== null)
                $expire = (int)$value;
            $size = $result = count($key);
            foreach($key as $k => $v)
                $result = $this->_cache->set($k, $v, 0, $expire ? time() + $expire : 0) ? $result - 1 : $result;
            return !$result ? true : ($size === $result ? false : $result);
        }
        return $this->_cache->set($key, $value, 0, $expire ? time() + $expire : 0);
    }
    
    /**
     * Получение значения из кэша
     * 
     * @access public
     * @param string|array $key
     * @param boolean|closure $unserialize
     * @return mixed
     */
    public function get($key, $unserialize = false)
    {
        if (is_array($key))
        {
            $result = array();
            foreach($key as $k => $uns)
            {
                if (is_bool($uns) || is_callable($uns))
                    $keyName = $k;
                else
                {
                    $keyName = $uns;
                    $us = $unserialize;
                }
                if ($value = $this->_cache->get($keyName))
                {
                    if ($uns === true)
                        $result[] = call_user_func($this->_unserializer, $value);
                    else
                    if (is_callable($uns))
                        $result[] = $uns($value);
                    else
                        $result[] = $value;
                }
            }
            return $result;
        }
        $value = $this->_cache->get($key);
        if ($value && $unserialize)
            $value = !$unserialize ? $value : (is_callable($unserialize) ? $unserialize($value) : call_user_func($this->_unserializer, $value));
        return $value;
    }
    
    /**
     * Проверка на наличие в кэше значения под указанным ключём
     * 
     * @access public
     * @param string|array $key
     * @return boolean|array
     */
    public function exists($key)
    {
        if (is_array($key))
        {
            $result = array();
            foreach($key as $k)
                $result[$k] = $this->_cache->get($k) !== false ? true : false;
        }
        return $this->_cache->get($key) !== false ? true : false;<commit_msg>refactoring & bug fix<commit_after><?php

namespace gear\plugins\gear\cache;
use gear\Core;
use gear\library\cache\GCache;
use gear\library\GModel;

/** 
 * Плагин для Memcache
 * 
 * @package Gear Framework
 * @author <NAME>
 * @copyright <NAME> 2013
 * @version 0.0.1
 * @since 29.01.2014
 */
class GMemCache extends GCache
{
    /* Const */
    /* Private */
    /* Protected */
    protected $_servers = null;
    protected $_serializer = 'serialize';
    protected $_unserializer = 'unserialize';
    /* Public */
    public $defaultHost = '127.0.0.1';
    public $defaultPort = 11211;
    
    /**
     * Установка списка серверов
     * 
     * @access public
     * @param array $servers
     * @return void
     */
    public function addServers($servers)
    {
        foreach($servers as $server)
            $this->_servers[] = new GModel($server);
    }
    
    /**
     * Возвращает список серверов
     * 
     * @access public
     * @return array
     */
    public function getServers()
    {
        return $this->_servers;
    }
    
    /**
     * Добавление сервера
     * 
     * @access public
     * @param array|object $server
     * @return void
     */
    public function addServer($server)
    {
        if (is_array($server))
            $server = new GModel($server);
        else
        if (!($server instanceof GModel))
            $this->e('Invalid server');
        $this->_servers[] = $server;
    }
    
    /**
     * Добавление значения в кэш
     * 
     * @access public
     * @param string|array $key array as array(key => value, key => value, ...)
     * @param mixed $value as $expire when $key is array
     * @param integer $expire
     * @return boolean
     */
    public function add($key, $value = null, $expire = 30, $serializer = false)
    {
        if (is_array($key))
        {
            $args = func_get_args();
            $expire = isset($args[1]) ? (int)$args[1] : 30;
            if (isset($args[2]))
                $serializer = is_callable($args[2]) ? $args[2] : ($args[2] === true ? $this->_serializer : false);
            else
                $serializer = false;
            $size = $result = count($key);
            foreach($key as $k => $v)
            {
                if ($serializer)
                    $v = $serializer($v);
                $result = $this->_cache->add($k, $v, 0, $expire ? time() + $expire : 0) ? $result - 1 : $result;
            }
            return !$result ? true : ($size === $result ? false : $result);
        }
        if ($serializer === true)
            $value = call_user_func($this->_serializer, $value);
        else
        if (is_callable($serializer))
            $value = $serializer($value);
        return $this->_cache->add($key, $value, 0, $expire ? time() + $expire : 0);
    }
    
    /**
     * Добавление значения или обновление существующего в 
     * кэше
     * 
     * @access public
     * @param string|array $key array as array(key => value, key => value, ...)
     * @param mixed $value as $expire when $key is array
     * @param integer $expire
     * @param boolean|callable $serializer
     * @return boolean
     */
    public function set($key, $value = null, $expire = 30, $serializer = false)
    {
        if (is_array($key))
        {
            $args = func_get_args();
            $expire = isset($args[1]) ? (int)$args[1] : 30;
            if (isset($args[2]))
                $serializer = is_callable($args[2]) ? $args[2] : ($args[2] === true ? $this->_serializer : false);
            else
                $serializer = false;
            $size = $result = count($key);
            foreach($key as $k => $v)
            {
                if ($serializer)
                    $v = $serializer($v);
                $result = $this->_cache->set($k, $v, 0, $expire ? time() + $expire : 0) ? $result - 1 : $result;
            }
            return !$result ? true : ($size === $result ? false : $result);
        }
        if ($serializer === true)
            $value = call_user_func($this->_serializer, $value);
        else
        if (is_callable($serializer))
            $value = $serializer($value);
        return $this->_cache->set($key, $value, 0, $expire ? time() + $expire : 0);
    }
    
    /**
     * Получение значения из кэша
     * 
     * @access public
     * @param string|array $key
     * @param boolean|closure $unserialize
     * @return mixed
     */
    public function get($key, $unserializer = false)
    {
        if (is_array($key))
        {
            $result = array();
            foreach($key as $k => $uns)
            {
                if (is_bool($uns) || is_callable($uns))
                    $keyName = $k;
                else
                {
                    $keyName = $uns;
                    $us = $unserializer;
                }
                if ($value = $this->_cache->get($keyName))
                {
                    if ($uns === true)
                        $result[] = call_user_func($this->_unserializer, $value);
                    else
                    if (is_callable($uns))
                        $result[] = $uns($value);
                    else
                        $result[] = $value;
                }
            }
            return $result;
        }
        $value = $this->_cache->get($key);
        if ($value && $unserializer)
            $value = !$unserializer ? $value : (is_callable($unserializer) ? $unserializer($value) : call_user_func($this->_unserializer, $value));
        return $value;
    }
    
    /**
     * Проверка на наличие в кэше значения под указанным ключём
     * 
     * @access public
     * @param string|array $key
     * @return boolean|array
     */
    public function exists($key)
    {
        if (is_array($key))
        {
            $result = array();
            foreach($key as $k)
                $result[$k] = $this->_cache->get($k) !== false ? true : false;
        }
        return $this->_cache->get($key) !== false ? true : false;
{'fraction_non_alphanumeric': 0.14724264705882353, 'fraction_numerical': 0.008823529411764706, 'mean_word_length': 1.603732950466619, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2741357', 'n_tokens_mistral': 3406, 'n_tokens_neox': 3322, 'n_words': 793}