ngx_hash.{c|h} implements a more important hash structure in nginx, which is often used in module configuration analysis. The hash structure is read-only. Only the key-val pairs stored in it can be given during initial creation, and then only "add, delete, modify, and check" operations can be performed.
First let’s take a look at the memory layout of the hash structure:

[cpp] view
plaincopyprint?
- typedef struct {
- ngx_hash_t *hash; / Points to the hash structure to be initialized
- ngx_hash_key_pt key; Function pointer for column values
ngx_uint_t max_size; - //Maximum number of buckets allowed
ngx_uint_t bucket_size; - //The maximum space allowed for each bucket
- char ) ngx_pool_t *pool; //Use In the memory pool that distributes the laidance structure
- ngx_pool_t*temp_pool; // Memory Ponds used to allocate temporary data space}} ngx_hash_init_t;
Specific meaning meaning Also look at the pictures to understand. The code is not parsed here. Although it looks cumbersome, it is still quite convenient to use. - General operations include creating hash and searching in hash. Create hash:1. Construct an array with ngx_hash_key_t as a member, including key, value and a hash value calculated using key
2. Construct a variable of the ngx_hash_init_t structure, which contains the members of ngx_hash_t, as The hash structure also includes some other initial settings, such as bucket size, memory pool, etc. - 3. Call ngx_hash_init and pass in the ngx_hash_init_t structure, the array of ngx_hash_key_t, and the length of the array for initialization, so that the hash member of ngx_hash_init_t is ours The required hash structure
The search process is very simple
1. Calculate the hash value of the key
2. Use ngx_hash_find to search. You need to pass in the hash value and key at the same time. What is returned is the value pointer
It should be noted that , nginx’s hash uses the linear search method after bucketing, so when the number of buckets is determined, the search efficiency is inversely proportional to the total number of key-val pairs
The above introduces the nginx source code study notes (10) - basic container - ngx_hash, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.