dsfdsf21321 2015-04-16 14:56
浏览 75
已采纳

使用Slim Framework,HTTP缓存不会过期

I'm using Slim framework in order to develop an API.

I wanted to cache some request in the HTTP cache with:

$app->etag('Unique-ID')

But the time expiration didn't seems to work :

$app->expires('10 seconds')

When i look the headers with Chrome, first call i get a 200 Status Code : OK.

Second call, i get a 304 Status Code : OK.

Waiting 30 seconds.

Third call, i still get a 304 Status Code : NOT OK in my mind.

Should i not get a 200 Status Code because the cache is expired ?

Thank you.

  • 写回答

1条回答 默认 最新

  • dsgsdg206050 2015-04-17 15:23
    关注

    In short

    Expires header instructs the browser how long it should cache content in client side cache. Browser will serve the content from client side cache until the expiration date is reached.

    When client side cache is expired browser will send request to server again. Request includes If-None-Match header with the previous Etag value it received from server. If value of If-None-Match header still matches current Etag value on server it will respond with 304 Not Modified.

    When you use Etag and If-None-Match headers and want server to send updated content, value of Etag header must change.

    Longer explanation

    Lest assume you have the following code.

    $app = new \Slim\Slim();
    
    $app->get("/hello", function() use ($app){
        $app->etag("unique-etag-1");
        echo "Hello world.
    ";
    });
    
    $app->run();
    

    Then you make a request.

    $ curl --include http://localhost:8080/hello
    
    HTTP/1.1 200 OK
    Host: localhost:8080
    Connection: close
    X-Powered-By: PHP/5.6.2
    Content-type: text/html;charset=UTF-8
    Etag: "unique-etag-1"
    
    Hello world
    

    On subsequent requests browser will send If-None-Match request header. Value of this header is the same as value of previously received Etag header.

    When Slim receives the request it compares the value of If-None-Match header to the value you set with $app->etag() call. If these match 304 Not Modified will be returned.

    $ curl --include --header 'If-None-Match: "unique-etag-1"' http://localhost:8080/hello
    
    HTTP/1.1 304 Not Modified
    Host: localhost:8080
    Connection: close
    X-Powered-By: PHP/5.6.2
    Etag: "unique-etag-1"
    Content-type: text/html; charset=UTF-8
    

    If the content of URI changes or you want to browser to refetch the content for some other reasons, change the value of Etag header.

    $app->get("/hello", function() use ($app){
        $app->etag("unique-etag-2");
        echo "Hello world.
    ";
    });
    

    Now when browser makes new request you will get 200 OK response.

    curl --include --header 'If-None-Match: "unique-etag-1"' http://localhost:8080/hello
    HTTP/1.1 200 OK
    Host: localhost:8080
    Connection: close
    X-Powered-By: PHP/5.6.2
    Content-type: text/html;charset=UTF-8
    Etag: "unique-etag-2"
    
    Hello world.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler