You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by howard chen <ho...@gmail.com> on 2008/04/16 04:59:31 UTC

[users@httpd] Apache dynamic page caching?

Hi,

Are there any existing method for Apache which can do the followings?

E.g.

When a user first go to a page, www.example.com/article.php?id=123, my
PHP problem will
generated a static copy of HTML under a temp folder (e.g.
/tmp/article/md5(123).html), so following requests to the
page will not require SQL query, and my PHP will fetch the page from
cache directly.

However, given that Apache fetch a static page is faster the PHP, are there
any method for apache to look up the cache folder, and if exist, it
will skip the
PHP and fetch the page directly?

The trick I think is during URL rewrite, how to MD5() the query string
and map into
a file name.


Thanks.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Apache dynamic page caching?

Posted by Joshua Slive <jo...@slive.ca>.
On Wed, Apr 16, 2008 at 1:41 PM, howard chen <ho...@gmail.com> wrote:
> Hi all,
>
>
>  On Wed, Apr 16, 2008 at 9:21 PM, Narendra Verma
>  <na...@impetus.co.in> wrote:
>  > Hi Howard,
>  >  1. Be sure to load following module
>  >
>  >     LoadModule headers_module modules/mod_headers.so
>  >
>  >  2. Add following Header directive at the last of httpd.conf file.
>  >
>  >     Header add Cache-Control max-age=3600              (in seconds)
>  >
>  >  Here we can decide that what is the maximum age of cached response.
>  >  If max-age passed then apache again sends request to backend(tomcat) for re-caching, if the body of response is not found modified then 304 status is found means existing cache would be used.If body changed then then it is re-cached. You can use any amount of time to decide re-caching.
>  >
>
>  The problem is I don't know to cache for how long. Maybe 1 month, or
>  maybe one second, consider a blog post, it will be only updated when a
>  user leave a comment, but the exact time is unpredictable.
>
>  Currently I am using PHP / Smarty cache, if a user leave a comment, I
>  will remove cache from the file system and the cache will be
>  re-generated next time if cache miss.
>
>  The only draw back is since for a cached case, I am sending HTML via
>  PHP, so why I don't send the cache via Apache? The lighttpd Cache Meta
>  Language (http://en.wikipedia.org/wiki/Cache_Meta_Language) is what
>  exactly doing what I want, but I am just wondering if Apache users
>  have think of this before? )
>
>  On the other hand, mod_cache seems can't do what I want. Squid did
>  (wikipedia is using similar approach with "purge' multicast). Of coz
>  setting a squid is another overhead for my simple use.

How, exactly, did you have squid configured?

There are two ways to handle this problem in general:

1. You check for cache freshness on every request; or
2. You invalidate the cache when you know it becomes bad.

For the first option, apache will send if-modified-since requests to
the back-end on expired content (if the cache is in its own http
layer). You can tune your php code to handle these quickly for the
not-modified case, letting the cache send its copy.

For the second option, there are two ways to invalidate a cache entry.
First, you can send an HTTP request to the cache with Cache-control:
max-age=0. Second, you can manipulate the cache directly on the
filesystem. To see how this works, see the source code to
htcacheclean.

Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Apache dynamic page caching?

Posted by howard chen <ho...@gmail.com>.
Hi all,

On Wed, Apr 16, 2008 at 9:21 PM, Narendra Verma
<na...@impetus.co.in> wrote:
> Hi Howard,
>  1. Be sure to load following module
>
>     LoadModule headers_module modules/mod_headers.so
>
>  2. Add following Header directive at the last of httpd.conf file.
>
>     Header add Cache-Control max-age=3600              (in seconds)
>
>  Here we can decide that what is the maximum age of cached response.
>  If max-age passed then apache again sends request to backend(tomcat) for re-caching, if the body of response is not found modified then 304 status is found means existing cache would be used.If body changed then then it is re-cached. You can use any amount of time to decide re-caching.
>

The problem is I don't know to cache for how long. Maybe 1 month, or
maybe one second, consider a blog post, it will be only updated when a
user leave a comment, but the exact time is unpredictable.

Currently I am using PHP / Smarty cache, if a user leave a comment, I
will remove cache from the file system and the cache will be
re-generated next time if cache miss.

The only draw back is since for a cached case, I am sending HTML via
PHP, so why I don't send the cache via Apache? The lighttpd Cache Meta
Language (http://en.wikipedia.org/wiki/Cache_Meta_Language) is what
exactly doing what I want, but I am just wondering if Apache users
have think of this before? )

On the other hand, mod_cache seems can't do what I want. Squid did
(wikipedia is using similar approach with "purge' multicast). Of coz
setting a squid is another overhead for my simple use.

Howard

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


RE: [users@httpd] Apache dynamic page caching?

Posted by Narendra Verma <na...@impetus.co.in>.
Hi Howard,
1. Be sure to load following module

    LoadModule headers_module modules/mod_headers.so

2. Add following Header directive at the last of httpd.conf file.

    Header add Cache-Control max-age=3600              (in seconds)

Here we can decide that what is the maximum age of cached response.
If max-age passed then apache again sends request to backend(tomcat) for re-caching, if the body of response is not found modified then 304 status is found means existing cache would be used.If body changed then then it is re-cached. You can use any amount of time to decide re-caching.

Thanks
Narendra

-----Original Message-----
From: howard chen [mailto:howachen@gmail.com]
Sent: Wednesday, April 16, 2008 4:09 PM
To: users@httpd.apache.org
Subject: Re: [users@httpd] Apache dynamic page caching?

Hi,


On Wed, Apr 16, 2008 at 3:53 PM, Nick Kew <ni...@webthing.com> wrote:
>
>  mod_cache.
>
>

The problem with mod_cache is when user update a page, how do I tell
the cache should be removed from mod_cache?

Currently I stored the cache in local file system and it can be easily
unlink() to force an update. In the context of dynamic page, I don't
have field such as last modifed or etag.


Howard

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Apache dynamic page caching?

Posted by Joshua Slive <jo...@slive.ca>.
On Wed, Apr 16, 2008 at 8:10 AM, Guillaume Duchesneau
<gd...@taleo.com> wrote:
> To my knowledge, you cannot directly invalidate an entry in the cache.

You can by making an HTTP request to the cache with appropriate
Cache-Control headers or by directly manipulating the cache (see
htcacheclean).

Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


RE: [users@httpd] Apache dynamic page caching?

Posted by Guillaume Duchesneau <gd...@taleo.com>.
To my knowledge, you cannot directly invalidate an entry in the cache.
mod_cache uses a expiration strategy to invalidate items in the cache.
You need to set expiration headers in your response to tell mod_cache
when it should be removed from the cache.

Guillaume


-----Original Message-----
From: howard chen [mailto:howachen@gmail.com] 
Sent: Wednesday, April 16, 2008 6:39 AM
To: users@httpd.apache.org
Subject: Re: [users@httpd] Apache dynamic page caching?

Hi,


On Wed, Apr 16, 2008 at 3:53 PM, Nick Kew <ni...@webthing.com> wrote:
>
>  mod_cache.
>
>

The problem with mod_cache is when user update a page, how do I tell
the cache should be removed from mod_cache?

Currently I stored the cache in local file system and it can be easily
unlink() to force an update. In the context of dynamic page, I don't
have field such as last modifed or etag.


Howard

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server
Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Apache dynamic page caching?

Posted by howard chen <ho...@gmail.com>.
Hi,


On Wed, Apr 16, 2008 at 3:53 PM, Nick Kew <ni...@webthing.com> wrote:
>
>  mod_cache.
>
>

The problem with mod_cache is when user update a page, how do I tell
the cache should be removed from mod_cache?

Currently I stored the cache in local file system and it can be easily
unlink() to force an update. In the context of dynamic page, I don't
have field such as last modifed or etag.


Howard

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Apache dynamic page caching?

Posted by Nick Kew <ni...@webthing.com>.
On 16 Apr 2008, at 03:59, howard chen wrote:
> Hi,
>
> Are there any existing method for Apache which can do the followings?

mod_cache.

> E.g.
>
> When a user first go to a page, www.example.com/article.php?id=123, my
> PHP problem will
> generated a static copy of HTML under a temp folder (e.g.
> /tmp/article/md5(123).html), so following requests to the
> page will not require SQL query, and my PHP will fetch the page from
> cache directly.

If you're using PHP, then it's up to you to make it cacheable.  Set a  
Last-Modified
and perhaps an ETag header, and respond as aggressively as you like to
cacheing request headers.

-- 
Nick Kew

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org