golang 八行代码实现的一致性哈希
A Fast, Minimal Memory, Consistent Hash Algorithm
https://arxiv.org/abs/1406.2294
https://github.com/dgryski/go-jump
// Hash consistently chooses a hash bucket number in the range [0, numBuckets) for the given key. numBuckets must be >= 1.
func Hash(key uint64, numBuckets int) int32 {
var b int64 = -1
var j int64
for j < int64(numBuckets) {
b = j
key = key*2862933555777941757 + 1
j = int64(float64(b+1) * (float64(int64(1)<<31) / float64((key>>33)+1)))
}
return int32(b)
}
0
See Also
- 请教一下站长一个 golang template 语法
- Golang boltdb 与 leveldb 的读写性能比较
- golang 两个优秀的websocket 库
- 百度统计代码里有弹疮、暗疮?
- 代码中的变量名中既有‘-’也有‘_’,用法含义上有什么区别吗?
Nearby
- 上一篇 › 怎样防止html代码格式化?
- 下一篇 › 完全按照教程安装,为什么装不了?