drn61317 2012-02-17 07:34
浏览 185
已采纳

缓存JSON:Apache,PHP,jQuery

I'm trying to cache JSON content generated by a php script from database. However the dataset is very stable, there are very few changes or additions. Meaning data could go unchanged for weeks. The issue is that it contains a LOB column and that just takes a noticeable time to load, longer compared to supplying the json from a text file meaning git is the actual database call that makes it slow.

I'm displaying the data in a table with pagination(datatables jquery plugin) and for each page change the data is fetched from database again, also when going back to the previous page.

I've tried following:

"beforeSend": function (request)
{
    request.setRequestHeader("cache-control", "max-age=86400");
},

Does not work.

I tried mod_expires:

ExpiresActive On
ExpiresDefault "access plus 4 hours"
ExpiresByType application/javascript "access plus 1 day"
ExpiresByType application/json "access plus 1 day"

Does not work.

Therefore I assume all these settings are for real files on the file system only and not for dynamic generated stuff?

I would prefer a configurable approach meaning using Apache/PHP since I will not have full control over the server.

EDIT BEFORE FIRST ANSWER:

Note that the JSON contains multiple records so a key/value store would be kind of difficult to avhieve. The key would have to contain a lot of stuff: Query/filter expression and the requested page for paging.

EDIT 2:

Development and prod. are Windows... so memcached is not really an option...

EDIT 3:

I've tried kristovaher solution but does not work. The cache headers are not in the response all the time and after some plaing around I believe I determined the issue: I'm required do use NTLM authentication and when doing 2 request shortly after each other it works fine, however if you wait a bit, it seems the user is re-authenticated and then the cache control header is "lost".

  • 写回答

2条回答 默认 最新

  • duanou3868 2012-02-23 08:42
    关注

    I've played around a bit and come to the conclusion that caching with client side and using headers does not seem to work in my case. Maybe I'm doing it wrong, maybe it is the way my application works or web server config.

    Anyway my solution was to use APC:

    http://www.php.net/manual/en/book.apc.php

    I'm using windows, the appropriate binaries can be found here:

    http://downloads.php.net/pierre/

    which one depends on your PHP version and how it was compiled (with vc6 or vc9). The php_apc.dll will need to be put in your php extension directory and you will need to add the line

    extension=php_apc.dll 
    

    to the php.ini

    Then you bascially do:

    if (apc_exists($key)){
       return apc_fetch($key);
    }
    // get data from database because it was not in the cache
    //...
    //add data to cache
    apc_add($key, $result);
    

    If data on my page is uncached it took around 1-2s to load. Well thats not bad but feels very laggy. if data is in cache it is more like 20-30ms. of course this difference is very noticeable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测