Forum Discussion

Emre_27149's avatar
Emre_27149
Icon for Nimbostratus rankNimbostratus
May 03, 2011

disable ram cache while when get http error codes

Hi All,

I have the below irule code but I also want to disable the ram cache feature when a backend server returned a http error codes (404, 303... ) to clients.So how can I need to update the rule to accomplish my goal.?

 

Thank you,

 

 

 

rule cache_rule3 {

 

when HTTP_REQUEST {

 

if { [ class match [HTTP::path] starts_with cache_obj] } {

 

CACHE::enable

 

log local0. "Caching [HTTP::path]"

 

} else {

 

log local0. "NON Caching [HTTP::path]"

 

CACHE::disable

 

log local0. "Non Caching [HTTP::path]"

 

}

 

}

 

3 Replies

  • Hi Emre,

     

     

    Here's an option:

     

     

    when HTTP_REQUEST {
    
       set uri [HTTP::uri]
    
       if { [ class match [HTTP::path] starts_with cache_obj] } {
    
          CACHE::enable
          log local0. "Caching [HTTP::path]"
    
       } else {
    
          log local0. "NON Caching [HTTP::path]"
          CACHE::disable
       }
    }
    when HTTP_RESPONSE {
    
        Check the response status code
       switch [HTTP::status] {
    
          404 -
          500 -
          503 {
             CACHE::disable
             log local0. "Disabled cache for [HTTP::status] response to $uri"
          }
          default {
             CACHE::enable
             log local0. "Enabled cache for [HTTP::status] response to $uri"
          }
       }
    }
    

     

     

    If you wanted to enable caching only for 200, 301, 302, etc responses, you could flip the CACHE::enable/disable statements:

     

     

    when HTTP_REQUEST {
    
       set uri [HTTP::uri]
    
       if { [ class match [HTTP::path] starts_with cache_obj] } {
    
          CACHE::enable
          log local0. "Caching [HTTP::path]"
    
       } else {
    
          log local0. "NON Caching [HTTP::path]"
          CACHE::disable
       }
    }
    when HTTP_RESPONSE {
    
        Check the response status code
       switch [HTTP::status] {
    
          200 -
          301 -
          302 {
             CACHE::enable
             log local0. "Enabled cache for [HTTP::status] response to $uri"
          }
          default {
             CACHE::disable
             log local0. "Disabled cache for [HTTP::status] response to $uri"
          }
       }
    }
    

     

     

    Aaron
  • Hi Aaron,

     

    Thank you so much for your interest and quick response.

     

    Kindly..

     

  • I know this is digging up a really old post, but one caveat on hoolio's second recommendation:

     

    If you wanted to enable caching only for 200, 301, 302, etc responses, you could flip the CACHE::enable/disable statements:

     

    > when HTTP_REQUEST {
    > 
    >    set uri [HTTP::uri]
    > 
    >    if { [ class match [HTTP::path] starts_with cache_obj] } {
    > 
    >       CACHE::enable
    >       log local0. "Caching [HTTP::path]"
    > 
    >    } else {
    > 
    >       log local0. "NON Caching [HTTP::path]"
    >       CACHE::disable
    >    }
    > }
    > when HTTP_RESPONSE {
    > 
    >     Check the response status code
    >    switch [HTTP::status] {
    > 
    >       200 -
    >       301 -
    >       302 {
    >          CACHE::enable
    >          log local0. "Enabled cache for [HTTP::status] response to $uri"
    >       }
    >       default {
    >          CACHE::disable
    >          log local0. "Disabled cache for [HTTP::status] response to $uri"
    >       }
    >    }
    > }

    Without 304 in the 'CACHE::enable' half of the switch statement in the HTTP_RESPONSE event, RAMCache doesn't properly catch responses from the originating web server on checks for expired objects (at least on 10.2.4 HF3).