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