Forum Moderators: coopster

Message Too Old, No Replies

How to enable responses to be cached in PHP?

         

ericazhj

7:18 am on Mar 6, 2007 (gmt 0)

10+ Year Member



Hi,

I want to cache the responses produced by PHP for XMLHttp request. So I configured Apache mod_cache. The mod_cache could cache some web pages or javascripts. However, it could not work for the dynamic responses for XMLHttp request. I really do not know whether it is some error on my configuration or my PHP script. So I just post some of the PHP script here, which is relative to cache. Could you help me to analyze it? Thanks.

For mod_cache, I use httpd.conf to configure it:

MaxKeepAliveRequests 10000
<IfModule mpm_winnt.c>
ThreadsPerChild 1900
MaxRequestsPerChild 10000
</IfModule>

LoadModule cache_module modules/mod_cache.so

<IfModule mod_cache.c>
CacheForceCompletion 100
CacheDefaultEXPire 3600
CacheMaxExpire 86400
CacheLastModifiedFactor 0.1
LoadModule disk_cache_module modules/mod_disk_cache.so

<IfModule mod_disk_cache.c>
CacheEnable disk /
CacheRoot c:/cacheroot
CacheSize 327680
CacheDirLength 4
CacheDirLevels 5
CacheGcInterval 4
</IfModule>

#LoadModule mem_cache_module modules/mod_mem_cache.so
<IfModule mod_mem_cache.c>
CacheEnable mem /
MCacheSize 8192
MCacheMaxObjectCount 10000
MCacheMinObjectSize 1
MCacheMaxObjectSize 51200
</IfModule>
</IfModule>

On client, I use ajax technology and XMLHttpRequest and on server, I use php script.
...
header("Cache-Control: public"); // HTTP/1.1
header("Expires: " .gmdate ("D, d M Y H:i:s", time() + 60 * 10). " GMT");
header( "Last-Modified: " .gmdate( 'D, d M Y H:i:s' ). " GMT" );
echo $response;
....

mcibor

8:05 am on Mar 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it is valid for an hour you could create html files from your php, eg by using cron job.
Or make a small caching script. There were threads on this here

[webmasterworld.com...]
[webmasterworld.com...]
[webmasterworld.com...]
[webmasterworld.com...]

Regards
Michal

ericazhj

8:21 am on Mar 6, 2007 (gmt 0)

10+ Year Member



Thanks a lot. Those links teach me to provide simple cache using php. However, here, I want to let a proxy cache or browser cache to cache the responses produced by PHP. This means for php, I just used HTTP 1.1 protocol headers, which could be identified by those caches.

mcibor

8:26 am on Mar 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Headers should be enough, but many browsers disregard them.
I don't think there is another option to force proxy / browser to store cache.

Maybe changing script to html will help, but still it's not the solution

Michal

PS. Do you send those headers also in Ajax files?

ericazhj

6:48 pm on Mar 6, 2007 (gmt 0)

10+ Year Member



In fact, I just make sure the responses to have those headers. For Ajax one, I do not add those headers. Is this the problem?

mcibor

11:55 am on Mar 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It maybe the issue, that ajax is not cached as well.

Try to add headers and we'll see what happens