关于keys指令

来源:13-10 实现KEYS命令集(三)

慕瓜9063200

2022-07-30

在keys.go文件execKeys(db *DB, args [][]byte)函数中,既然已经知道这里的args指令是keys *,就是要遍历所有的key然后返回

 // 源代码
pattern := wildcard.CompilePattern(string(args[0]))
result := make([][]byte, 0)
db.data.ForEach(func(key string, val interface{}) bool {
	if pattern.IsMatch(key) {
		result = append(result, []byte(key))
	}
	return true
})



// 为什么不能这么写
db.data.ForEach(func(key string, val interface{}) bool {
	result = append(result, []byte(key))
	return true
})

这个key不就是遍历过程中遍历到的key吗,为什么还需要做一个IsMatch判断,为什么不能直接加入result中

写回答

1回答

Moody

2022-07-30

判断match是针对一般情况,因为输入的条件可能不是*。当然如果你觉得需要优化一下,可以在输入*的时候跳过match
1
0

深入Go底层原理,重写Redis中间件实战

深入Go语言原理+Go重写Redis

474 学习 · 172 问题

查看课程