<?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();
?>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理