memcached的使用

php教程评论270 views阅读模式

基本使用方法

 addServer('127.0.0.1', 11211);
print_r($m->getStats());
echo "
"; print_r($m->getVersion()); echo "
"; $data = array( 'key' => 'value', 'key2' => 'value2', ); //$m->setMulti($data, 600); $result = $m->getMulti(array('key', 'key2')); $m->deleteMulti(array('key', 'key2')); //print_r($result); echo $m->get('key'); echo $m->getResultCode(); echo $m->getResultMessage(); //$m->add('mkey', 'mvalue', 600); //$m->replace('mkey', 'mvalue2', 1); //$m->flush(); //$m->set('num', 50, 600); //$m->increment('num', 5); $m->decrement('num', 5); echo $m->get('num'); $m->flush();

封装类:

 type)) {
            $this->error = 'No '.$this->type;
            return false;
        } else {
            $this->m = new $this->type;
        }
    }

    public function addServer($arr) {
        $this->m->addServers($arr);
    }

    public function s($key, $value = '', $time = NULL) {
        $number = func_num_args();
        if ($number == 1) {
            //get操作
            return $this->get($key);
        } else if ($number >= 2) {
            if ($value === NULL) {
                //delete操作
                $this->delete($key);
            } else {
                //set操作
                $this->set($key, $value, $time);
            }

        }
    }

    private function set($key, $value, $time = NULL) {
        if ($time === NULL) {
            $time = $this->time;
        }
        $this->m->set($key, $value, $time);
        if ($this->debug) {
            if ($this->m->getResultCode() != 0) {
                return false;
            }
        }
    }

    private function get($key) {
        $ret = $this->m->get($key);
        if ($this->debug) {
            if ($this->m->getResultCode() != 0) {
                return false;
            }
        }

        return $ret;
    }

    /**
     * 删除
     * @param $key
     */
    private function delete($key) {
        $this->m->delete($key);
    }

    public function getError() {
        if ($this->error) {
            return $this->error;
        } else {
            return $this->m->getResultMessage();
        }
    }

    public function d($debug) {
        $this->debug = $debug;
    }



}

封装类的使用:

 addServer(array(
    array('127.0.0.1', 11211),
));

//$m->s('key', 'value', 1800);
//
//$m->s('key', NULL);
//echo $m->s('key');
//echo $m->getError();

$m->s('test', 'testvalue', 0);
echo $m->s('test');
echo "
"; $m->s('test', NULL); echo $m->s('test');

以上就介绍了 memcached的使用,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

企鹅博客
  • 本文由 发表于 2019年9月7日 12:48:41
  • 转载请务必保留本文链接:https://www.qieseo.com/320104.html

发表评论