您的位置:首页 > 数据库 > Memcache

memcache存储 字符串、数组、及其对象

2014-07-23 16:03 141 查看
<span style="font-size:18px;"><?php                                    //php作为客户端使用memcache
	//memcache占内存所,占CPU少
	//memcache是通过IP直接获取的数据的,没有任何的验证---不安全,轻则数据被人查看,重则服务器被攻击
	//最好放到内网访问,不对外公开 memcache -d -u root -l 192.168.1.111 -p 11211 只允许111服务器访问
	$mem=new Memcache();

	$mem->connect("localhost", 11211);   //connect持久链接
//	$mem->addServer("www.lamp.com", 11221);
//	$mem->addServer("192.167.1.112", 11211);

	$mem->add("mystr", "this is a memcache test!", MEMCACHE_COMPRESSED, 3600);
	//$mem->add("mystr", "this is a memcache test!", MEMCACHE_COMPRESSED, 3600);//这个值添加不进去,重复键名(memcache重复添加不了)
	$mem->set("mystr", "wwwwwwwwwwwwww", MEMCACHE_COMPRESSED, 3600);  //修改键名的值 也可以replace
	
	$mem->delete("mystr");//删除单个
	$mem->flush();        //删除所有

	$str=$mem->get("mystr");//获取设置的值
	echo "string: ".$str."<br>";

	$mem->add("myarr", array("aaa", "bbb", "ccc", "ddd")); //存数组
	print_r($mem->get("myarr"));

	
	
	
	echo '<br>';
	class Person {
		var $name="zhangsan";
		var $age=10;
	}
	$mem->add("myobj", new Person); //存对象
	var_dump($mem->get("myobj"));
	echo "<br>";

	
	
	echo $mem->getVersion();//获取memcache的版本
	echo '<pre>';
	print_r($mem->getStats());//获取状态
	echo '</pre>';
	$mem->close();
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