-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
52 lines (41 loc) · 1.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 werbenhu
// SPDX-FileContributor: werbenhu
package main
import (
"fmt"
"github.com/werbenhu/chash"
)
func main() {
// Create a group named "db" with 10000 virtual elements for each key
dbHash, _ := chash.CreateGroup("db", 10000)
// insert three mysql server elements
dbHash.Insert("192.168.1.100:3306", []byte("mysql0-info"))
dbHash.Insert("192.168.1.101:3306", []byte("mysql1-info"))
dbHash.Insert("192.168.1.102:3306", []byte("mysql2-info"))
// create a group named redis
redisHash, _ := chash.CreateGroup("redis", 10000)
// insert three redis server elements
redisHash.Insert("192.168.1.100:6379", []byte("redis0-info"))
redisHash.Insert("192.168.1.101:6379", []byte("redis1-info"))
redisHash.Insert("192.168.1.102:6379", []byte("redis2-info"))
// get the mysql server close to where the user to in the circle by userid
user1DbHost, user1Dbinfo, err := dbHash.Match("user-id-1")
if err != nil {
panic(err)
}
fmt.Printf("user-id-1 matched db host:%s, info:%s\n", user1DbHost, user1Dbinfo)
// get the mysql server close to where the user to in the circle by userid
user1RedisHost, user1RedisInfo, err := redisHash.Match("user-id-1")
if err != nil {
panic(err)
}
fmt.Printf("user-id-1 matched redis host:%s, info:%s\n", user1RedisHost, user1RedisInfo)
// delete a mysql server element from the group's circle
dbHash.Delete("192.168.1.101:3306")
user1DbHost, user1Dbinfo, err = dbHash.Match("user-id-1")
if err != nil {
panic(err)
}
fmt.Printf("user-id-1 matched db host:%s, info:%s\n", user1DbHost, user1Dbinfo)
}