<commit_before><?php
/**
 * @name Cache.class.php
 * @desc 通用缓冲机制控制类,目前只支持Memcached内存缓存
 * @author 曹晓冬
 * @createtime 2008-09-10 02:32
 * @updatetime 2009-03-20 06:12
 * @usage：
 * $cache_obj = Cache::getInstance('default');  //default为cache.config.php中$CACHE['memcached']配置的服务名称
 * $cache_obj->get($key); //$key为唯一的字符串标识
 * @update 2014-09-16 by <EMAIL>  自动识别memcache插件类型，避免memcache与memcached冲突
 */
namespace yoka;
use yoka\Debug as Debug;
use Memcached;

class Cache implements \yoka\CacheInterface
{
	/* Memcache扩展的类型： Memcache or Memcached */
	private $memcacheType;
	
	/**
	 * 默认的连接项目
	 */
	static $default_item = 'default';
	
	/**
	 * 默认缓存时间(1个月)
	 */
	static $default_lifetime = 2592000;
	
	/**
	 * 实例化数组
	 * @var array
	 */
	static $instance = array();
	/**
	 * 缓存前缀名
	 * @var string
	 */
	private $prefix = '';
	/**
	 * 服务器列表
	 * @var array
	 */
	private $serverlist = array(); 
	/**
	 * 缓存访问对象
	 * @var object
	 */
	private $cache; 
    /**
	 * @name __construct
	 * @desc 构造函数
	 * @param void
	 * @return object instance of Cache
	 * @access protected
	 *
	 */
	
    private function __construct($item = '', $serverList = array(), $type = 'memcached')
    {
    	if(class_exists('Memcached') && method_exists('Memcached', 'setOption')) $this->memcacheType = 'Memcached';
    	else $this->memcacheType = 'Memcache';
    	
    	if('memcached' == $type)
    	{
			if($this->memcacheType == 'Memcache') $this->cache = new \yoka\Memcached();
			else $this->cache = new Memcached();
			if(!empty($serverList))
			{
				foreach($serverList as $v)
				{
			           $is_sucess = $this->cache->addServer($v['host'],$v['port']);
			           if($is_sucess === false)
			                         Debug::log('memcacheConnectError','ip:'.$v['host'].":".$v['port'],__FILE__.':'.__LINE__);  
			           $this->serverlist[] = array('ip' => $v['host'], 'port' => $v['port'], 'is_sucess'=>$is_sucess);				
				}
			}
    	}
		$this->prefix = $item;
    }
    
    /**
     * 设置缺省项目
     * @param unknown $item
     */
    public static function setDefault($item){
    	self::$default_item = $item;
    }
    
    /**
     * @name getInstance
     * @desc 单件模式调用Cache类入口
     * @param string $item
     * @return object instance of Cache
     * @access public
     **/
    public static function getInstance($item = null)
    {
    	if($item === null) $item = self::$default_item;
    	global $CACHE;
    	$obj = Cache::$instance;
    	if(!isset($obj[$item]))
    	{
    		$key = "";
    		$list = array();
			if(isset($CACHE['memcached'][$item]))
			{
				$config = $CACHE['memcached'][$item];
				$list = $config['server'];
				$key = $item;
			}
			$obj[$item] = new Cache($item, $list);
			Cache::$instance = $obj;
		}
    	return $obj[$item];
    }
    /**
     * 将数据存入缓存。只有键值不存在时，才成功；否则失败。
     * 该方法提供原子操作！
     *
     * @param string $cacheKey
     * @param mixed $cacheValue
     * @param int $lifetime
     * @return Boolean
     */
    public function add($cacheKey, $cacheValue, $lifetime = null) {
    	if($lifetime === null) $lifetime = self::$default_lifetime;
    	$begin_microtime = Debug::getTime();
        $cacheKey = $this->getKey($cacheKey);
        if(empty($cacheKey)) return false;
        $re = $this->cache->add($cacheKey, $cacheValue, $lifetime);
        Debug::cache($this->_getServer(), $cacheKey, Debug::getTime() - $begin_microtime, 'add', $re);
        return $re;
    }
    /**
	 * @name set
	 * @desc 将数据插入缓冲中
	 * @param string $cacheKey 字符串标识<commit_msg>Cache: 增加后备设定。<commit_after><?php
/**
 * @name Cache.class.php
 * @desc 通用缓冲机制控制类,目前只支持Memcached内存缓存
 * @author 曹晓冬
 * @createtime 2008-09-10 02:32
 * @updatetime 2009-03-20 06:12
 * @usage：
 * $cache_obj = Cache::getInstance('default');  //default为cache.config.php中$CACHE['memcached']配置的服务名称
 * $cache_obj->get($key); //$key为唯一的字符串标识
 * @update 2014-09-16 by <EMAIL>  自动识别memcache插件类型，避免memcache与memcached冲突
 */
namespace yoka;
use yoka\Debug as Debug;
use Memcached;

class Cache implements \yoka\CacheInterface
{
	/* Memcache扩展的类型： Memcache or Memcached */
	private $memcacheType;
	
