PHP 對 memcache操作類

來源:互聯網
上載者:User
class myMemcache {    private $memcache;    /**     * 一般建議這2個值做成常量的形式     */    public function __construct($host = '192.102.1.8', $port = 6379) {        $this->memcache = new Memcache();        $this->memcache->connect($host, $port);        return $this->memcache;    }    /**     * add  添加一個新key,但是如果 key已經在服務端存在,此操作會失敗。     * @param string $Key    KEY名稱     * @param string $value  值----可以是數組,對象,單值     * @param int $timelift 存留時間   add存留時間預設為0表示資料用不到期     */    public function add($key, $value, $timeLife) {        if ($time > 0) {            $retMes = $this->memcache->add($key, $value, MEMCACHE_COMPRESSED, $timeLife);        } else {            $retMes = $this->memcache->add($key, $value);        }        return $retMes;    }    /**     * set?置一致key  修改鍵名的值     * @param string $key 鍵名     * @param string $value 索引值     * @param int $timeLife 生命週期     */    public function set($key, $value, $timeLife) {        if ($timeLife > 0) {            $retMes = $this->memcache->set($key, $value, MEMCACHE_COMPRESSED, $timeLife);        } else {            $retMes = $this->memcache->set($key, $value);        }        return $retMes;    }    /**     * 擷取key     * @param string  $key 鍵名     */    public function get($key) {        $retMes = $this->memcache->get($key);        return $retMes;    }    /**     * 刪除單個key     * @param string $key 鍵名     */    public function deleteKey($key) {        $retMes = $this->memcache->delete($key);        return $retMes;    }    /**     * 刪除所有key     */    public function deleteAll() {        return $this->memcache->flush();    }        /**     * 返回memcache對象     * memcache我們只封裝了常用的一部分     * 拿著這個對象就可以直接調用memcache自身方法     * eg:$memcache->memcacheOtherMethods()->getStats()   getStats方法沒封     */    public function memcacheOtherMethods() {        return $this->memcache;    }    /**     * 釋放     */    public function __destruct() {        $this->memcache->close();    }}
  • 聯繫我們

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