PHP指令碼redis類的執行個體源碼

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   使用   sp   on   資料   

class redisDB{       private $redis; //redis對象          /**     * 初始化Redis     * $config = array(     *  ‘server‘ => ‘127.0.0.1‘ 伺服器     *  ‘port‘   => ‘6379‘ 連接埠號碼     * )     * @param array $config     */    function __construct($config = array()){        $this->redis = new Redis();        $this->redis->connect(REDIS_SERVER,REDIS_PORT);        return $this->redis;    }       /**     * 設定值     * @param string $key KEY名稱     * @param string|array $value 擷取得到的資料     * @param int $timeOut 時間     */    public function set($key, $value, $timeOut = 0,$type=json) {        if($type==serialize)        {            $value = serialize($value);        }        else        {            $value = json_encode($value);        }                   $retRes = $this->redis->set($key, $value);        if ($timeOut > 0) $this->redis->setTimeout($key, $timeOut);        return $retRes;    }       /**     * 通過KEY擷取資料     * @param string $key KEY名稱     */    public function get($key,$type=json) {        $result = $this->redis->get($key);                   if($type==serialize)        {            return unserialize($result);        }        else        {            return json_decode($result);        }    }       /**     * 刪除一條資料     * @param string $key KEY名稱     */    public function delete($key) {        return $this->redis->delete($key);    }       /**     * 清空資料     */    public function flushAll() {        return $this->redis->flushAll();    }       /**     * 資料入隊列     * @param string $key KEY名稱     * @param string|array $value 擷取得到的資料     * @param bool $right 是否從右邊開始入     */    public function push($key, $value ,$right = true) {        $value = json_encode($value);        return $right ? $this->redis->rPush($key, $value) : $this->redis->lPush($key, $value);    }       /**     * 資料出隊列     * @param string $key KEY名稱     * @param bool $left 是否從左邊開始出資料     */    public function pop($key , $left = true) {        $val = $left ? $this->redis->lPop($key) : $this->redis->rPop($key);        return json_decode($val);    }       /**     * 資料自增     * @param string $key KEY名稱     */    public function increment($key) {        return $this->redis->incr($key);    }       /**     * 資料自減     * @param string $key KEY名稱     */    public function decrement($key) {        return $this->redis->decr($key);    }       /**     * key是否存在,存在返回ture     * @param string $key KEY名稱     */    public function exists($key) {        return $this->redis->exists($key);    }       /**     * 返回redis對象     * redis有非常多的操作方法,我們只封裝了一部分     * 拿著這個對象就可以直接調用redis自身方法     */    public function redis() {        return $this->redis;    }}

使用方法

include redis.php;$redis new redisDB();$key = fields;$value = 好指令碼;   //value可以是字串或者數組$redis->set($key,$value);//擷取fields的值也很簡單$fvalue = $redis->get(fields);print_r($fvalue);

 

PHP指令碼redis類的執行個體源碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.