redis單例模式寫法

<?php

namespace ZW\Memory;
use \Redis as Redis;
use ZW\Conf\Memory as Conf;

class Handle {

    private $handle = NULL;
    private static $_instance = NULL;        //定義私有的屬性變量


    public static function getInstance() {   //定義公用的靜態方法
        if (NULL == self::$_instance) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }


    public function __construct() {
        $redis = new Redis();          //實例化redis
        $redis->connect(Conf::HOST, Conf::PORT);
        $redis->auth(Conf::AUTH);
        $this->handle = &$redis;       //將變量與redis通過引用符關聯在一起,以後直接使用handle即可,相當於將redis付給一個變量,這是另一種寫法
        $this->handle->select(ENVIRONMENT);
    }

    public function __destruct() {
        $this->handle->close();
    }

    public function get($k) {
        return $this->handle->get($k . '');  //獲取redis鍵名
    }


    public function set($k, $v) {
        return $this->han
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章