php操作redies封裝的類_PHP教程

來源:互聯網
上載者:User

php操作redies封裝的類


null,// 只支援一台 Master        'slave'=>array(),// 可以有多台 Slave    );           /**     * 建構函式     *     * @param boolean $isUseCluster 是否採用 M/S 方案     */    public function __construct($isUseCluster=false){        $this->_isUseCluster = $isUseCluster;    }           /**     * 串連伺服器,注意:這裡使用長串連,提高效率,但不會自動關閉     *     * @param array $config Redis伺服器配置     * @param boolean $isMaster 當前添加的伺服器是否為 Master 伺服器     * @return boolean     */    public function connect($config=array('host'=>'127.0.0.1','port'=>6379), $isMaster=true){        // default port        if(!isset($config['port'])){            $config['port'] = 6379;        }        // 設定 Master 串連        if($isMaster){            $this->_linkHandle['master'] = new Redis();            $ret = $this->_linkHandle['master']->pconnect($config['host'],$config['port']);        }else{            // 多個 Slave 串連            $this->_linkHandle['slave'][$this->_sn] = new Redis();            $ret = $this->_linkHandle['slave'][$this->_sn]->pconnect($config['host'],$config['port']);            ++$this->_sn;        }        return $ret;    }           /**     * 關閉串連     *     * @param int $flag 關閉選擇 0:關閉 Master 1:關閉 Slave 2:關閉所有     * @return boolean     */    public function close($flag=2){        switch($flag){            // 關閉 Master            case 0:                $this->getRedis()->close();            break;            // 關閉 Slave            case 1:                for($i=0; $i<$this->_sn; ++$i){                    $this->_linkHandle['slave'][$i]->close();                }            break;            // 關閉所有            case 1:                $this->getRedis()->close();                for($i=0; $i<$this->_sn; ++$i){                    $this->_linkHandle['slave'][$i]->close();                }            break;        }        return true;    }           /**     * 得到 Redis 原始對象可以有更多的操作     *     * @param boolean $isMaster 返回伺服器的類型 true:返回Master false:返回Slave     * @param boolean $slaveOne 返回的Slave選擇 true:負載平衡隨機返回一個Slave選擇 false:返回所有的Slave選擇     * @return redis object     */    public function getRedis($isMaster=true,$slaveOne=true){        // 只返回 Master        if($isMaster){            return $this->_linkHandle['master'];        }else{            return $slaveOne ? $this->_getSlaveRedis() : $this->_linkHandle['slave'];        }    }           /**     * 寫緩衝     *     * @param string $key 組存KEY     * @param string $value 緩衝值     * @param int $expire 到期時間, 0:表示無到期時間     */    public function set($key, $value, $expire=0){        // 永不逾時        if($expire == 0){            $ret = $this->getRedis()->set($key, $value);        }else{            $ret = $this->getRedis()->setex($key, $expire, $value);        }        return $ret;    }           /**     * 讀緩衝     *     * @param string $key 緩衝KEY,支援一次取多個 $key = array('key1','key2')     * @return string || boolean  失敗返回 false, 成功返回字串     */    public function get($key){        // 是否一次取多個值        $func = is_array($key) ? 'mGet' : 'get';        // 沒有使用M/S        if(! $this->_isUseCluster){            return $this->getRedis()->{$func}($key);        }        // 使用了 M/S        return $this->_getSlaveRedis()->{$func}($key);    }     /*    // magic function    public function __call($name,$arguments){        return call_user_func($name,$arguments);        }*/    /**     * 條件形式設定緩衝,如果 key 不存時就設定,存在時設定失敗     *     * @param string $key 緩衝KEY     * @param string $value 緩衝值     * @return boolean     */    public function setnx($key, $value){        return $this->getRedis()->setnx($key, $value);    }           /**     * 刪除緩衝     *     * @param string || array $key 緩衝KEY,支援單個健:"key1" 或多個健:array('key1','key2')     * @return int 刪除的健的數量     */    public function remove($key){        // $key => "key1" || array('key1','key2')        return $this->getRedis()->delete($key);    }           /**     * 值加加操作,類似 ++$i ,如果 key 不存在時自動化佈建為 0 後進行加加操作     *     * @param string $key 緩衝KEY     * @param int $default 操作時的預設值     * @return int 操作後的值     */    public function incr($key,$default=1){        if($default == 1){            return $this->getRedis()->incr($key);        }else{            return $this->getRedis()->incrBy($key, $default);        }    }           /**     * 值減減操作,類似 --$i ,如果 key 不存在時自動化佈建為 0 後進行減減操作     *     * @param string $key 緩衝KEY     * @param int $default 操作時的預設值     * @return int 操作後的值     */    public function decr($key,$default=1){        if($default == 1){            return $this->getRedis()->decr($key);        }else{            return $this->getRedis()->decrBy($key, $default);        }    }           /**     * 添空當前資料庫     *     * @return boolean     */    public function clear(){        return $this->getRedis()->flushDB();    }           /* =================== 以下私人方法 =================== */           /**     * 隨機 HASH 得到 Redis Slave 伺服器控制代碼     *     * @return redis object     */    private function _getSlaveRedis(){        // 就一台 Slave 機直接返回        if($this->_sn <= 1){            return $this->_linkHandle['slave'][0];        }        // 隨機 Hash 得到 Slave 的控制代碼        $hash = $this->_hashId(mt_rand(), $this->_sn);        return $this->_linkHandle['slave'][$hash];    }           /**     * 根據ID得到 hash 後 0~m-1 之間的值     *     * @param string $id     * @param int $m     * @return int     */    private function _hashId($id,$m=10)    {        //把字串K轉換為 0~m-1 之間的一個值作為對應記錄的散列地址        $k = md5($id);        $l = strlen($k);        $b = bin2hex($k);        $h = 0;        for($i=0;$i<$l;$i++)        {            //相加模式HASH            $h += substr($b,$i*2,2);        }        $hash = ($h*1)%$m;        return $hash;    }    /**     *    lpush     */    public function lpush($key,$value){        return $this->getRedis()->lpush($key,$value);    }    /**     *    add lpop     */    public function lpop($key){        return $this->getRedis()->lpop($key);    }    /**     * lrange     */    public function lrange($key,$start,$end){        return $this->getRedis()->lrange($key,$start,$end);        }    /**     *    set hash opeation     */    public function hset($name,$key,$value){        if(is_array($value)){            return $this->getRedis()->hset($name,$key,serialize($value));            }        return $this->getRedis()->hset($name,$key,$value);    }    /**     *    get hash opeation     */    public function hget($name,$key = null,$serialize=true){        if($key){            $row = $this->getRedis()->hget($name,$key);            if($row && $serialize){                unserialize($row);            }            return $row;        }        return $this->getRedis()->hgetAll($name);    }    /**     *    delete hash opeation     */    public function hdel($name,$key = null){        if($key){            return $this->getRedis()->hdel($name,$key);        }        return $this->getRedis()->hdel($name);    }    /**     * Transaction start     */    public function multi(){        return $this->getRedis()->multi();        }    /**     * Transaction send     */    public function exec(){        return $this->getRedis()->exec();        }       }// End Class       // ================= TEST DEMO =================       // 只有一台 Redis 的應用$redis = new RedisCluster();$redis->connect(array('host'=>'127.0.0.1','port'=>6379));//*$cron_id = 10001;$CRON_KEY = 'CRON_LIST'; //$PHONE_KEY = 'PHONE_LIST:'.$cron_id;////cron info$cron = $redis->hget($CRON_KEY,$cron_id);if(empty($cron)){        $cron = array('id'=>10,'name'=>'jackluo');//mysql data    $redis->hset($CRON_KEY,$cron_id,$cron); // set redis    }//phone list$phone_list = $redis->lrange($PHONE_KEY,0,-1);print_r($phone_list);if(empty($phone_list)){    $phone_list =explode(',','13228191831,18608041585');    //mysql data    //join  list    if($phone_list){        $redis->multi();        foreach ($phone_list as $phone) {            $redis->lpush($PHONE_KEY,$phone);                    }        $redis->exec();    }}print_r($phone_list);/*$list = $redis->hget($cron_list,);var_dump($list);*///*///$redis->set('id',35);/*    $redis->lpush('test','1111');    $redis->lpush('test','2222');    $redis->lpush('test','3333');    $list = $redis->lrange('test',0,-1);    print_r($list);    $lpop = $redis->lpop('test');    print_r($lpop);    $lpop = $redis->lpop('test');    print_r($lpop);    $lpop = $redis->lpop('test');    print_r($lpop);*///    var_dump($redis->get('id'));


http://www.bkjia.com/PHPjc/995651.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/995651.htmlTechArticlephp操作redies封裝的類 null,// 只支援一台 Master slave=array(),// 可以有多台 Slave ); /** * 建構函式 * * @param boolean $isUseCluster 是否採用 M/S 方案 */...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.