zoukankan      html  css  js  c++  java
  • PHP 开发 APP 接口 学习笔记与总结

    用于 linux 执行 crontab 命令生成缓存的文件 crop.php

    <?php
    //让crontab 定时执行的脚本程序
    require_once 'db.php';
    require_once 'file.php';
    
    $sql = 'select * from review where is_enabled = 1 order by creation_time desc limit 6';
    try{
        $connect = DB::getInstance()->connect();
    }catch(Exception $e){
        //如果捕获异常,记录错误日志
        file_put_contents('logs/'.date('Y-m-d').'.txt',$e->getMessage());
        return; 
    }
    $res = mysql_query($sql,$connect);    
    $vals = array();
    while($val = mysql_fetch_assoc($res)){
        $vals[] = $val; //二维数组
    }
    //加入缓存
    $file = new Cache();
    if($vals){
        $file->cacheData('index-cron_cache',$vals);
    }else{
        file_put_contents('logs/'.date('Y-m-d').'.txt','没有相关数据');
        return;
    }

    注意捕获数据库连接的异常时,应该记录在日志文件中。

    测试缓存的文件 list2.php

    <?php
    require_once 'response.php';
    require_once 'file.php';
    
    $file = new Cache();
    $data = $file->cacheData('index-cron_cache');
    //如果获取到数据,则生成接口数据
    if($data){
        return Response::show(200,'获取缓存成功',$data);
    }else{
        return Response::show(400,'首页数据获取失败',$vals);
    }
  • 相关阅读:
    《需求分析与系统设计》第二篇阅读体会
    《需求分析与系统设计》第一篇阅读体会
    《编写有效用例》第二篇阅读体会
    项目目标文档
    字符流
    字节流
    递归
    File类
    JDBC接口和工具类
    异常
  • 原文地址:https://www.cnblogs.com/dee0912/p/4357350.html
Copyright © 2011-2022 走看看