Forum Discussion
Mike_62629
Nimbostratus
Apr 07, 2008Has anyone successfully used dicts in iRules?
Has anyone successfully used dicts in iRules?
I'm currently iterating through a loop doing string comparisons and think it may be a bit more efficient to do a single dictionary lookup instead. I was having trouble getting dicts to pass the iRule Editor's syntax checking. I'm new to both iRules and TCL, so it could just be my own inexperience with the base language.
I plan to move the logic for this outside of the iRule altogether soon, but for now, here's where I am:
set myPool Application-Pool
set myExtensions {.css .gif .gif .ico .jpeg .jpg .js}
set myPath [string tolower [HTTP::path]]
pull out the extension from the path
set myIndex [string last {.} $myPath]
set myExtension [string range $myPath $myIndex end]
if the requested object's extension is in the list, serve from cache
foreach extension $myExtensions {
if { [ $extension equals $myExtension] } {
set myPool Cache-Pool
}
}
pool $myPool
I'd like to move toward something like this:
set myExtensions(.css) 1
set myExtensions(.gif) 1
set myExtensions(.ico) 1
set myExtensions(.jpeg) 1
set myExtensions(.jpg) 1
set myExtensions(.js) 1
set myPath [string tolower [HTTP::path]]
pull out the extension from the path
set myIndex [string last {.} $myPath]
set myExtension [string range $myPath $myIndex end]
if { dict exists $myExtensions $myExtension } {
pool Cache-Pool
} else {
pool Application-Pool
}
- please note, this isn't the only logic used to determine cacheability, but it's the component I think could most benefit from refactoring. The list / dictionary is larger, and I'd rather do one lookup instead of 20+ string comparisons for every request. It's a fairly highly trafficked site.
- Nicolas_Menant
Employee
Hi,when HTTP_REQUEST { if {[matchclass [HTTP::path] ends_with $::Extension ] } { pool Cache-Pool } else { pool Application-Pool } }
- Colin_Walker_12Historic F5 Accountnmenant is on the right track here. There's no need to do a dictionary lookup, although that's an interesting (and sneaky!) way of getting around multiple comparisons.
- Mike_62629
Nimbostratus
By Class do you guys mean a TCL class that's a subclass of string, or is this some F5/BigIP feature? - Mike_62629
Nimbostratus
Nevermind on that, it seems pretty simple. The DevWiki had some stuff on it. - Mike_62629
Nimbostratus
class CacheableExtensions { .css .gif .ico .jpg .jpeg .js } when HTTP_REQUEST { if { [matchlass [HTTP::path] ends_with $::CacheableExtensions] } { pool Cache-Pool } else { pool Application-Pool } }
- Mike_62629
Nimbostratus
Would it be more appropriate to initialize my class within RULE_INIT so it doesnt have to be initialized and destroyed with each request? - Mike_62629
Nimbostratus
Yeap, thanks. I kept reading the matchclass wiki page and found out about data groups. What mislead me to think it was to be implemented in code was the fact that it includes a snippet of what the data group ends up looking like in the bigip.conf file. Skimming the doc made me think I had to code it somewhere.class CacheableExtensions { .css .js .gif *SNIP* .jpg } class CacheableDirs { /user/docs/ /user/images/ /user/videos/ } when HTTP_REQUEST { if { [matchclass [string tolower [HTTP::path]] starts_with $::CacheableDirectories] } { pool Cache-Pool } elseif { [matchclass [string tolower [HTTP::path]] ends_with $::CacheableExtensions] } { pool Cache-Pool } else { pool Application-Pool } }
- Dave_Whitla_254
Nimbostratus
That's great, except sometimes you really want a dict. - Dave_Whitla_254
Nimbostratus
Actually - this may still be current: - hoolio
Cirrostratus
Hi Dave,when HTTP_REQUEST { Save the request headers split on a carriage return set request_headers [split [HTTP::request] \\\r] } when HTTP_RESPONSE { log local0. "request header count: [llength $request_headers]" Loop through the request headers for {set i 0} {$i < [llength $request_headers]} {incr i} { Trim off the leading \r\n from each list element and log it log local0. "$i: [string trimleft [lindex $request_headers $i]]" } }
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects