memcache

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="jquery-2.1.1.min.js"></script>
</head>
<body>
<center>
    <table>
        <tr>
            <td>用户名:</td>
            <td><input type="text"/></td>
        </tr>
        <tr>
            <td>手机号码:</td>
            <td><input type="text" id="tel"/><span></span></td>
        </tr>
        <tr>
            <td><button>查询</button></td>
        </tr>
    </table>
</center>
</body>
</html>
<script>
$(function(){
    $("button").click(function(){
        var obj=$(this);
        var tel=$("#tel").val();
        var url='http://www.lijian.com/m9/d919a/index.php?tel='+tel;
        $.get(url,function(msg){
         //alert(msg)
//            var str='';
//            for(var i= 0;i<msg.length;i++){
//                str+='<span>'+msg[i].name+'</span>'
//            }
            $("span").html(msg);
        },'json')
    })
})
</script>

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/20
 * Time: 9:28
 */
header("content-type:text/html;charset=utf-8");
//$pdo=new PDO('mysql:host=localhost;dbname=test','root','root');
//$pdo->exec("set names utf8");
//开启缓存
$mem=new Memcache();

$mem->connect("127.0.0.1",11211);
//清除缓存
//$mem->flush();die;
$tel=$_GET['tel'];

//从缓存取出被查询了的次数
$num=$mem->get($tel);
set_time_limit(0);
$url = "http://api.k780.com:88/?app=phone.get&phone=".$tel."&appkey=20900&sign=b07840f44dc8e8c7b08dd44b12032338&format=json";
if($num){
//已存在
    //每一次加一
    $num++;
    $mem->set($tel,$num);
    //再判断次数
    if($num>5){
        //大于五次取memcache数据
        $str=$mem->get('data'.$tel);
        echo $str;
    }elseif(
        //第五次取出的数据放到memcache
        $num==5
    ){
//        $data=$pdo->query("select * from tel WHERE tel='$tel'")->fetchAll(PDO::FETCH_ASSOC);
//        $str=json_encode($data);
//        $mem->set('data'.$tel,$str);
//        echo $str;
        $str = file_get_contents($url);
        $data = json_decode($str,true);
        $operators = $data['result']['operators'];
        echo json_encode($operators);

    }else{
        //直接查输出
//        $data=$pdo->query("select * from tel WHERE tel='$tel'")->fetchAll(PDO::FETCH_ASSOC);
//        $str=json_encode($data);
//        echo $str;
        $str = file_get_contents($url);
        $data = json_decode($str,true);
        $operators = $data['result']['operators'];
        echo json_encode($operators);

    }
}else{
    //第一次访问
    $mem->set($tel,1);
//    $data=$pdo->query("select * from tel WHERE tel='$tel'")->fetchAll(PDO::FETCH_ASSOC);
//    echo json_encode($data);
    $str = file_get_contents($url);
    $data = json_decode($str,true);
    $operators = $data['result']['operators'];
    echo json_encode($operators);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值