Basic usage
<?php /** * Created by PhpStorm. * User: raid * Date: 2016/7/6 * Time: 12:03 */ $m = new Memcached; $arr = array( array('127.0.0.1', 11211), array('127.0.0.2', 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 = 'Memcached'; //缓存时间 private $time = 0; //错误 private $error; //是否开启调试模式 private $debug = 'true'; 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 'Mem.class.php'; $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');
The above has introduced the use of memcached, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.