Forum OpenACS Q&A: Re: Strange cross-caching of users

Collapse
Posted by Bart Teeuwisse on

What does the community think of this caching scheme (based on Tom's suggestion):


ad_proc cache_control {conn why} {

    Control cacheing of images, static and dynamic pages.

} {
    set url [string tolower [ad_conn url]]
    switch -regexp $url {

        .gif$ -
        .jpe?g$ -
        .png$ {

            # Expire images after an hour.

            set seconds [expr 60 * 60]
            ns_set update [ad_conn outputheaders] Expires \
                [ns_httptime [expr $seconds + [ns_time]]]
            ns_set update [ad_conn outputheaders] Cache-Control \
                "max-age=$seconds"
       }

        .css$ -
        .html?$ {

            # Expire static pages after half an hour.

            set seconds [expr 30 * 60]
            ns_set update [ad_conn outputheaders] Expires \
                [ns_httptime [expr $seconds + [ns_time]]]
            ns_set update [ad_conn outputheaders] Cache-Control \
                "max-age=$seconds"
        }

        default {

            # Expire all other pages immediately.

            ns_set update [ad_conn outputheaders] Expires \
                [ns_httptime [ns_time]]
            ns_set update [ad_conn outputheaders] Cache-Control \
                "no-cache,no-store,must-revalidate,proxy-revalidate"
            ns_set update [ad_conn outputheaders] Pragma \
                "no-cache"
        }
    }
    return filter_ok
}

# Register the cache control proc.

ad_register_filter -critical f trace * /* cache_control

I've saved this as cache-control-init.tcl in the /tcl directory.

/Bart