REDIS 中的递归搜索

Recursive search in REDIS

在 ETCD 中,假设以下是键值对:

company/google/employees/employee1 employeeID1

company/google/employees/employee2 employeeID2

company/amazon/employees/employee1 employeeID1

company/amazon/employees/employee2 employeeID2

当我在 "company/amazon/" 上执行 Fetch 时,我获得了亚马逊所有员工的员工 ID(这同样适用于 "company/google/")并且在 "company/" 上执行的 fetch 会获取所有员工 ID。 这在 REDIS 中受支持吗?如果不是,我们如何做同样的事情?

提前致谢。

一种方法是使用散列和 HSCAN 对密钥进行模式匹配。

包含所有数据的散列将扫描封装到仅相关键,而不是数据库中的所有键。

HSCAN 的优势在于它 return 是键和值。 KEYSSCAN 只有 return 键,然后你需要获取值。

> HSET myData company/google/employees/employee1 employeeID1
(integer) 1
> HSET myData company/google/employees/employee2 employeeID2
(integer) 1
> HSET myData company/amazon/employees/employee1 employeeID1
(integer) 1
> HSET myData company/amazon/employees/employee2 employeeID2
(integer) 1
> HSCAN myData 0 MATCH company/amazon/* COUNT 100
1) "0"
2) 1) "company/amazon/employees/employee1"
   2) "employeeID1"
   3) "company/amazon/employees/employee2"
   4) "employeeID2"
> HSCAN myData 0 MATCH company/google/* COUNT 100
1) "0"
2) 1) "company/google/employees/employee1"
   2) "employeeID1"
   3) "company/google/employees/employee2"
   4) "employeeID2"
> HSCAN myData 0 MATCH company/* COUNT 100
1) "0"
2) 1) "company/google/employees/employee1"
   2) "employeeID1"
   3) "company/google/employees/employee2"
   4) "employeeID2"
   5) "company/amazon/employees/employee1"
   6) "employeeID1"
   7) "company/amazon/employees/employee2"
   8) "employeeID2"