简单研究了下市面上Redis管理器key分隔符算法的时间下面分享下.
效果
下面展示了其他redis管理器的效果和我们要实现的目标效果
常见效果
目标实现效果
实现算法
可以看到上面通过冒号分割符将 key 分割成层级目录方便管理查看.
实现步骤
- scan获取redis指定数量key
- 循环遍历所有key
- 指定转换分割符号
:
或者其他符号为数组 - 判断数组是否有多个
- 只存在一个则直接赋值键
- 存在多个则赋值引用类型到临时变量
- 然后循环赋值键到临时变量
难点在于 &
引用类型的理解
实现算法
$iterator = null;
$all_keys = $redis->scan($iterator, $search, 1000);
$keys = [];
foreach ($all_keys as $datum) {
$key_array = explode(':', $datum);
if (count($key_array) > 1) {
$keys_temp = &$keys;
foreach ($key_array as $key_array_value) {
$keys_temp = &$keys_temp[$key_array_value];
}
} else {
$keys[$datum] = '';
}
}
结果
array(14) {
["member_click_key"] => string(0) ""
["token-31"] => string(0) ""
["token-71"] => string(0) ""
["token-24"] => string(0) ""
["api"] => array(1) {
["limit"] => array(3) {
[202209151615] => array(1) {
["ip"] => array(1) {
["192.168.116.2"] => NULL
}
}
[202209151614] => array(1) {
["ip"] => array(1) {
["192.168.116.2"] => NULL
}
}
[202209151616] => array(1) {
["ip"] => array(1) {
["192.168.116.2"] => &NULL
}
}
}
}
["goods_stock_number_45"] => string(0) ""
["token-70"] => string(0) ""
["token-3"] => string(0) ""
["token-13"] => string(0) ""
[1111] => string(0) ""
["token-12"] => string(0) ""
["token-14"] => string(0) ""
["token-1"] => string(0) ""
["collectionList"] => string(0) ""
}
渲染树形结构
博主这里使用的是 bootstrap-treeview
控件 如果使用其他树形控件大致逻辑一致
实现算法
function generate_tree(&$data, &$return_data)
{
$i = 0;
$new_data = &$data;
$new_return_data = &$return_data;
foreach ($new_data as $key => $item) {
if (is_array($item)) {
$new_return_data[$i] = [
'text' => $key,
'nodes' => [],
];
generate_tree($item, $new_return_data[$i]['nodes']);
} else {
$new_return_data[$i] = [
'text' => $key,
'icon' => "fa fa-key",
];
}
$i++;
}
}
结果
array(14) {
[0] => array(2) {
["text"] => string(16) "member_click_key"
["icon"] => string(9) "fa fa-key"
}
[1] => array(2) {
["text"] => string(8) "token-31"
["icon"] => string(9) "fa fa-key"
}
[2] => array(2) {
["text"] => string(8) "token-71"
["icon"] => string(9) "fa fa-key"
}
[3] => array(2) {
["text"] => string(8) "token-24"
["icon"] => string(9) "fa fa-key"
}
[4] => array(2) {
["text"] => string(3) "api"
["nodes"] => array(1) {
[0] => array(2) {
["text"] => string(5) "limit"
["nodes"] => array(3) {
[0] => array(2) {
["text"] => int(202209151615)
["nodes"] => array(1) {
[0] => array(2) {
["text"] => string(2) "ip"
["nodes"] => array(1) {
[0] => array(2) {
["text"] => string(13) "192.168.116.2"
["icon"] => string(9) "fa fa-key"
}
}
}
}
}
[1] => array(2) {
["text"] => int(202209151614)
["nodes"] => array(1) {
[0] => array(2) {
["text"] => string(2) "ip"
["nodes"] => array(1) {
[0] => array(2) {
["text"] => string(13) "192.168.116.2"
["icon"] => string(9) "fa fa-key"
}
}
}
}
}
[2] => array(2) {
["text"] => int(202209151616)
["nodes"] => array(1) {
[0] => array(2) {
["text"] => string(2) "ip"
["nodes"] => array(1) {
[0] => array(2) {
["text"] => string(13) "192.168.116.2"
["icon"] => string(9) "fa fa-key"
}
}
}
}
}
}
}
}
}
[5] => array(2) {
["text"] => string(21) "goods_stock_number_45"
["icon"] => string(9) "fa fa-key"
}
[6] => array(2) {
["text"] => string(8) "token-70"
["icon"] => string(9) "fa fa-key"
}
[7] => array(2) {
["text"] => string(7) "token-3"
["icon"] => string(9) "fa fa-key"
}
[8] => array(2) {
["text"] => string(8) "token-13"
["icon"] => string(9) "fa fa-key"
}
[9] => array(2) {
["text"] => int(1111)
["icon"] => string(9) "fa fa-key"
}
[10] => array(2) {
["text"] => string(8) "token-12"
["icon"] => string(9) "fa fa-key"
}
[11] => array(2) {
["text"] => string(8) "token-14"
["icon"] => string(9) "fa fa-key"
}
[12] => array(2) {
["text"] => string(7) "token-1"
["icon"] => string(9) "fa fa-key"
}
[13] => array(2) {
["text"] => string(14) "collectionList"
["icon"] => string(9) "fa fa-key"
}
}
完整代码
$iterator = null;
$all_keys = $redis->scan($iterator, $search, 1000);
$keys = [];
foreach ($all_keys as $datum) {
$key_array = explode(':', $datum);
if (count($key_array) > 1) {
$keys_temp = &$keys;
foreach ($key_array as $key_array_value) {
$keys_temp = &$keys_temp[$key_array_value];
}
} else {
$keys[$datum] = '';
}
}
/* 整合节点 */
generate_tree($keys, $result_keys);
var_dump($result_keys);
以上我们就实现了redis键key任意分割符层次算法
评论已关闭