Detailed methods nginx variables (4)

In the case where the "acquisition process procedure", Nginx variables can also choose to value the container as a cache, so that when a plurality of times to read a variable, you only need to call "get handler" once. Here we will look at an example like this:
map $args $foo {
        default     0;
        debug       1;
    }

    server {
        listen 8080;

        location /test {
            set $orig_foo $foo;
            set $args debug;
            echo "orginal foo: $orig_foo";
            echo "foo: $foo";
        }
    }
Here for the first time used the map configuration directives Standard ngx_map module, we need to introduce here. map in English in addition to the "map" and also "Mapping" means. For example, high school mathematics speak of "function" is a "mapping." This map Nginx and instructions can be used to define the mapping relationship between the two variables Nginx, or a function. Back to the example above, we use the map command defines a user variable $ foo and $ args built-in mapping the relationships between variables. In particular, a function of mathematical notation y = f (x), our $ args is the "independent variable" x, and $ foo is "dependent variable" y, that is, the value of $ foo is $ args to determine the value, or the writing order can say that we value the $ args variable is mapped to the $ foo variable. Now we look at the map directive defines the mapping rules:
map $args $foo {
        default     0;
        debug       1;
    }
default braces in the first row is a special match conditions, namely when other conditions are not matched, this condition will match. When this default condition match, put the "dependent variable" $ foo maps to the value 0. The second line of braces mean to say, if the "independent variable" $ args debug exactly match the string, put " the dependent variable "$ foo maps to the value 1. These two lines together, we get a complete mapping rules as follows: when the value is equal to $ args debug, when the value is 1 $ foo variable, or the value of $ foo on 0. understand the meaning of Directive map, look at location / test. in there, we put the current value of $ foo variable is stored in the variable $ orig_foo another user, and then forced to rewrite the value of $ args debug, Finally, we were then echo command output value of $ foo and $ orig_foo. From a logical point of view, it seems that when we are forced to rewrite a value of $ args debug, based on the previous map mapping rule, the value of $ foo variable at this time should be automatically adjusted to a string 1, regardless of the original value of $ foo what . However, the test results is not the case:
$ curl 'http://localhost:8080/test'
    original foo: 0
    foo: 0
The first line indicates $ orig_foo output value of 0, which is what we wanted: The above does not provide the request parameter string URL, then the initial value of $ args is empty, then in accordance with the mapping rule we defined earlier, $ foo when the first value of the variable to be read should be 0 (i.e., the default matching the default condition). And the second output line of the display, after forcibly rewriting a string value of the variable $ args Debug, $ foo condition is still 0, which is clearly inconsistent with mapping rules, when $ args because when Debug, the value should be $ foo 1. Why is this it? In fact, the reason is very simple, that is $ foo variable in the first reading, the calculated value is cached mapping rules to live. We have just said, Nginx module can be selected using the value of its container variables created as part of its "get handler" cached results of calculations. Obviously, ngx_map module that calculates a mapping between the variables expensive enough, due to the need to automatically cached variable calculation results, so that if the reading again dependent variable, Nginx cached results can be returned directly to live in the duration of the current request, instead of calling the variable "get handler" re-calculated. To further verify this, we may wish to specify the URL parameters directly in the request string to debug:
$ curl 'http://localhost:8080/test?debug'
    original foo: 1
    foo: 1
We see now the value of $ orig_foo became 1 because the variable foo the first time it is read, the argument value is the $ args debug, then according to the mapping rules, "get handler" to calculate the value returned by $ 1. when is the follow-up and re-read the value of $ foo, you always get a result is cached to live, regardless of the $ args later turned into what the. map instruction is actually a special case, because it can be registered "get handler" for the user variables, and the user can define their own "get handler" of the calculation rules. Of course, the rule here is defined as a mapping relationship with another variable. At the same time, not all used the "get handler" of the variables are cached results, for example in front of us (c) has seen $ arg_XXX does not use the value of the container cache. Similarly ngx_map module, standard ngx_geo modules use the same caching mechanism variable values. In the above example, it should be noted that we are outside the map server configuration instruction block, which is defined in the outermost http configuration block. Many readers may feel this strange, after all, we just use it in the location / test in. This is not because we do not want to map statements directly moved to the location in the config block, but because the map command can only be used at http blocks! Nginx so many newcomers are concerned about "global" scope of the map is set to make access to all location requests interfaces are all virtual hosts mapping is performed to calculate the variable value again, but it is not. Earlier works we have learned that map configuration command is registered as a user variable "get handler", and the actual map calculation is done in the "get handler", and "get handler" only if the user is variable the actual execution will be (of course, because of the presence of the cache, only read the first time at the request of the lifetime was only executed) when reading, so there is no use for those requests related variables, it simply can not perform any useless calculations. This technical objects calculated only on the actual use value in the object, called "lazy evaluation" (lazy evaluation) in computing. Provide a "lazy evaluation" semantics of programming languages ​​are rare, the most classic example is Haskell. In contrast is the "active evaluation" (eager evaluation). We are fortunate in Nginx also saw an example of "lazy evaluation", but the "active evaluation" semantic actually more common in Nginx inside, for example, the following line common, but the set statement:
set $b "$a,$a";
Here will be set when a predetermined operation is performed assignment, "active" to calculate the variable b value of $, without calculating the evaluated delay time variable $ b to actually read.

Reproduced in: https: //my.oschina.net/766/blog/211251

Guess you like

Origin blog.csdn.net/weixin_34337381/article/details/91545848