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 luckysheet
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题