Cache 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
   }
}
Published Jan 30, 2015
Version 1.0
No CommentsBe the first to comment