Forum Discussion

Brett_Channon_1's avatar
Brett_Channon_1
Icon for Nimbostratus rankNimbostratus
Aug 29, 2006

cache based on content type

Hi all,

I wrote this in a bit of a hurry (can you tell!) and figure there must be a better/more efficient way of writing this..


 when HTTP_RESPONSE {
  if { [HTTP::header Content-Type] contains "image/" } {
    HTTP::header replace Cache-Control public,max-age=604800,must-revalidate,proxy-revalidate
  } elseif { [HTTP::header Content-Type] equals "text/css" } {
    HTTP::header replace Cache-Control private,max-age=604800,must-revalidate,proxy-revalidate
  } elseif { [HTTP::header Content-Type] equals "application/x-javascript" } {
     HTTP::header replace Cache-Control private,max-age=604800,must-revalidate,proxy-revalidate
  } else {
     HTTP::header replace Cache-Control private
  }
}

Since the javascript and css content types can be treated the same I'm sure there's a better way of coding this particular part. Also, rather than set the header to be 'private' if the content type doesn't match I want to just leave it as is..

Thanks

Brett
  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    I would try a switch:
    when HTTP_RESPONSE {
       switch -glob [HTTP::header Content-Type] {
          *image/* {
             HTTP::header replace Cache-Control public,max-age=604800,must-revalidate,proxy-revalidate
          }
          text/css -
          application/x-javascript {
             HTTP::header replace Cache-Control private,max-age=604800,must-revalidate,proxy-revalidate
          }
          default {
             HTTP::header replace Cache-Control private
          }
       }
    }

    You could also combine the two similar cases merely using an "or":
    ...
    } elseif { ( [HTTP::header Content-Type] equals "text/css" ) or ( [HTTP::header Content-Type] equals "application/x-javascript" ) } {
    HTTP::header replace Cache-Control private,max-age=604800,must-revalidate,proxy-revalidate
    } else {
    ...
    However, this is slightly less efficient because the HTTP::header command still has to be evaluated multiple times.

    HTH