In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of redis how to generate a tree, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this redis how to generate a tree article, let's take a look at it.
In general, in order to facilitate the management of redis cache, we use: to separate different key to store cache, which is easy to view.
For example:
Game:upload_role:1000game:member_info:2000game:member_info:state_info:3000
The above structure is shown in Redis Desktop Manager as follows:
We can get all the key in the redis through the keys command. But these key have no hierarchy, how can they be generated?
The relationship between the upper and lower layers of each key can only be handled through the: delimiter.
The code is as follows:
Function relationCache ($keys, & $index, & $index_tree) {$result = []; if ($keys) {foreach ($keys as $key) {$arr = explode (':', $key); $len = count ($arr); for ($ix = 0; $ix)
< $len; $ix++) { $cur_key = implode(':', array_slice($arr, 0, $ix + 1)); if (!isset($index_tree[$cur_key])) { $index_tree[$cur_key] = $index++; $pid = 0; if ($ix >= 1) {$pre_key = implode (':', array_slice ($arr, 0, $ix)); $pid = $index_tree [$pre_key] } $result [] = ['id' = > $index_tree [$cur_key],' pid' = > $pid, 'name' = > $arr [$ix],' key' = > $cur_key,] }} return $result;}
Then the tree function is generated as follows:
Function genTree ($items, $id = 'id', $pid =' pid', $son = 'child') {$tree = array (); $tmpMap = array (); foreach ($items as $item) {$tmpMap [$item [$id] = $item;} foreach ($items as $item) {if (isset ($tmpMap [$item [$pid])) {$tmpMap [$item [$pid] [$son] [] = & $tmpMap [$item [$id]] } else {$tree [] = & $tmpMap [$item [$id]];}} unset ($tmpMap); return $tree;}
The use is as follows:
$keys = ['game:upload_role:1000',' game:member_info:2000', 'game:member_info:state_info:3000',]; / / Index $index = 1 keys / index tree $index_tree = []; / / Note that if you want to call relationCache multiple times and share the index, please pass parameters externally $result = relationCache ($keys, $index, $index_tree); $result = genTree ($result,' id', 'pid',' children') Echo'; print_r ($result)
The resulting results can be displayed using zTree through json_encode.
This is the end of the article on "how to generate a tree in redis". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to generate a tree in redis". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.