Using iRules To Manipulate Cache

Description

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. This iRule 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.
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"
   }
  }
 }
}

Here is another cache related iRule that 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. The following seems to help resolve this:
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"
}

This 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.
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
  }
 }
}

Here’s another for caching permanent redirects:
when HTTP_RESPONSE {

   if { [HTTP::status] == 301 }{
      CACHE::enable
   }
}

The BIG-IP API Reference documentation contains community-contributed content. F5 does not monitor or control community code contributions. We make no guarantees or warranties regarding the available code, and it may contain errors, defects, bugs, inaccuracies, or security vulnerabilities. Your access to and use of any code available in the BIG-IP API reference guides is solely at your own risk.