awf

<PRE class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">&lt;?php
/**
 * memcache 类封装层,用户数据缓存
 */
//powered by kevin

class memchache {
	
	private static $_instance;
	private $mem;
	
	public function __construct(){
		global $_FWC;
		$this -&gt; mem = new Memcache;
		$this -&gt; mem -&gt; connect($_FWC['memcacheserver'], $_FWC['memcacheport']);
	}
	public function __destruct(){
		$this -&gt; mem -&gt; close();
	}
    public static function getInstance(){
       if (!self::$_instance instanceof self)
            self::$_instance = new self();
        return self::$_instance;
    }
    
    public function read($name){
    	return $this -&gt; mem -&gt; get($name);
    }
    public function write($name, $values,$time=0){
    	$this -&gt; mem -&gt; set($name, $values, 0, $time);
    }
    public function replace($name,$values,$time=0){
    	$this -&gt; mem -&gt; replace($name, $values, false, $time);	
    }
    public function delete($name){
    	$this -&gt; mem -&gt; delete($name);	
    }
    public function flush(){
    	$this -&gt; mem -&gt; flush();
    }
    /**
     * 获取多条行缓存数据
     * @param array $names 
     * array(
     * 	'id' =&gt; key,
     * )
     * @return hits:命中信息数据 | misses:未命中数据id
     */
    public function getMulti($names){
    	//初始化命中和未命中数组
    	$hits 	 = $misses = array();
    	 
    	$names = (array) $names;
    	 
    	$keys 	 = array_values($names);
    	//命中的缓存
    	$hits 	 = $this-&gt;mem-&gt;get($keys);
    
    	//key和id的对应关系数组
    	$flipKey = array_flip($names);
    	
    	if (!$hits) {
    		return array($misses, $flipKey);
    	}
    	
    	//获取的键值对缺少
    	if ( count($hits) != count($keys) ) {
    		//获取未命中的缓存id
    		$misses = array_diff_key($flipKey, $hits);
    	}
    	
    	return array($hits, $misses);
    }
}
?&gt;</PRE>