Heim >Backend-Entwicklung >PHP-Tutorial >memcached的使用

memcached的使用

WBOY
WBOYOriginal
2016-07-28 08:27:44677Durchsuche

基本使用方法

<?php /**
 * Created by PhpStorm.
 * User: raid
 * Date: 2016/7/6
 * Time: 12:03
 */

$m = new Memcached;
$arr = array(
    array(&#39;127.0.0.1&#39;, 11211),
    array(&#39;127.0.0.2&#39;, 11211),
);
$m->addServer('127.0.0.1', 11211);
print_r($m->getStats());
echo "<br>";
print_r($m->getVersion());
echo "<br>";


$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();

封装类:

<?php /**
 * Created by PhpStorm.
 * User: raid
 * Date: 2016/7/6
 * Time: 19:25
 */
class Mem {
    //Memcached对象
    private $m;
    //对象类型
    private $type = &#39;Memcached&#39;;
    //缓存时间
    private $time = 0;
    //错误
    private $error;
    //是否开启调试模式
    private $debug = &#39;true&#39;;

    public function __construct() {
        if (!class_exists($this->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;
    }



}

封装类的使用:

<?php /**
 * Created by PhpStorm.
 * User: raid
 * Date: 2016/7/6
 * Time: 19:06
 */

include &#39;Mem.class.php&#39;;

$m = new Mem();

$m->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 "<br>";
$m->s('test', NULL);
echo $m->s('test');

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn