cache
4 TopicsCache Expire
Problem this snippet solves: This iRule sets caching headers Expires and Cache-Control on the response. Mostly the use case will be to set client-side caching of static resources as images, stylesheets, javascripts etc.. It will honor, by default, the headers set by the origin server, then give precedence to the resource's mime-type (over it's file-extension). Also, if the origin server supplies the mime-type of the resource then file-extension entries are not considered. Code : # Expires iRule, Version 0.9.1 # August, 2012 # Created by Opher Shachar (contact me through devcentral.f5.com) # (please see end of iRule for additional credits) # Purpose: # This iRule sets caching headers on the response. Mostly the use case will be # to set client-side caching of static resources as images, stylesheets, # javascripts etc. # Configuration Requirements: # Uses a datagroup named Expires of type string containing lines that # specify mime-types or extention for the name and, the number of seconds the # client should cache the resource for the value. ex.: # "image/" := "604800" # ".js" := "604800" # The Content-Type, if specified, takes precedence over the file extention. when RULE_INIT { # Enable to debug Expires via log messages in /var/log/ltm # (2 = verbose, 1 = essential, 0 = none) set static::ExpiresDebug 0 # Overwrite cache headers in response # (1 = yes, 0 = no) set static::ExpiresOverwrite 0 } when CLIENT_ACCEPTED { # The name of the Data Group (aka class) we are going to use set vname [URI::basename [virtual name]] set vpath [URI::path [virtual name]] set Expires_clname "${vpath}Expires$vname" if {! [class exists $Expires_clname]} { log local0.notice "Data Group $Expires_clname not found." } } when HTTP_REQUEST { # The log prefix so you can find yourself in the log set Expires_lp "VS=[virtual name], URI=[HTTP::uri]" if {[class exists $Expires_clname]} { set period [string last . [HTTP::path]] if { $period >= 0 } { # Set the timeout based on the class entry if it exists for this request. set expire_content_timeout [class match -value [string tolower [getfield [string range [HTTP::path] $period end] ";" 1]] ends_with $Expires_clname] if { ($static::ExpiresDebug > 1) and ($expire_content_timeout ne "") } { log local0. "$Expires_lp: found file suffix based expiration: $expire_content_timeout." } } else { set expire_content_timeout "" } } } when HTTP_RESPONSE { if { [info exists expire_content_timeout] } { # if expire_content_timeout not set then no class was found if { [HTTP::header exists "Content-Type"] } { # Set the tiemout based on the class entry if it exists for this mime type. # TODO: Allow globbing in matching mime-types set expire_content_timeout [class match -value [string tolower [HTTP::header "Content-Type"]] starts_with $Expires_clname] if { ($static::ExpiresDebug > 1) and ($expire_content_timeout ne "") } { log local0. "$Expires_lp: found mime type based expiration: $expire_content_timeout." } } if { $expire_content_timeout ne "" } { # either matched Content-Type or file extention if { $static::ExpiresOverwrite or not [HTTP::header exists "Expires"] } { HTTP::header replace "Expires" "[clock format [expr ([clock seconds]+$expire_content_timeout)] -format "%a, %d %h %Y %T GMT" -gmt true]" if { ($static::ExpiresDebug > 0) } { log local0. "$Expires_lp: Set 'Expires' to '[clock format [expr ([clock seconds]+$expire_content_timeout)] -format "%a, %d %h %Y %T GMT" -gmt true]'." } } elseif { [HTTP::header exists "Expires"] } { set expire_content_timeout [expr [clock scan "[HTTP::header Expires]" -gmt true] - [clock seconds]] if { $expire_content_timeout < 0 } { if { ($static::ExpiresDebug > 0) } { log local0. "$Expires_lp: Found 'Expires' header either invalid or in the past." } return } if { ($static::ExpiresDebug > 0) } { log local0. "$Expires_lp: Found 'Expires' header and calculated $expire_content_timeout seconds timeout." } } if { $static::ExpiresOverwrite or not [HTTP::header exists "Cache-Control"] } { HTTP::header replace "Cache-Control" "max-age=$expire_content_timeout, public" if { ($static::ExpiresDebug > 0) } { log local0. "$Expires_lp: Set 'Cache-Control' to 'max-age=$expire_content_timeout, public'." } } } } }975Views0likes3CommentsDisable Partial GET Requests for RAM Cache
Problem this snippet solves: A side effect of using RAM Cache is that partial GET requests (206 Partial Content - see RFC 2616 sections 10.2.7 and 14.35) is enabled by BIG-IP for objects available in RAM Cache, even if the server that produced the content did not support partial GET. While partial GET requests are usually a benefit in terms of bandwidth, some applications (such viewing PDF documents with Adobe Reader) react poorly when the browser (such as Internet Explorer) attempts to make use of this capability. Here is an iRule that disables partial GET requests by removing the "Accept-Ranges" header that advertises it. The example can be expanded upon to disable partial GET requests only for certain conditions (such as the URI path ends in ".pdf"). Code : when CACHE_RESPONSE { HTTP::header remove "Accept-Ranges" }533Views0likes1CommentCache Manipulation Examples
Problem this snippet solves: One iRule will allow for different cache times in the event that not all objects should be cached at the same rate. Another contains some fixes for Safari back-button issues. And yet another for Movable Type and TypeKey. Just a footnote, I've found that these behave differently as bugs get fixed in the LTM code, so stuff that works in one version of code may need some modification in another version of LTM code. iRule 1 creates two time intervals, one for 60 seconds and another for 5 minutes. Keep in mind that ramcache needs to be enabled and the 'Maximum Age' in the profile should be longer than any of the timers specified. Also you should have the bigip insert the 'Age: ' header. iRule 2 is handy for Mac related sites. Safari has some rather unique features that force the browser to retain a separate object for hitting the back button. This iRule seems to solve this. iRule 3 is a nice one for sites that use Movable Type and TypeKey. We have a site that we want to cache, unless someone is accessing the admin cgi's or logged in via TypeKey. Interesting thing about TypeKey, if you log in it sends a cookie "tk_commenter=BlahBlah" but when you logout it will send the cookie, but it is blank. This rule looks to see if the cookie exists, and if so it will look to ensure that the cookie isn't blank. The first switch can be omitted, I just like to keep them there because I'll probably put them to use at some point. iRule 4 is for caching permanent redirects Code : ### iRule 1: Cache Timers ### when CACHE_REQUEST { if { [CACHE::age] > 60 } { switch -glob [string tolower [HTTP::uri]] { "/shorttimer1/*" - "/shorttimer2/*" - "/shorttimer3/*" { CACHE::expire log local0. "Matched 60 seconds" } } } elseif { [CACHE::age] > 300 } { switch -glob [string tolower [HTTP::uri]] { "/fiveminutes1/*" - "/fiveminutes2/*" - "/fiveminutes3/*" { CACHE::expire log local0. "Matched 5 minutes" } } } } ### iRule 2: Mac related sites ### when HTTP_REQUEST { set mytime [clock seconds] } when HTTP_RESPONSE { HTTP::header insert Last-Modified "[clock format $mytime -format "%a, %d %b %Y %T %Z"]" HTTP::header insert Cache-Control "no-store, no-cache, must-revalidate, max-age=0" HTTP::header insert Cache-Control "post-check=0, pre-check=0, false" HTTP::header insert Pragma "no-cache" } ### iRule 3: Movable Type & TypeKey ### when HTTP_REQUEST { if { [HTTP::cookie exists "tk_commenter"]} { if { [string length [HTTP::cookie value "tk_commenter"]] } { switch -glob [string tolower [HTTP::uri]] { default { log local0. "tk_commenter cookie exists, [IP::client_addr] has been passed to origin with cookie: [HTTP::cookie value "tk_commenter"]" CACHE::disable pool mypool } } } } switch [string tolower [HTTP::path]] { "/mt-comments.cgi" - "/mt-search.cgi" - "/mt.cgi" { log local0. "mt-comments or mt.cgi accesses from [IP::client_addr]" CACHE::disable pool mypool } default { pool mypool } } } ### iRule 4: Caching Permanent Redirects ### when HTTP_RESPONSE { if { [HTTP::status] == 301 }{ CACHE::enable } }372Views0likes0Comments