Crates.io | ConsistentHashing |
lib.rs | ConsistentHashing |
version | 0.1.0 |
source | src |
created_at | 2020-08-10 03:36:37.114336 |
updated_at | 2020-08-10 03:36:37.114336 |
description | ConsistentHashing Alogrithm in Rust |
homepage | |
repository | |
max_upload_size | |
id | 274901 |
size | 8,345 |
Consistent Hashing is a distributed system algorithm for assigning values to partitions in a highly dynamic environment.
Using the algorithm to LB redis cache. (Crate redis = "0.17.0"
)
let mut ring: ConsistentHash<String,redis::Client> = match ConsistentHash::new(100) {
Ok(ring) => ring,
Err(err) => panic!(err),
};
ring.add_node("redis://my-redis-node-1".to_string(), redis::Client::open("redis://my-redis-node-1")?).unwrap();
ring.add_node("redis://my-redis-node-2".to_string(), redis::Client::open("redis://my-redis-node-2")?).unwrap();
ring.add_node("redis://my-redis-node-3".to_string(), redis::Client::open("redis://my-redis-node-3")?).unwrap();
ring.add_node("redis://my-redis-node-4".to_string(), redis::Client::open("redis://my-redis-node-4")?).unwrap();
let client = match ring.get_node(String::from("some-key")){
Some(node) => node,
None => panic!("Not found!"),
};
let mut con = client.get_connection()?;
con.get("some-key");