微信公众号开发之(32)缓存

缓存

  • Memcached
  • 是一个高性能的分布式内存对象缓存系统,可以用于动态 Web 应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。
  • 缓存的数据不是永久有效的,因此应用程序必须有针对 Memcached 失效时的向后端存取数据的重试方案。
  • Memcached 不适合存放大文件,目前仅允许存放小于 1MB 的数据。
  • 若应用开启了跨应用授权,可以在调用 new Memcached() 时传入被授权应用的 AccessKey,初始化成功后,即可读写被授权应用的 Memcached 数据。

操作方法:

新浪云 – Memcached

使用示例

  • $mmc = new Memcached(); //使用本应用Memcache
  • 设置:
  • $mmc->set(“key”, “value” , “expire=0”); expire:过期时间
  • 读取:
  • $mmc->get(“key”);
  • 删除:
  • $mmc->delete(“key”);
    -
<?php
    $mmc = new Memcached(); //使用本应用Memcache

    $mmc = new Memcached("accesskey"); //使用其他应用的Memcache
    if ($mmc == false) {
        echo "mc init failed\n";
    } else {
        $mmc->set("key", "value");
        echo $mmc->get("key");
    }

?>

这里写图片描述

核心代码如下:

    $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $MsgType=$postObj->MsgType;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                             <MsgType><![CDATA[text]]></MsgType>
                             <Content>%s</Content>
                             <FuncFlag>0</FuncFlag>
                            </xml>";             
                //实例化
                $mem = new Memcached(); //使用本应用Memcache

                if($keyword=="天气"){
                    $mem->set($fromUsername."user","{$fromUsername}",$expire=30);
                    $mem->set("key","天气",$expire=30);        
                    $content="请输入地区,如北京进行天气情况查询";
                }
                else
                {
                    $struser=$mem->get($fromUsername."user");
                    $strkey=$mem->get("key");


                   if ($struser==$fromUsername and $strkey=="天气"){
                         //判断是否同一用户发送以及是否发送过天气
                         $url="http://api.map.baidu.com/telematics/v2/weather?location={$keyword}&ak=1a3cde429f38434f1811a75e1a90310c";

                         $fa=file_get_contents($url);
                         $f=simplexml_load_string($fa);
                         $city=$f->currentCity;
                         $da1=$f->results->result[0]->date;
                         $da2=$f->results->result[1]->date;
                         $da3=$f->results->result[2]->date;     
                         $w1=$f->results->result[0]->weather;
                         $w2=$f->results->result[1]->weather;
                         $w3=$f->results->result[2]->weather;      
                         $p1=$f->results->result[0]->wind;
                         $p2=$f->results->result[1]->wind;
                         $p3=$f->results->result[2]->wind;
                         $q1=$f->results->result[0]->temperature;
                         $q2=$f->results->result[1]->temperature;
                         $q3=$f->results->result[2]->temperature;  
                         $d1=$city.$da1.$w1.$p1.$q1;
                         $d2=$city.$da2.$w2.$p2.$q2;
                         $d3=$city.$da3.$w3.$p3.$q3;
                         $content=$d1.$d2.$d3;
                         if (empty($content))
                         {
                             $content="你输入的地区有误";}
                         }else{
                               $content="请先输入天气";
                         }
                }                              
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$content);
                echo $resultStr;

index.php整体代码如下:

<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    public function responseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];


                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $MsgType=$postObj->MsgType;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                             <MsgType><![CDATA[text]]></MsgType>
                             <Content>%s</Content>
                             <FuncFlag>0</FuncFlag>
                            </xml>";             
                //实例化
                $mem = new Memcached(); //使用本应用Memcache

                if($keyword=="天气"){
                    $mem->set($fromUsername."user","{$fromUsername}",$expire=30);
                    $mem->set("key","天气",$expire=30);       
                    $content="请输入地区,如北京进行天气情况查询";
                }
                else
                {
                    $struser=$mem->get($fromUsername."user");
                    $strkey=$mem->get("key");


                   if ($struser==$fromUsername and $strkey=="天气"){
                         //判断是否同一用户发送以及是否发送过天气
                         $url="http://api.map.baidu.com/telematics/v2/weather?location={$keyword}&ak=1a3cde429f38434f1811a75e1a90310c";

                         $fa=file_get_contents($url);
                         $f=simplexml_load_string($fa);
                         $city=$f->currentCity;
                         $da1=$f->results->result[0]->date;
                         $da2=$f->results->result[1]->date;
                         $da3=$f->results->result[2]->date;     
                         $w1=$f->results->result[0]->weather;
                         $w2=$f->results->result[1]->weather;
                         $w3=$f->results->result[2]->weather;      
                         $p1=$f->results->result[0]->wind;
                         $p2=$f->results->result[1]->wind;
                         $p3=$f->results->result[2]->wind;
                         $q1=$f->results->result[0]->temperature;
                         $q2=$f->results->result[1]->temperature;
                         $q3=$f->results->result[2]->temperature;  
                         $d1=$city.$da1.$w1.$p1.$q1;
                         $d2=$city.$da2.$w2.$p2.$q2;
                         $d3=$city.$da3.$w3.$p3.$q3;
                         $content=$d1.$d2.$d3;
                         if (empty($content))
                         {
                             $content="你输入的地区有误";}
                         }else{
                               $content="请先输入天气";
                         }
                }                              
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$content);
                echo $resultStr;
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];    

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

?>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值