Php 例如,使用cookie加速web应用程序

Php 例如,使用cookie加速web应用程序,php,jquery,mysql,caching,cookies,Php,Jquery,Mysql,Caching,Cookies,现在,我使用此代码在web应用程序中获取随机文本 $(document).ready(function() { $("#refresh").click(function(evt) { $("#content").load("load.php") evt.preventDefault(); }) }) Load.php正在从数据库加载随机文本。我能为他们做些什么来加速这次会议。如果有人也知道Web应用程序如何在没有3G和WiF

现在,我使用此代码在web应用程序中获取随机文本

 $(document).ready(function() {
      $("#refresh").click(function(evt) {
         $("#content").load("load.php")
         evt.preventDefault();
      })
    })

Load.php正在从数据库加载随机文本。我能为他们做些什么来加速这次会议。如果有人也知道Web应用程序如何在没有3G和WiFi的情况下使用,那就太好了。

您可以在php代码中添加缓存功能

加载随机文本时,将其写入
/cache/mytext.cache
并将unix时间戳写入
/cache/mytext.info

现在,在php脚本的顶部,读取
/cache/mytext.info
并检查它是否太旧,如果是,则生成一个新文本并更新mytext.info的时间戳,否则,将/cache/mytext.cache的内容作为文本加载

// Fetch the timestamp saved in /cache/mytext.info
$cachedate = file_get_contents('./cache/mytext.info', true);

// If timestamp + _× seconds_ is < of current time  
if(($cachedate + 3600) < time()) {

    // Fetch your new random text and store into $mytext
    // for example: $mytext = getrandomtext();

    // Write into /cache/mytext.cache your new random text
    $fp = fopen('./cache/mytext.cache', 'w');
    fwrite($fp, $mytext);
    fclose($fp);

    // Update timestamp written into /cache/mytext.info
    $fp = fopen('./cache/mytext.info', 'w');
    fwrite($fp, time());
    fclose($fp);

}

// Your random text is into $mytext
$mytext = file_get_contents('./cache/mytext.cache', true);

// Print it with echo
echo $mytext;
//获取保存在/cache/mytext.info中的时间戳
$cachedate=file_get_contents('./cache/mytext.info',true);
//如果时间戳+\ux秒\uu小于当前时间
如果(($cachedate+3600)
在浏览器中缓存一个请求,并使用jQuery的真正有用且未充分利用的延迟,其中ajax调用就是一个实例,以确保仅在内容到达后加载它

$(document).ready(function() {
  var nextContent = $.get('/load.php');//preload the result of the first random text request
  var clicked = false;

  $("#refresh").click(function(evt) {
     if (!clicked) { // prevent sending loads of requests if user reclicks impatiently
        clicked = true;

        nextContent.done(function (response) { 
          console.log('yay')
          $("#content").html(response); // print the text
          clicked = false;
          nextContent = $.get('/load.php'); //preload the next request
        })
     }
     evt.preventDefault();
  });
})

我应该用我的旧代码替换它吗?当我替换它时,它现在不起作用。我编辑了代码示例,希望修复任何错误-这是一个正在工作的fiddle-,其中响应时间几乎是即时的-您想降低请求响应时间或在客户端缓存随机文本的结果吗?选择你想要的。我认为缓存对用户来说是最好的@Denisermolinh那我怎么打印文本呢?我是新来的,抱歉@费兹·弗拉斯塔