1.memcache的基本使用
輸出效果:
上代碼:
<?php
//連接Memcache
$mem = new Memcache;
$mem->connect("localhost", 11211) or die ("Could not connect");
//保存數據
$mem->set('key1', 'This is first value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val ."<br>";
//替換數據
$mem->replace('key1', 'This is replace value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "<br>";
// 後面追加數據
$mem->append('key1', 'This is afterzhuijia value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "<br>";
// 前面追加數據
$mem->prepend('key1', 'This is prezhuijia value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "<br>";
//保存數組數據
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 0, 60);
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "<br>";
//添加對象
class Dog{
public $name;
public $age;
public function __construct($name,$age){
$this->name=$name;
$this->age=$age;
}
}
$dog3=new Dog('小狗',50);
$mem->set('key3',$dog3,MEMCACHE_COMPRESSED,60);
$val3 = $mem->get('key3');
echo "Get key3 value name: ";
print_r($val3->name);
echo "<br>";
//獲取版本信息
$version = $mem->getversion();
echo "Get version value: ";
print_r($version);
echo "<br>";
//刪除數據
$mem->delete('key1');
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "<br>";
//清除所有數據
$mem->flush();
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "<br>";
$val3 = $mem->get('key3');
echo "Get key3 value: ";
print_r($val3);
echo "<br>";
//關閉連接
$mem->close();
2.先從緩存讀數據,無數據在調用數據庫
效果圖:
上代碼:
<?php
$mem = new Memcache();
$mem->connect('localhost',11211);
$users = $mem->get('users');//從memcached中讀取users數據
if(empty($users)){
$dsn = 'mysql:host=localhost;dbname=pzkc';
$pdo = new PDO($dsn,'root','root');
$sql = 'select * from lyy_member;';
$st = $pdo->prepare($sql);
$st->execute();
$users = $st->fetchAll(PDO::FETCH_ASSOC);
$mem->add('users',$users,false,5);//將users數據添加到memcached中並保存5秒
echo 'from mysql'.'<br/>';
}else{
echo 'from cache'.'<br/>';
}
print_r($users);
?>
3.構造簡單消息列隊
參考:https://www.cnblogs.com/ximu/articles/2118323.html
性能特點
1. 無差錯高併發寫入
2.內部指針以int計數, 一個隊列當寫入次數超過int範圍時,出錯
<?php
/**
* Memcache 消息隊列類
* @author xiaowu <[email protected]>
*/
class QMC {
const PREFIX = 'ASDFASDFFWQKE';
/**
* 初始化mc
* @staticvar string $mc
* @return Memcache
*/
static private function mc_init() {
static $mc = null;
if (is_null($mc)) {
$mc = new Memcache;
$mc->connect('127.0.0.1', 11211);
}
return $mc;
}
/**
* mc 計數器,增加計數並返回新的計數
* @param string $key 計數器
* @param int $offset 計數增量,可爲負數.0爲不改變計數
* @param int $time 時間
* @return int/false 失敗是返回false,成功時返回更新計數器後的計數
*/
static public function set_counter( $key, $offset, $time=0 ){
$mc = self::mc_init();
$val = $mc->get($key);
if( !is_numeric($val) || $val < 0 ){
$ret = $mc->set( $key, 0, $time );
if( !$ret ) return false;
$val = 0;
}
$offset = intval( $offset );
if( $offset > 0 ){
return $mc->increment( $key, $offset );
}elseif( $offset < 0 ){
return $mc->decrement( $key, -$offset );
}
return $val;
}
/**
* 寫入隊列
* @param string $key
* @param mixed $value
* @return bool
*/
static public function input( $key, $value ){
$mc = self::mc_init();
$w_key = self::PREFIX.$key.'W';
$v_key = self::PREFIX.$key.self::set_counter($w_key, 1);
return $mc->set( $v_key, $value );
}
/**
* 讀取隊列裏的數據
* @param string $key
* @param int $max 最多讀取條數
* @return array
*/
static public function output( $key, $max=100 ){
$out = array();
$mc = self::mc_init();
$r_key = self::PREFIX.$key.'R';
$w_key = self::PREFIX.$key.'W';
$r_p = self::set_counter( $r_key, 0 );//讀指針
$w_p = self::set_counter( $w_key, 0 );//寫指針
if( $r_p == 0 ) $r_p = 1;
while( $w_p >= $r_p ){
if( --$max < 0 ) break;
$v_key = self::PREFIX.$key.$r_p;
$r_p = self::set_counter( $r_key, 1 );
$out[] = $mc->get( $v_key );
$mc->delete($v_key);
}
return $out;
}
}
/**
使用方法:
QMC::input($key, $value );//寫入隊列
$list = QMC::output($key);//讀取隊列
*/
QMC::input(1, 1 );//寫入隊列
QMC::input(1, 2 );//寫入隊列
QMC::input(1, 3 );//寫入隊列
QMC::input(1, 4 );//寫入隊列
$list = QMC::output(1);//讀取隊列
print_r($list);
?>