首页 » php

php memcached扩展 链接memcache操作实例

   发表于:php评论 (0)   热度:2586

 1、示例1:最基本的连接OCS及set/get操作:

$connect = new Memcached;  //声明一个新的memcached链接
$connect->setOption(Memcached::OPT_COMPRESSION, false); //关闭压缩功能
$connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true); //使用binary二进制协议
$connect->addServer('aaaaaaaaaa.m.yyyyyyyyyyy.ocs.aliyuncs.com', 11211); //添加OCS实例地址及端口号
$connect->setSaslAuthData('aaaaaaaaaa', 'password'); //设置OCS帐号密码进行鉴权,如已开启免密码功能,则无需此步骤

$connect->set("hello", "world");
echo 'hello: ',$connect->get("hello");
$connect->quit();

2、示例2:在OCS中缓存一个数组

memcached村数组的时候 如果开启了 二进制协议 就要关闭 压缩  否则 数组存不进去 .   或者关闭二进制协议

$connect= new Memcached; //声明一个新的memcached链接
$connect->setOption(Memcached::OPT_COMPRESSION, false); //关闭压缩功能
$connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true);//使用binary二进制协议
$connect->addServer('xxxxxxxx.m.yyyyyyyy.ocs.aliyuncs.com', 11211);//添加OCS实例地址及端口号
$connect->setSaslAuthData('xxxxxxxx', 'bbbbbbbb');//设置OCS帐号密码进行鉴权,如已开启免密码功能,则无需此步骤。

$user = array(
    "name" => "ocs",
    "age" => 1,
    "sex" => "male"

); //声明一组数组  

$expire = 60; //设置过期时间
test($connect->set('your_name',$user,$expire), true, 'Set cache failed');
if($connect->get('your_name')){
    $result =$connect->get('your_name');
}else{  
    echo "Return code:", $connect->getResultCode();
    echo "Retucn Message:", $connect->getResultMessage (); //如出现错误,解析出返回码
    $result=" ";
}  
print_r($result);
$connect->quit();

function test($val, $expect, $msg)
{  
    if($val!= $expect) throw new Exception($msg);
}   

3、示例3:OCS与MySQL数据库结合使用的例子

$connect = new Memcached; //声明一个新的memcached链接
$connect->setOption(Memcached::OPT_COMPRESSION, false);//关闭压缩功能
$connect->setOption(Memcached::OPT_BINARY_PROTOCOL, true);//使用binary二进制协议
$connect->addServer('xxxxxx.m.yyyyyyyy.ocs.aliyuncs.com', 11211);//添加实例地址  端口号
$connect->setSaslAuthData('xxxxxx', 'my_passwd');//设置OCS帐号密码进行鉴权,如已开启免密码功能,则无需此步骤

$user = array(
   "name" => "ocs",
   "age"  => 1,
   "sex"  => "male"
); //定义一组数组

if($connect->get('your_name'))
{
  $result =$connect->get('your_name');
  print_r($result);
  echo "Found in OCS, get data from OCS"; //如果获取到数据,则打印此数据来源于OCS
  exit;  
}
else
{
  echo "Return code:", $connect->getResultCode();
  echo "Retucn Message:", $connect->getResultMessage ();//抛出code返回码
  $db_host='zzzzzz.mysql.rds.aliyuncs.com'; //数据库地址
  $db_name='my_db';         //database name
  $db_username='db_user';    //数据库用户名
  $db_password='db_passwd';//数据库用户密码
  $connection=mysql_connect($db_host,$db_username,$db_password);
  if (!mysql_select_db($db_name, $connection))
  {
    echo 'Could not select database'; //数据库连接不成功则抛出错误信息
    exit;
  }
  $sql = "SELECT name,age,sex FROM test1 WHERE name = 'ocs'";
  $result = mysql_query($sql, $connection);
  while ($row = mysql_fetch_assoc($result))
  {
    $user = array(
      "name" => $row["name"],
      "age"  => $row["age"],
      "sex"  => $row["sex"],
    );
    $expire = 5; //设置数据在缓存中的过期时间
    test($connect->set('your_name',$user,$expire), true, 'Set cache failed'); //写入OCS缓存
  }
  mysql_free_result($result);
  mysql_close($connection);
}
print_r($connect->get('your_name')); //打印出  获取到的数据
echo "Not Found in OCS,get data from MySQL"; //确认从数据库获取的数据
$connect->quit();

function test($val, $expect, $msg)
{
  if($val!= $expect) throw new Exception($msg);

}

 

(。・v・。)
喜欢这篇文章吗?欢迎分享到你的微博、QQ群,并关注我们的微博,谢谢支持。