donglaohua1671 2014-07-28 04:21
浏览 21
已采纳

MySql或Json的多请求?

I have 1000 json files in my server and users request their file with something like this mysite.com/request.php?file=id and i should show them id.json .But before i show them i should check if id.json need to be updated or not .

My json files are something like this:

{
  "response": {
        ......,
  "lastupdate":14323342

 }
}

In last update i store the last update time in seconds and if the current time is bigger than 1 hour i should update my file from somewhere else . Now my question : Is it good to save lastupdate in Mysql or in each json file ?
If i use mysql i need 1000 rows and each row should have 2 columns ,first columns are id and second columns are lastupdate.And my users are more than 10,000 and each day and server hardwares is important for me.

  • 写回答

1条回答 默认 最新

  • duanlai1855 2014-07-28 04:51
    关注

    If you're grabbing a specific file (not opening a "flatfile database" and search something), it should be faster than querying a mySQL db.


    A thought form a similar case; if the same ID could be requested a number of times in less than an hour, AND if you actually don't need to update the last update time in the "json file" until a user is asking, then when there's a request to request.php?file=id I'll:

    "try" to get and decode the json file, put the decoded to (eg) $data
    
    if the file doesn't exist OR lastupdate is more than an hour
    {
    
        update the lastupdate, and update $data
    
        update the json file
    
    }
    
    show $data
    

    ... it's basically a simple caching to try to get data from file instead of db, and to prevent updating (db query) when no one is asking.

    You can safely rm -rf everything in the "cache folder" anytime.

    if ( $data = file_get_contents( 'path_to_cache/' . $id . '.json' ) )
        $data = json_decode( $data );
    
    if ( empty( $data ) || $data->lastupdate < time() - 3600 )
    {
        // do query, put to $data
        // encode $data, put to 'path_to_cache/' . $id . '.json'
    }
    
    echo $data;
    

    ... and actually, in my case I don't need to put lastupdate to DB. So I can simply use the json file's filemtime().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错