	/**
	 * 默认的连接项目
	 */
	static $default_item = 'default';
	
	/**
	 * 默认缓存时间(1个月)
	 */
	static $default_lifetime = 2592000;
	
	/**
	 * 实例化数组
	 * @var array
	 */
	static $instance = array();
	/**
	 * 缓存前缀名
	 * @var string
	 */
	private $prefix = '';
	/**
	 * 服务器列表
	 * @var array
	 */
	private $serverlist = array(); 
	/**
	 * 缓存访问对象
	 * @var object
	 */
	private $cache; 
    /**
	 * @name __construct
	 * @desc 构造函数
	 * @param void
	 * @return object instance of Cache
	 * @access protected
	 *
	 */
	
    private function __construct($item = '', $serverList = array(), $backupList = array(), $type = 'memcached')
    {
    	if(class_exists('Memcached') && method_exists('Memcached', 'setOption')) $this->memcacheType = 'Memcached';
    	else $this->memcacheType = 'Memcache';
    	
    	if('memcached' == $type)
    	{
			if($this->memcacheType == 'Memcache') $this->cache = new \yoka\Memcached();
			else $this->cache = new Memcached();
			//主力服务器
			$backup_flag = true;
			if(!empty($serverList))
			{
				foreach($serverList as $v)
				{
					$is_sucess = $this->cache->addServer($v['host'],$v['port']);
					if($is_sucess === false){
						Debug::log('Memcache addServer Error','ip:'.$v['host'].":".$v['port']);  
					}else{
						$this->serverlist[] = array('ip' => $v['host'], 'port' => $v['port'], 'is_sucess'=>$is_sucess);				
						$backup_flag = false;
					}
				}
			}
			//后备服务器：只有主力服务器全部添加失败时使用。集群设计应充分考虑数据一致性！
			if($backup_flag && !empty($backupList))
			{
				foreach($backupList as $v)
				{
					$is_sucess = $this->cache->addServer($v['host'],$v['port']);
					if($is_sucess === false){
						Debug::log('Memcache backupServer Error','ip:'.$v['host'].":".$v['port']);
					}else{
						$this->serverlist[] = array('ip' => $v['host'], 'port' => $v['port'], 'is_sucess'=>$is_sucess);
					}
				}
			}
    	}
		$this->prefix = $item;
    }
    
    /**
     * 设置缺省项目
     * @param unknown $item
     */
    public static function setDefault($item){
    	self::$default_item = $item;
    }
    
    /**
     * @name getInstance
     * @desc 单件模式调用Cache类入口
     * @param string $item
     * @return object instance of Cache
     * @access public
     **/
    public static function getInstance($item = null)
    {
    	if($item === null) $item = self::$default_item;
    	global $CACHE;
    	$obj = Cache::$instance;
    	if(!isset($obj[$item]))
    	{
    		$key = "";
    		$list = array();
			if(isset($CACHE['memcached'][$item]))
			{
				$config = $CACHE['memcached'][$item];
				$list = $config['server'];
				$backup = $config['backup'];
				$key = $item;
			}else{
				\yoka\Debug::log('Cache Error','无配置项:' . $item);
				return false;
			}
			$obj[$item] = new Cache($item, $list, $backup);
			Cache::$instance = $obj;
		}
    	return $obj[$item];
    }
    /**
     * 将数据存入缓存。只有键值不存在时，才成功；否则失败。
     * 该方法提供原子操作！
     *
     * @param string $cacheKey
     * @param mixed $cacheValue
     * @param int $lifetime
     * @return Boolean
     */
    public function add($cacheKey, $cacheValue, $lifetime = null) {
    	if($lifetime === null) $lifetime = self::$default_lifetime;
    	$begin_microtime = Debug::getTime();
        $cacheKey = $this->getKey($cacheKey);
        if(empty($cacheKey)) return false;
        $re = $this->cache->add($cacheKey, $cacheValue, $lifetime);
        Debug::cache($this->_getServer(), $cacheKey, Debug::getTime() - $begin_microtime, 'add', $re);
        return $re;
    }
    /**
	 * @name set
	 * @desc 将数据插入缓冲中
	 * @param string $cacheKey 字符串标识
{'fraction_non_alphanumeric': 0.18590843119145006, 'fraction_numerical': 0.01055548225359546, 'mean_word_length': 2.832153690596562, 'pattern_counts': {'":': 3, '<': 7, '<?xml version=': 0, '>': 45, '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': '1836380', 'n_tokens_mistral': 3162, 'n_tokens_neox': 2976, 'n_words': 507}