欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-23 10:14:00
...

缓存

  • 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");
    }

?>

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

核心代码如下:

    $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;
        }
    }
}

?>
相关标签: 缓存