Hi,

I'm having difficulties trying to get PHP caching using header with apache2's mod_cache.

From my understanding, PHP header caching using cache-control and expire needs mod_cache running.
I have set a default timeout in mod_cache but it only caches some contents. Using the PHP header should enable us to cache more stuff and overwrite the expire time set in cache_control.
I've done a few tests where header cache expires after 30 secs which works but afterwards it always refreshes the contents from the server.

Have I missed out something on my view of caching using header and mod_cache? Any help appreciated 🙂

    In order to enable caching by mod_cache, I believe you need to set the following headers:

    Last-modified: (mandatory)
    Expires: (optional)
    Vary: (desirable)
    

    Any request which has no Last-modified header, won't be cached at all as there is no way for its age to be determined.

    These should be rfc-822 date/time values, as made with date('r')

    The "Expires" header will tell it when the object expires - it will never cache it after it's expired.

    The "Vary" header will tell the cache what header fields it needs to consider to vary the response content, e.g. if you need to vary the contents based on "Accept-language" to provide appropriately localised pages, or "Cookie" to vary by cookie.

    Read the HTTP RFCs for more info.

    Apache's mod_cache works exactly like a proxy server's cache, except it's built into the server.

    Mark

      Thanks for your help, I have some further questions:

      • could last mod date be replaced by cache-control max-age?
      • if the script is dynamic how can we do the last mod date?

      Cheers,

        Alysum wrote:

        Thanks for your help, I have some further questions:
        - could last mod date be replaced by cache-control max-age?

        No, that does something different. Normally if you want the cache to only cache for a limited time, you should use "Expires:".

        - if the script is dynamic how can we do the last mod date?

        Determine when the most recently modified item on the page was modified and use that.

        Normally this will mean storing some column in the database to store the last modified date, e.g. for a news article.

        e.g. for a "user profile" page, you could use the date it was last modified. For a search results page, you could just use the date it was generated.

        You don't have to provide it for every page, but pages without it won't be cached.

        Mark

          Write a Reply...