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 Llama如何调用shell或者Python
  • ¥20 eclipse连接sap后代码跑出来空白
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案