Re: using APC as content-cache?

From: Date: Fri, 29 Apr 2005 10:05:06 +0000
Subject: Re: using APC as content-cache?
References: 1 2  Groups: php.pecl.dev 
Request: Send a blank email to pecl-dev+get-2323@lists.php.net to get a copy of this message
Rasmus Lerdorf wrote:
With Turck mmCache/eAccelerator you can do something like that: eaccelerator_cache_page( $_SERVER['PHP_SELF'].'?GET='.serialize($_GET), 1800); php without any cache: 8.91 php with opcode-cache: 43.27 php with content-cache: 543.48 from: http://turck-mmcache.sourceforge.net/index_old.html#bench
Interesting idea. Although it seems like it could be done almost completely in user-space, couldn't it? Perhaps something like this wrapper set as your auto_prepend? $page_id = md5($_SERVER['REQUEST_URI']); if(!$content = apc_fetch($page_id)) {
    ob_start();
    require $_SERVER['SCRIPT_FILENAME'];
    $content = ob_end_clean();
    apc_store($page_id, $content, 1800);
} echo $content; exit;
Yes, that's what I thought about. But I'm quite sure it will be much slower. btw.: If I cache a page, eAccelerator sends an Etag HTTP-header, so most clients will send an if-non-match during cache-ttl and get "304 Not Modified"! Perhaps it's a good idea to send Expires-Header... too (to avoid requests during cache-ttl)?
Would be interesting to see the performance numbers on this approach.
I tried to do some tests, but there are some php5-related bugs in eAccelerator, so not everything I tried worked. I benchmarked index.php of phpmyadmin using ab: 1. no caching: 5.03 2. opcode caching: 15.02 3. content caching: 177.65 4. Rasmus idea(ob): 155.40 5. static request(html): 909.09 OK, the difference between 3 and 4 is not as big as I expected. But there is a problem with eaccelerator_cache_page(), it outputs something the browser does not understand, I think a php5 problem.
Then again, for full content-caching like this, a 404 handler which saves a static version of the content plus a cron job that periodically removes files older than 30 minutes might be even more effective.
But you cannot handle different GET-parameters, so mod_rewrite with RewriteCond and "-f" would be an option.
I would think that something like the above would be most effective when used in a partial page caching context.
I think today memory is not very expensive anymore (of course it depends on your application), so it would be possible to store a compressed and/or uncompressed versions in memory (and/or on disk if you want). You also could send caching HTTP-Headers to completely avoid requests. "eAccelerator API": http://cvs.sourceforge.net/viewcvs.py/eaccelerator/eaccelerator/README?view=markup I'm trying to solve my following problem: I have a complex php-script which includes a lot of things, so it becomes quite slow. For 90-95% of my requests only a very simple information is requested, but I have to authenticate the user (I need his User-ID from his Session). I cannot cache this information, it must be checked on every request. I'm looking for a way to check it if possible without the need of executing a php-script (only on cache-miss). So I could not use something like a 404 handler, I tried to use mod_rewrite, it is possible to find out the Session-ID there (check query-string and cookie), but I cannot read anything from his session. My only idea was, to use somthing like a filesystem-based cache, which means I could write empty files with his session-id, and check from mod_rewrite if this file is available ("-f"). But that is not very nice, and becomes more complex if I need additional information. I need something in the webserver (an own module?), which can read php-session, which can be configured and make a caching-decision. Perhaps I should use an encrypted cookie, not a local session-file to store that information. But there is still the problem how to communicate with the php-application. Only the php-application knows when the cache is expired, so this must be told to the webserver-cache-module. But how? By file? By SHM? Database? I don't like any of this options very much. But I think something like this would be by far the fastes methode. Perhaps somehow comparable to CML(Cache Meta Language) of lighttpd (http://www.incremental.de/talks/phpconf-2003-caching/lighttpd_cache/cml.html sorry, I don't know of any english version). Using an opcode-cache as content-cache is not the fastest solution, but it's flexible and simple (eAccelerator is more flexible at the moment, see its API above), and does not require writing a webserver-module in C ;-) Best regards Andreas

« previous php.pecl.dev (#2323) next »