Forum Discussion
Robert_47833
Altostratus
Nov 14, 2011how to use switch to match datagroup
1:there is a datagroup:cjj
how to use switch to match this group
switch -glob [HTTP::path] {
"cjj" xxxx ???
2:
switch -glob [HTTP::path] { "*jpg" - "*bmp" - "*gif" - "*png" - "*img" - "*ico" - "*js" - "*css" - "*swf" - "*swz" - "*vbs" - "*html" - "*htm" - "*csv" - "*txt" - "*crossdomain.xml" {pool xyz}
in this case,should I create a variable uri to replace [HTTP::path] to make the irule more efficent?
25 Replies
- nitass
Employee
1
Data Group List use in a switch
http://devcentral.f5.com/Community/GroupDetails/tabid/1082223/asg/50/aft/1179074/showtab/groupforums/Default.aspx
2
in your case, using [HTTP::path] is more efficent. - Robert_47833
Altostratus
Hi,nitass
I have read one link to ask me to use variable if we need to evaluate it repetitively
http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/281/Ten-Steps-to-iRules-Optimization.aspx
6- Use Variables - nitass
Employee
http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/281/Ten-Steps-to-iRules-Optimization.aspx
6- Use Variables since you do nothing on [HTTP::path], i do not think using variable is more efficient. - Robert_47833
Altostratus
1:it seems datagroup can't be used in switch
2:I use [HTTP::path] to evaluate each url included in the request to hit the bigip,it seems it matches what has been said in this link
http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/281/Ten-Steps-to-iRules-Optimization.aspx - hoolio
Cirrostratus
Jucao,
What are you actually trying to do? Do you want to see if an HTTP path ends with a set of extensions?
If so, you could either use a switch statement or the class command with the ends_with operator and a datagroup. There isn't a logical way to combine a switch statement and a class lookup in one operation. From Joe's testing, a datagroup is probably more efficient than a switch statement if you consider matches in the beginning, middle and end of the list of strings:
http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/1086424/Comparing-iRule-Control-Statements.aspx
However, when using a small list of strings (<100) the results are close enough that it probably makes sense to choose a switch statement if it's easier for you to manage the strings inline within the iRule instead of in a datagroup.
Aaron - Robert_47833
Altostratus
Hi,Aaron
sorry for making any confusion here
they are 2 different questions
1:I want to know whether datagroup can be applied in switch synatx
2:I 'd like to know in wich case I should use variable
like set path [HTTP::path]
switch -glob $path { "*jpg" - "*bmp" - "*gif" - "*png" - "*img" - "*ico" - "*js" - "*css" - "*swf" - "*swz" - "*vbs" - "*html" - "*htm" - "*csv" - "*txt" - "*crossdomain.xml" {pool xyz}
or switch -glob [HTTP::path] { "*jpg" - "*bmp" - "*gif" - "*png" - "*img" - "*ico" - "*js" - "*css" - "*swf" - "*swz" - "*vbs" - "*html" - "*htm" - "*csv" - "*txt" - "*crossdomain.xml" {pool xyz}
I have read one link to ask me to use variable if we need to evaluate it repetitively
http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/281/Ten-Steps-to-iRules-Optimization.aspx
6- Use Variables - Michael_Yates
Nimbostratus
Hi jucao,
As Hoolio pointed out, if you are after efficiency then you can go with a switch statement over a Data Group. Some (myself included) view management of a Data Group easier to keep track of (and you can make modifications without having to alter your iRule).
You can so it with something like:when HTTP_REQUEST { if { [class match [string tolower [HTTP::uri]] ends_with image_types ] } { pool image_server_pool } else { pool content_server_pool } } - hoolio
Cirrostratus
1: I want to know whether datagroup can be applied in switch syntax
No. A switch statement can't reference a datagroup.
2: I 'd like to know in which case I should use variable
As you're only referencing HTTP::path once there isn't any reason to use a variable. If you were running an operation against a command multiple times you'd want to run it just once and then save the output to a variable. For example, if you were setting a URI to lower case and checking it multiple times, you'd want to save the lower case URI rather than running string tolower multiple times.
Aaron - Robert_47833
Altostratus
oh,I got u
so for example :
switch -glob [HTTP::path] {
"*gif" -
"*png" {
switch -glob [HTTP::path] {
"/static.*" -
"/s.*" {
switch -glob [HTTP::path] {
"*cswebtool*" -
"*cobrands*" {
persist none
pool SRWD52-STATIC
return
}
}
}
}
}
in this case ,I can save the output of HTTP::path to a variable,right? because I run [HTTP::path] 3 times - Robert_47833
Altostratus
hmm,I should use variable if one command run more than 1 time,right?
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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
