Forum Moderators: coopster

Message Too Old, No Replies

PHP caching output of a CGI script

It stops caching when virtual() is called

         

donovanh

2:50 pm on Jun 2, 2004 (gmt 0)

10+ Year Member



Hi,

I am calling a script using php's virtual() function, which works in the same way as adding <!--include virtual="".. to a page. This works nicely and my site is working well.

I'm looking for a way to speed up some of the index pages, which call a script several times. I've tried caching the output using cache_lite, cgi_cache and several other hotscripts scripts, but each has the same problem. They all cache the page up to the point where virtual() is called, then stop. Until the cache timer runs out, only the top fragment of the page is displayed. The full page then displays, but only once, and if the page is refreshed, only the top fragment again.

Has anyone seen this problem and know of a reliable way to cache pages that are called in this way?

Thanks,

Don

donovanh

11:00 am on Jun 3, 2004 (gmt 0)

10+ Year Member



Update: I've been trying other methods, including output buffering but always the same problem. The fact that i'm using virtual() to call a cgi script is the problem.

Has anybody else encountered problems with virtual() and caching?

jatar_k

7:00 pm on Jun 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I haven't used virtual as I have no need.

seems strange, anyone else have any thoughts on this or use virtual?

coopster

6:24 pm on Jun 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The only thing I noted was that the manual states that when Apache performs the subrequest all buffers are terminated and flushed to the browser, pending headers are sent too.

Could this be causing some conflict with the caching?

Hanu

8:05 pm on Jun 5, 2004 (gmt 0)

10+ Year Member



Well, I've never used cache_lite and brothers, but it seems pretty obvious to me that they do not work with virtual(). On a cache miss, the caching solutions for PHP redirect all output generated by PHP scripts into the cached copy of the page. But they cannot intercept the output of a CGI which writes its output directly to the browser, bypassing any PHP output buffering.

It seems that you have two options, assuming you use cache_lite:

Call $cache- >end() before calling virtual() and cache->start() with a different id after that. The problem with this is that the output of the CGI will not be cached.

Alternatively, use Pear's HTTP_Request to generate a real request to the CGI as opposed to a virtual subrequest. Fetch the output into a string and echo that string. This is the more elegant solutions because the CGI output is cached as well but it might be tricky to setup the query string, http headers and the request body (POST vars) for the CGI request.

donovanh

10:49 pm on Jun 5, 2004 (gmt 0)

10+ Year Member



Interesting.. I'll look into the Pear HTTP_REQUEST option. The ability to cache the cgi output would certainly be the best way, as it currently slows the page quite considerably.

Thanks for the replies!

Don